Mar 14

jBPM Task node

jBPM task nodes are designed to interact with users. The main utility of this kind of nodes is to treat forms data. When a process arrives to a task node, it waits until the required actor makes an action.

Task nodes are the most complex nodes, you will have to implement two handlers: one to assign the task to an actor and another to define this task.

Lets start with the AssignmentHandler.

Using Eclipse interface you can setup your task node as it follows, specifying where is your class.

jBPM AssignmentHandler

ExampleAssign must be something like that:

public class ExampleAssign implements AssignmentHandler{
....
}

Implementing this interface means that you will have to develop an assign method.

public void assign(Assignable assignable, ExecutionContext executionContext) throws Exception;

Inside this function you will need to assign the current task(assignable) to somebody using assignable.setActorId(actor).

The responsibility of this method does not finish here. When you came into a task node, you will have to save all process state and variables to stop the process. You can not assume that the actor will be waiting for the assignment and neither you will want to wait until he logins into the application. System must continue running normally.

Calling this two functions you will save the execution context into database using Hibernate.

executionContext.getJbpmContext().save(executionContext.getTaskInstance());
executionContext.getJbpmContext().close();

Next step is to configure and develop a task controller.

jBPM task controller config

This class must look like this:

public class ControlExample implements TaskControllerHandler{
...
}

It must implement this two methods:

public void initializeTaskVariables(TaskInstance taskInstance, ContextInstance contextInstance, Token token);
public void submitTaskVariables(TaskInstance taskInstance, ContextInstance contextInstance, Token token);

First one is used to initialize task variables, you can set default values or whatever you need.

Second one treat all task variables. This is the main function of the task nodes. Remember to invoke token.signal(); when all treatment is done.

Thats all, but you will be wondering how does it work, how can I wake up the sleeping process…

Somewhere in your program, in a from treatment function or in a button event, for example, you will have to make some extra work. You will need to identify the user and the process instance. Functions like:

 List l = jbpmContext.getTaskMgmtSession().findTaskInstances(user);

will help you. When you have the correct TaskInstance you will have to invoke ti.end();. This function will wake up the process and the associated state from database using Hibernate and call submitTaskVariables(). Workflow will continue normally at this point.

Thats all I wanted to explain of jBPM nodes. I’m not normally using Fork and Join nodes in my projects, so I will not write a guide for them. They are very easy to include in your workflows but if somebody ask for it, I will write a mini how-to.

Related Posts