External Task list is empty even when tasks are waiting at the camunda process instance activity

Hi,
I have below camunda process:

and listens to topic:

I intend to use external task(this is supposed to complete tasks) in the same SpringBoot app and also use Camunda Java API and not REST API to retrieve the external tasks to complete them.

This is the delegate code that is supposed to retrieve the task and complete it.

Here is Kotlin delegate code that tries to fetch tasks:

@Component
class ExternalTaskExecutor(
  private val externalTaskService: ExternalTaskService
) : JavaDelegate {

  private val tasks = externalTaskService.fetchAndLock(1, "breakConcurrencyWorker")
    .topic("breakConcurrency", 60L * 1000L)
    .execute()

  override fun execute(execution: DelegateExecution) {
    val taskId = tasks.firstOrNull()?.id

    if (taskId != null) {
      externalTaskService.complete(taskId, "breakConcurrencyWorker")
    }
  }
}

But the “tasks” list is empty even when there are camunda process instances waiting at the “external task” activity. Why?

I see that I have initialised the tasks variable outside the execute() methods so the variable is always empty. Just move it inside the execute() method to fix that.

The tasks list is empty in this delegate because it is initialized in the class constructor. When the class is created, the fetchAndLock method is executed, and any available tasks are fetched and locked. However, once the tasks list is populated with those tasks, it is not refreshed, and it remains the same for the lifetime of the ExternalTaskExecutor instance.

I have to loop the camunda flow back to the delegate because until delegate is done the complete() state is not really flushed to database