Chapter 4. Actions : The JBoss jBPM Integration Mechanism

In this chapter we will show how to do software integration with JBoss jBPM. The standard mechanism to realize this is to wrap the functionality you want to integrate in a class that implements the ActionHandler interface.

4.1. Creating a Hello World Action

Each Hello World process should integrate one or more Hello World actions, so this is what we will be doing. We can integrate custom code at different points in the process definition. To do this we have to specify an action handler, represented by an implementation of the ActionHandler interface, and attach this piece of code to a particular event. These events are amongst others, going over a transition, leaving or enterning nodes, after and before signalling.

A Simple Hello Action

Figure 4.1. A Simple Hello Action

To make things a little bit more concrete, we will implement an action handler. To do this, create a new class called HelloActionHandler, which implements the ActionHandler interface and implement the execute method as shown in Figure 4.1, “A Simple Hello Action ” and Figure 4.2, “A Simple Hello Action ”. This test will add a variable named 'greeting' to the collection of process variables and puts a message in it : "Hello from ActionHandler".

A Simple Hello Action

Figure 4.2. A Simple Hello Action

4.2. Integrating the Hello World Action

As good Testcity citizens we will first create a unit test that proves the behaviour we want to achieve by adding the action handler to the process. So we implement another test. Creating the process instance is code we already saw in the previous chapter. We assert that no variable called greeting exist. Then we give the process a signal to move it to the first state. We want to associate the execution of the action with the event of going over the transition from the start state to the first state. So after the signal, the process should be in the first state as in the previous scenario. But moreover, the 'greeting' variable should exist and contain the string "Hello from ActionHandler". That's what we assert in the last lines of the test method shown in Figure 4.3, “Create the Hello Action Test ”

Create the Hello Action Test

Figure 4.3. Create the Hello Action Test

Running the tests now results in a failure. This is shown in Figure 4.4, “Test Results Before Integration” As a matter of fact, we did not associate the action with any particular event in the process definition, so the process variable did not get set.

Test Results Before Integration

Figure 4.4. Test Results Before Integration

Let's do something about it and add an action to the first transition of our sample process. This is done by bringing up the context menu of the transition in the tree page of the outline view, as shown in Figure 4.5, “Adding an Action to a Transition ”.

Adding an Action to a Transition

Figure 4.5. Adding an Action to a Transition

Right-clicking the action brings up a popupmenu with an entry to edit the properties of the selected action. Clicking on this menu entry opens a configuration dialog for actions. For now we will only configure the name and class properties of this action. The rest will be treated later. We enter 'hello' as the name of the action and click on the 'Browse...' button to open a choose class dialog where we can look for the classes implementing the ActionHandler interface in our project's classpath (Figure 4.6, “The Choose Action Handler Dialog ”).

The Choose Action Handler Dialog

Figure 4.6. The Choose Action Handler Dialog

The Action Configuration Dialog

Figure 4.7. The Action Configuration Dialog

We choose our previously created 'HelloActionHandler' class and push the 'OK' button (Figure 4.7, “The Action Configuration Dialog ”). After the selection of the action handler for the action, we can run the test and observe it gives us a green light (Figure 4.8, “The Action Configuration Dialog ”).

The Action Configuration Dialog

Figure 4.8. The Action Configuration Dialog

4.3.  Integration Points

The different integration points in a process definition are thoroughly documented in the JBoss jBPM User Guide. As you see on Figure 4.9, “Adding an Event Action ” different kinds of actions can be added to for instance nodes. Adding such an action will create an event object in the outline view and add an action as a child of this newly created event. In the same way, actions can be added to the process definition object. Moreover, actions can directly be added to events.

Adding an Event Action

Figure 4.9. Adding an Event Action