2
votes

My requirement is to execute some java code on entry of every user task. Referring to the user guide, I was able to achieve this using the On Entry Action(bpmn).

Since the same piece of code has to be executed on entry of every user task, I do not want to configure it on all user tasks. Is there any interface which jbpm provides where I can specify the On Entry Action programmatically rather than specifying on each user task in the bpmn.

1

1 Answers

4
votes

There are many different options here.

  1. If I take your question quite literally, and if you are sure your requirement holds true for ALL human tasks, the sledgehammer approach would be to intercept the parsing of the UserTask process node by subclassing org.jbpm.bpmn2.xml.UserTaskHandler and injecting your custom OnEntry action by overriding the handleNode method. It's a bit tricky but have a look at org.jbpm.bpmn2.xml.AbstractNodeHandler.handleScript(ExtendedNodeImpl, Element, String) as an example on how to do it. Unfortunately, this was the easy bit. Now you have to register your custom handler in your own equivalent subclass of the org.jbpm.bpmn2.xml.BPMNSemanticModule and make sure your implementation is used which is a challenge depending on the environment you use jBPM in. If you are interested I can elaborate but unless the other approaches don't work for you, I would advise against this approach.

  2. You can also register your own implementation of the org.jbpm.services.task.lifecycle.listeners.TaskLifeCycleEventListener interface. I think the method you would be interested in is
    public void beforeTaskAddedEvent(TaskEvent event); With this option you loose the ProcessContext which means you do not have easy access the process state, which is perhaps not what you are looking for.

  3. The best approach I would say is to implement your own org.kie.api.event.process.ProcessEventListener and override the void beforeNodeTriggered(ProcessNodeTriggeredEvent event) or void afterNodeTriggered(ProcessNodeTriggeredEvent event) depending on what state you are interested in. Put your recurring logic in these methods, but make sure you only execute it for NodeInstances that return an instanceof org.jbpm.workflow.core.node.HumanTaskNode when you call getNode(). Both the TaskLifecycleEventListener and ProcessEventListener implementations can be registered in your kmodule.xml file

There are other options too (see Access node variables in ProcessEventListener) but it gets tricky.