How to create Activiti Event Listener with Spring Boot Example

Activiti provides event mechanism that allows us to get notified when various events occur within the engine. In this tutorial, we’re gonna look at an example that uses Activiti Event Listener with Spring Boot.

Related Posts:
Introduction to Activiti – a Java BPM Engine
Activiti Message Event with Spring Boot Example

I. Activiti Event Listener Overview

1. Implementation

The first thing we have to do is implementing org.activiti.engine.delegate.event.ActivitiEventListener interface. You can find all supported event types at Activiti User Guide.

isFailOnException() method will be called when onEvent() method throws an exception. Returning false means the exception is ignored.

All events dispatched are a subtype of org.activiti.engine.delegate.event.ActivitiEvent.
From the event, we can get (if available) type, executionId, processInstanceId and processDefinitionId.

2. Registering

The second thing we do is to register the ActivitiEventListener implementation above.

2.1 At runtime

We can add or remove event listener to the engine by using RuntimeService‘s methods:

2.2 in the BPMN XML

We can add event listener to a specific process definition. The listeners will only be called for events related to process instances that are started with that process definition.

Notes for registering in process definition:
– Event listener can only be declared as a child-element of the extensionElements inside process definition, and cannot be defined on any individual activity in the process.
– There is only a single instance of that class to be created. Make sure the listener implementations do not rely on member-fields and ensure safe usage from multiple threads/contexts.

II. Practice

1. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.4.RELEASE
– Spring Boot: 1.5.3.RELEASE

2. Project Overview

activiti-eventlistener-example

We will start the Process by message, then complete 2 Tasks to see how EvenListener works.

activiti-eventlistener-structure

MyEventListener is an implementation of ActivitiEventListener that contains onEvent() method to be called everytime an event (related to Process) occurs.
– Process definition will be written under XML format in process/process.bpmn20.xml file.
MyService provides service methods for Controller such as: startProcess(), getTasks(), completeTask(). Those functions can be done with help of some @Autowired objects which are instances of these classes:
+ RuntimeService
+ TaskService
MyController is a REST Controller which has request mapping methods for RESTful requests such as: /startprocess, /tasks, /completetask.
pom.xml contains dependencies for:
+ Activiti Spring Boot Starter Basic
+ Spring Boot Starter Web
+ H2 database: Activiti Engine needs a database to store its data. By default, it will connect to an in-memory H2 database.

3. Step by step
3.1 Create Spring Boot project

Using Spring Tool Suite/Eclipse to create Project and add Dependencies to pom.xml file:

3.2 Create implementation of ActivitiEventListener

3.3 Define Process

Under src/main/resources, create processes folder and add process.bpmn20.xml file:

3.4 Create Service Class

3.5 Create Controller Class

3.6 Run & Check Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results by open your browser and send request:

Request 1: Start Process by MyMessage and assign tasks to John
http://localhost:8080/startprocess/MyMessage?assignee=John

Request 2: show tasks of John
http://localhost:8080/tasks/John

Request 3: complete Task
http://localhost:8080/completetask?id=11

“New Task” has been assigned and created.

Request 4: show tasks of John
http://localhost:8080/tasks/John

Request 5: complete New Task
http://localhost:8080/completetask?id=15

III. Source Code

SpringActivitiEventListener


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*