Get the task instance from a execution listener


#1

I have an execution listener bind to the start of task (start: Java Class). How can I get the task id of this task?

public class WaitForReceiptAlert implements ExecutionListener {

    @Override
    public void notify(DelegateExecution execution) throws Exception {
              // Is null !
              execution.getProcessEngineServices().getTaskService().createTaskQuery().taskId(execution.getId())
     }
}

#2

If you want the task id, it’s probably better to use a Task Listener than an execution listener because the execution listener will run before the task comes into existence.

public class test implements TaskListener {

	@Override
	public void notify(DelegateTask delegateTask) {
		//get the id of the current task
		delegateTask.getId();

	}
}

#3

Hello,
because currently Camunda Modeler supports only Execution Listener template, not Task Listener, I faced the same problem.
I thought about it and today one idea came into my mind - Threads… Aaaand? It works! Finally! :ok_hand: :laughing:

package com.yourcompany.example;

import org.camunda.bpm.engine.ProcessEngineServices;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.camunda.bpm.engine.task.Task;

public class MyExecutionListener implements ExecutionListener {

    @Override
    public void notify(DelegateExecution delegateExecution) throws Exception {

        class RunnableTask implements Runnable {
            ProcessEngineServices processEngineServices;
            String processInstanceId;
            String executionId;

            RunnableTask(ProcessEngineServices processEngineServices, String processInstanceId, String executionId) {
                this.processEngineServices = processEngineServices;
                this.processInstanceId = processInstanceId;
                this.executionId = executionId;
            }

            public void run() {
                while(processEngineServices.getTaskService().createTaskQuery().processInstanceId(processInstanceId).executionId(executionId).singleResult() == null) {
                    // do nothing, wait for task instantiation
                }
                // get the task
                Task task = processEngineServices.getTaskService().createTaskQuery().processInstanceId(processInstanceId).executionId(executionId).singleResult();
                // just make sure that task really exists
                if(task != null) {
                    // YOUR (TaskListener) CODE
                }
            }
        }

        // since the delegateExecution instance is destroyed after Execution Listener end,
        // at least these three objects below can be useful ;)
        Thread thread = new Thread(
                            new RunnableTask(
                                delegateExecution.getProcessEngineServices(),
                                delegateExecution.getProcessInstanceId(),
                                delegateExecution.getId()));
        thread.start();
    }
}

EDIT: If you want to get or set (local) variable(s) use:

processEngineServices.getRuntimeService() or
processEngineServices.getTaskService()

As I say normally, I hope it will help to somebody :slight_smile: