cancel
Showing results for 
Search instead for 
Did you mean: 

Problems adding TaskListener from ParseHandler

felipe1
Champ in-the-making
Champ in-the-making
I'm trying to add custom a TaskListener to every UserTask declared in my processes.

I have looked at this bit of the documentation (http://www.activiti.org/userguide/#advanced_parseHandlers) to get me started.

I have also found this example of Handler that insert a custom handler to UserTasks - https://github.com/jbarrez/activiti-advanced-scripting/blob/master/src/test/java/org/activiti/Execut....

So I created the class shown below. It seems that I have did everything according to the documentation, however it is not working.  The problem is that I cannot get a reference to a TaskDefinition object.

I assumed that The ProcessDefinitionEntity#getTaskDefinitions() method would return all the tasks defined in the current process, then it would be easy to get the task with the taskDefinitionKey, but that is not the case.

When I open an external start form, the parse() method is called 3-4 times, and the return from the getTaskDefinitions() method is different on every call.



public class CustomParse implements BpmnParseHandler {

   @Override
   public Collection<Class<? extends BaseElement>> getHandledTypes() {
      List<Class<? extends BaseElement>> elements = new ArrayList<>();
      elements.add(UserTask.class);
      return elements;
   }

   @Override
   public void parse(BpmnParse bpmnParse, BaseElement element) {
      String taskDefinitionKey = element.getId(); // returns the correct task key
      Map<String, TaskDefinition> tarefas = ((ProcessDefinitionEntity) bpmnParse.getCurrentScope().getProcessDefinition()).getTaskDefinitions();
      
      for(Entry<String, TaskDefinition> e : tarefas.entrySet()) {
         System.out.println("****************** " + e.getKey() + " :: " + e.getValue());
      }
      
      TaskDefinition taskDefinition = ((ProcessDefinitionEntity) bpmnParse.getCurrentScope().getProcessDefinition()).getTaskDefinitions().get(
            taskDefinitionKey);

      if (taskDefinition != null) { // taskDefinition is always null at this point
         taskDefinition.addTaskListener(TaskListener.EVENTNAME_CREATE, new CustomTaskListener());
      }
   }
}
16 REPLIES 16

trademak
Star Contributor
Star Contributor
Could you create a unit test showing the issue?

Thanks,

felipe1
Champ in-the-making
Champ in-the-making
I'm attaching a unit test and supporting files. The test does not have any asserts, so it won't fail, but analyzing the console outputs will help you understand what is happening.

felipe1
Champ in-the-making
Champ in-the-making
Update:

- I have changed the activiti configuration file. I used "postBpmnParseHandlers" instead of "preBpmnParseHandlers", now the listener is added to all user tasks as expected.

I'm still facing an issue though.  My TaskListener uses injected resources,

<code>
@Named
public class NotificaTarefaCriadaActiviti implements TaskListener {
@Inject
private Event<EventoCriacaoTarefa> evento; // null

@Inject
private ProcessEngine processEngine; // null
}
</code>

so if I simply instantiate a listener and add it to the task definition, the injected resources will resolve to null:

<code>
taskDefinition.addTaskListener(TaskListener.EVENTNAME_CREATE, new NotificaTarefaCriadaActiviti() );
</code>

I have tried to inject a listener from the ParseHandler, but that also resolved to null:

<code>
public class ParseHandlerCriaTarefaListener implements BpmnParseHandler {
@Inject
private NotificaTarefaCriadaActiviti listener; //null

        …..
}
</code>

When I declare the listener from the process definition file, like so:

<code>
   <extensionElements>
    <activiti:taskListener
     event="create"
     delegateExpression="#{notificaTarefaCriadaActiviti}"></activiti:taskListener>
   </extensionElements>
</code>

The resources are injected correctly into the Listener.

trademak
Star Contributor
Star Contributor
Thanks, please create a Maven project and make that available if that's possible.

Best regards,

felipe1
Champ in-the-making
Champ in-the-making
Ok Tijs, I'll prepare a simple project and get back to you.

Thanks again!

felipe1
Champ in-the-making
Champ in-the-making
I have created a very simple web project that reproduces exactly what I have reported so far:

https://github.com/felipe-gdr/activiti-parsehandler

I have shared it in GitHub.

Please let me know if you need anything else.

Thanks again 🙂

trademak
Star Contributor
Star Contributor
Thanks for sharing the source code.
The problem is that the @Inject doesn't work with a parse handler.
The parse handlers are instantiated by the Activiti Engine with the default constructor.

Best regards,

felipe1
Champ in-the-making
Champ in-the-making
Right, can you help me think of a workaround for my problem?

- I need to inject resources in my TaskListener (specifically a ProcessEngine instance and a Event instance)
- The TaskListener has to be added through a ParseHandler

frederikherema1
Star Contributor
Star Contributor
Tasklistener instances you create, in your parse-handler, are not aware of @Inject annotations. You should locate the needed resources in the ParseHandler yourself from the container you're in and set them on the listener instance… Or get an instance that is injected from the container.
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.