Get the task instance from a execution listener


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 {

    public void notify(DelegateExecution execution) throws Exception {
              // Is null !


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 {

	public void notify(DelegateTask delegateTask) {
		//get the id of the current task



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 {

    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(

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

processEngineServices.getRuntimeService() or

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