cancel
Showing results for 
Search instead for 
Did you mean: 

Activiti Integration: How receive notification for UserTask

joeysr20det
Champ in-the-making
Champ in-the-making
We are integrating Activiti into our own software standing up and configuring our own ProcessEngine. We've managed to get most everything working, but we're a bit stumped on handling UserTasks when they are encountered in a given Process Instance. We are creating our own Activiti Explorer type interface that will query the engine's task service for a given user to display tasks they need to complete. I've got a good understanding on this portion, so my question is:

Is there any way to receive notification from the Process Engine saying "hey user blah needs to complete task blah"? I can't seem to find a place anywhere on the ProcessEngine instance, any of ProcessEngine's services, or an a ProcessInstance to place a listener. I know you can manually insert a listener in the BPMN XML for a workflow, but I don't want my user to have to add a listener on every single UserTask they put in their workflow. The only way I can think of right now, is to occasionally poll the ProcessEngine's TaskService looking for tasks to complete. Our software is very large with tons of data and thousands of users, polling is just too intensive for our application.

Is there anything I have missed here?

Thank you for your time.
5 REPLIES 5

naag
Champ in-the-making
Champ in-the-making
You can create a custom BpmnParseListener implementation to add execution listeners globally for all processes:


public class CustomBpmnParseListener implements BpmnParseListener {
   // …

@Override
public void parseUserTask(Element userTaskElement, ScopeImpl scope, ActivityImpl activity) {
  activity.addExecutionListener(ExecutionListener.EVENTNAME_CREATE, new MyHandler());
}
}

Then add it to your process engine configuration:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="processEngineConfiguration" class="org.activiti.cdi.CdiJtaProcessEngineConfiguration">
    <property name="customPostBPMNParseListeners">
      <list>
        <bean class="my.company.activiti.CustomBpmnParseListener" />
      </list>
    </property>
  </bean>
</beans>

I've seen more examples on the forums, just search for the BpmnParseListener interface. Of course this not only applies to user tasks, but all BPMN elements that are parsed by Activiti.

Regards,
Peter

joeysr20det
Champ in-the-making
Champ in-the-making
Awesome. This is exactly what I was after. I can figure it out from here.

Thanks!

joeysr20det
Champ in-the-making
Champ in-the-making
So from your example above,

Code:
public class CustomBpmnParseListener implements BpmnParseListener {
   // …

   @Override
   public void parseUserTask(Element userTaskElement, ScopeImpl scope, ActivityImpl activity) {
      activity.addExecutionListener(ExecutionListener.EVENTNAME_CREATE, new MyHandler());
   }
}


the ActivityImpl's addExecutionListener only takes an ExecutionListener and ExecutionListener does not contain a EVENTNAME_CREATE only ExecutionListener.START, ExecutionListener.END, ExecutionListener.TAKE. I can't seem to see how to use ExecutionListener here; is there something I'm missing?

joeysr20det
Champ in-the-making
Champ in-the-making
Ok I found what I needed to do. I instead just need to cast activity.getBehavoir to a UserTaskActivityBehavior and then add my listener. i.e.

  @Override
  public void parseUserTask(Element userTaskElement, ScopeImpl scope,
    ActivityImpl activity)
  {
    ActivityBehavior ab = activity.getActivityBehavior();
    if (ab instanceof UserTaskActivityBehavior)
    {
      UserTaskActivityBehavior uab = (UserTaskActivityBehavior)ab;
      uab.getTaskDefinition().addTaskListener(TaskListener.EVENTNAME_CREATE, new MyTaskListener());
    }

Thanks again for getting me in the right direction.

naag
Champ in-the-making
Champ in-the-making
Seems I didn't read your post carefully enough, but your solution looks like what we're also doing 🙂 And also what Alfresco is doing (e.g. http://svn.alfresco.com/repos/alfresco-open-mirror/alfresco/HEAD/root/projects/repository/source/jav...).

Cheers