cancel
Showing results for 
Search instead for 
Did you mean: 

Customized handling of UserTasks

jolivier
Champ in-the-making
Champ in-the-making
Hi,

I start using Activiti and would like to use my own front-end for user tasks. So I want my custom code to be called each time a user task is created. My options are:
- Poll for taskService.createTaskQuery().taskCandidateGroup(group.list(); for all groups and all users I know, this seem to be very expensive.
- Add a TaskListener to my user tasks in my xml processes definitions. This looks great, but I need to do that for all user tasks and all processes that my code will start, so I guess this could be automated.
- I try to automate this by modifying on the fly all created Process like this:

BpmnModel model = new BpmnModel();
      Process process;
      try {
         XMLInputFactory xif = XMLInputFactory.newInstance();
         XMLStreamReader xtr = null;
         InputStreamReader in = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("myprocess.bpmn20.xml"), "UTF-8");
         xtr = xif.createXMLStreamReader(in);
         process = new ProcessParser().parse(xtr, model);
                        // I can modify here my process and add the TaskListeners
                        for (UserTask userTask : process.findFlowElementsOfType(UserTask.class)) {
                                // Add the listener, but how?
                        }
      } catch (Exception e) {
         throw new RuntimeException("Could create process from xml.", e);
      }

      String deploymentId = repositoryService
            .createDeployment()
            .addBpmnModel("dynamic-model.bpmn", model).name("Dynamic process deployment.")
            .deploy()
            .getId();
The two issue with this way is that
- I don't see how to add the listeners programmaticaly to the UserTask.
- It looks like I circumvent a lot of logic this way (validation of the xml for instance), so I would prefer a way with less impact.

What is the preferred way to do this?

Thanks in advance
1 REPLY 1

trademak
Star Contributor
Star Contributor
The best way to solve this is to add a Task listener for every process definition that gets deployed. You can do this by adding a BpmnParseHandler like described in the userguide.

http://activiti.org/userguide/index.html#advanced_parseHandlers

Best regards,