Chapter 5. 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.

5.1. Creating a Hello World Action

Each hello process should integrate one or more hello 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 entering nodes, after and before signalling.

A Simple Hello Action

Figure 5.1. A Simple Hello Action


To make things a little bit more concrete, we will implement an ActionHandler . To do this, create a new class called HelloActionHandler , which implements the ActionHandler interface and implement the execute method as shown in the next figure. 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 5.2. A Simple Hello Action


5.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 ActionHandler 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.

Create the Hello Action Test

Figure 5.3. Create the Hello Action Test


Running the tests now results in a failure. 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 5.4. Test Results Before Integration


Let's do something about it and add an action to the first transition of our sample process. To do this you can use the Actions tab in the Properties Editor that is under the graphical canvas. Bring up the popup menu of the action element container and chose New Action as it’s shown on the screenshot below. The other way to add an action to the transition is simply to use the dropdown menu that is available under the action icon in the right upper corner of the Properties View.

Adding an Action to a Transition

Figure 5.5. Adding an Action to a Transition


After adding the action a tabbed view with three pages will appear.

Configuration Dialog for an Action

Figure 5.6. Configuration Dialog for an Action


The first of these three pages allows you to give the Action a name. The last page contains some advanced attributes such as whether the Action is asynchronous. The Details page is the most important. It allows to choose and configure the actual action handler implementation.

The Details page of an Action Configuration Dialog

Figure 5.7. The Details page of an Action Configuration Dialog


Clicking on the Search... button brings us to a Choose Class dialog.

The Choose Action Handler Dialog

Figure 5.8. The Choose Action Handler Dialog


We choose our previously created 'HelloActionHandler' class and push the OK button. After the selection of the action handler for the action, we can run the test and observe it gives us a green light.

Test Results

Figure 5.9. Test Results


5.3.  Integration Points

The different integration points in a process definition are thoroughly documented in the JBoss jBPM User Guide. Instance nodes can contain many action elements. Each of these will appear in the Action element list of the Actions tab. But each Action also has a properties view of itself. You can navigate to this view by selecting the added Action in the outline view.