Hi and welcome to DeveloperLife, written by Nadav Azaria and Roi Gamliel. In this blog we write about our own experience in software development, trying to share our knowledge following new technologies we adopt or problems we face.

Wednesday, February 22, 2012

Extending the Task Service in Activiti Engine

As we described in our latest post,
we have been trying out Activiti lately. A challenge we came across was
to create our own Human Task Service of some sort, as we
had used our own Identity Management. In a typical BPM system the Process Engine and Human Task Service are decoupled from one another. Moreover, a standard called WS-HumanTask Service exists for establishing a protocol between the last two. However, in Activiti's case the Human Task Service is embedded inside the engine.

So we took a different approach as we wanted to use our own human task server. We thought that if we will be notified about changes occurring inside Activiti's engine concerning user tasks (e.g. create task event), we could propagate those notifications to our Human Task Service. Unfortunately, we found out that Activiti's engine does not have a notification mechanism as we hoped (at least in 5.8 - there are talking about an event bus in future releases), so we had to use another mechanism to achieve this task.

Apparently Activiti does have a notification mechanism while parsing a BPMN process definition XML. It allows one to plug in a listener which will be notified in several events. One of those events is when a user task is being parsed. This was exactly what we needed. We plugged our own listener implementation and every time a user task was parsed we added a TaskListenr to the user task in the process definition. This listener was invoked by the engine when this task was created. Obviously the listener impl is to simply send a notification as "Task Created" to our own Human Task Service. Note: you will also have to manage the task life-cycle and update Activiti's engine using its services.

adding a Java class implementing BpmnParseListener. This is the interface you want to implement to plug in your own listener.

injecting the Java class using the Spring xml config file.

BpmnParseListener Class
We implement a TaskListener for each of the events we are interested in:

CompleteTaskListenerImpl

AssigneTaskListenerImpl

CreateTaskListenerImpl

We Override the parseUserTask method so that in each time a parse event occurs our TaskListeners will add some logic on our WS-HumanTask Service/Task Service. The rest of the parse events will be ignored.