cancel
Showing results for 
Search instead for 
Did you mean: 

Process Parsing to add task listeners

mprakash
Confirmed Champ
Confirmed Champ

I am trying to hook into process parsing to add task listeners to all tasks. I tried the following config:

     <bean id="processEngineConfiguration"
          class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">

             ...
          <property name="postBpmnParseHandlers">
               <list>
                    <bean class="xxx.yyy.DecorateTask" />
               </list>
          </property>
     </bean>

Where the code for DecorateTask is:

public class DecorateTask extends AbstractBpmnParseHandler<UserTask> {

     // private List<ActivitiListener> listeners = new ArrayList<>();
     private String logfile = "logger.txt";
     FileWriter log;

     public  DecorateTask() {
          try {
               log = new FileWriter(logfile, true);
               log.write("Initialising Devorator\n");
          } catch (IOException e) {
               e.printStackTrace();
          }
     }
     @Override
     protected Class getHandledType() {
          return UserTask.class;
     }

     @Override
     protected void executeParse(BpmnParse bpmnParse, UserTask element) {
          try {
               log.write(" >> " + element.getClass().getName() + "\n");
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
     }

}

However, the class is not even being instantiated, as I see no entries in the output file. Ideally, I want to intercept all tasks to emit events for internal logging purposes, but I tried to use BaseElement instead of UserTask, and that did not work either.

Any clues how to make it work will be greatly appreciated.

Thanks,

  Mayank.

1 ACCEPTED ANSWER

mprakash
Confirmed Champ
Confirmed Champ

That was user error-I wasn't flushing the file. Adding log.flush() fixed that.

However, I still haven't solved the basic problem--I want to add a listener to all User and Script tasks, which should get called upon entry and exit to the task, so I can generate some events. Ideally, this should be done automatically on all processes when they are parsed, so the definition creator does not have to manually add the listeners to each task.

What is the best way to do that? I can add listeners in the designer, but how can I do it programmatically? The parsing hook does see to have a way to do that.

Thanks.

View answer in original post

4 REPLIES 4

afaust
Legendary Innovator
Legendary Innovator

If your class is not even instantiated at all then it stands to reason that some Spring configuration may be overriding your configuration of the postBpmnParseHandlers property, otherwise Spring would have to call the constructor of your class.

mprakash
Confirmed Champ
Confirmed Champ

That was user error-I wasn't flushing the file. Adding log.flush() fixed that.

However, I still haven't solved the basic problem--I want to add a listener to all User and Script tasks, which should get called upon entry and exit to the task, so I can generate some events. Ideally, this should be done automatically on all processes when they are parsed, so the definition creator does not have to manually add the listeners to each task.

What is the best way to do that? I can add listeners in the designer, but how can I do it programmatically? The parsing hook does see to have a way to do that.

Thanks.

I found a possible solution in one of the threads, but it only partly works. It seems that ((ProcessDefinitionEntity) bpmnParsegetCurrentScope().getProcessDefinition()).getTaskDefinitions() is non-empty only for User Tasks. For Service and Script Tasks, it is an empty collection.

What will be the best way to attach a listener to all task nodes in a process definition at parsing time?#processparsing

Any answers are greatly appreciated. 

BTW, I am using Activiti 5.22 (CE).

Thanks.

Any answers?

Thanks everyone

process parsing