Process Parsing to add task listeners

cancel
Showing results for 
Search instead for 
Did you mean: 
mprakash
Active Member II

Process Parsing to add task listeners

Jump to solution

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 Solution

Accepted Solutions
mprakash
Active Member II

Re: Process Parsing to add task listeners

Jump to solution

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 solution in original post

4 Replies
afaust
Master

Re: Process Parsing to add task listeners

Jump to solution

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
Active Member II

Re: Process Parsing to add task listeners

Jump to solution

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.

mprakash
Active Member II

Re: Process Parsing to add task listeners

Jump to solution

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.

mprakash
Active Member II

Re: Process Parsing to add task listeners

Jump to solution

Any answers?

Thanks everyone

process parsing