avatarGhailene ben Marzouk

Summary

The provided content discusses the implementation and usage of Execution and Task Listeners in Camunda, which are triggered by specific events during the lifecycle of tasks and processes.

Abstract

The article delves into the concept of Listeners within Camunda, a workflow and decision automation platform. It distinguishes between Execution Listeners and Task Listeners, explaining their roles and when they are triggered. Execution Listeners are associated with the start, execution, and end of a process or task, and they require the implementation of the ExecutionListener interface with the notify method. Task Listeners, on the other hand, are triggered by events related to User Tasks, such as creation, assignment, completion, and deletion, and they also require implementation of the TaskListener interface with the notify method. The article includes practical examples with code snippets and BPMN diagrams to illustrate how to set up these listeners in a Camunda process, demonstrating the order of execution and the passing of variables between tasks. It concludes with a note of gratitude to the reader and provides links to additional Camunda-related resources and a GitHub repository for further exploration.

Opinions

  • The author emphasizes the importance of Listeners in Camunda for executing custom logic at specific points in a process.
  • Practical examples and code snippets are provided to enhance understanding and serve as a guide for implementation.
  • The article suggests that using Listeners can significantly enhance the functionality of Camunda processes by allowing for dynamic behavior and data manipulation.
  • The inclusion of screenshots from Camunda's Cockpit interface demonstrates the real-world application and observable outcomes of using Listeners.
  • By offering a GitHub repository with code examples, the author encourages readers to experiment with and learn from practical implementations of Execution and Task Listeners.
  • The article concludes with an invitation for further engagement, indicating the author's willingness to assist with additional information and fostering a community of learning around Camunda.

Listener in Camunda

A Listener in Camunda can be Execution Listener or Task Listener, it will trigger when an event start or stop.

For the User task : the Listener can be triggred when :

create : when creating a userTask

assignment: when assign a userTask to a user, or claming it

complete: when the userTask is completed

delete: when the userTask is deleted

Execution Listener:

Execution Listener can be used in the end or the start of a task, or in the start event or in the start of the process:

we gonna create a bmpn file, in main process we gonna set in the start an event, this event is of type start. it will be implemtented by a java class.

The class which will be called by an event listener must implement the internface ‘ExecutionListener’ and override the method notify.

First of all we gonna create a event lister with type “start” when starting the process:

This is the implementation in java class:

we gonna create a todayDate variable with the date of today

The serviceTask Print today date will call a java class in which we gonna retreive the value of the todayDate inserted from the start event listener:

finally when the service task completed (the execution of task is completed) we gonna, call an event listener (end event listener) in which we gonna create another variable date.

Let s run the process:

as we can see the process is execution and the order of the execution is well displayed:

  1. the start event listener
  2. the service task
  3. the end event listener of the service task

in cockpit:

Github repo: https://github.com/ghailen/execution-event-listener-camunda

Task Listener

Now we gonna talk about task listener. when using a user task we can trigger a listener this listener can be triggred, when creating,deleting,claming or completing a user task.

a class which present a listener must implement the TaskListener interface and override the notify method.

For the bpmn file: we create two usertasks, we putted a task listener in the first one, and another one when completing the second task.

The first task:

The second task : we gonna try to inject ‘using the interface TASKLIST” a variable named ghailene in the first task and get it in the completion of the second task

let s start the process:

as we can see in the console : we printed the event of tasklistener when triggred (in our case is : create )

the variables (no variable yet)

the task name: Set variable

Now let fill the variable ghailene to read it in the next task.

lets complete it.

now nothing is printed in the console. and the new task “check data” is displayed. lets complete it

after the completion of the “check data” we can see in the log:

=> the variable is successfully loaded after the completion of the second task “check data”.

Git repo:

Thank you for reading my article, please contact me for any further information.

Other Camunda topics:

camunda task events notifier camunda genericuser task listener in camunda task marker multi-instance taskmarker loop in camunda feel language camunda how to work with dmn how to work with dmn how-to-use-embedded-task-form-in-camunda all-about-camunda-database error-handling-in-camunda sub-process-in-camunda gateways-in-camunda camunda-with-spring-boot-example task-in-camunda events-in-camunda

Camunda
Listener
Tasks
Event
Recommended from ReadMedium