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.
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.
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".
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.
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.
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.
After adding the action a tabbed view with three pages will appear.
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.
Clicking on the Search... button brings us to a Choose Class 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.
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.