jBPM is distributed under the terms of the GNU Lesser General Public License (LGPL) and the JBoss End User License Agreement (EULA). See the full LGPL license text and the full End User License Agreement.
The distribution packages can be downloaded from sourceforge
http://sourceforge.net/project/showfiles.php?group_id=70542&package_id=268068The source code for this component can be found in the jBPM SVN repository:
https://anonsvn.jboss.org/repos/jbpm/jbpm4/jBPM requires a JDK (standard java) version 5 or higher.
http://java.sun.com/javase/downloads/index.jspjBPM is an extensible and flexible process engine that can run as a standalone server or embedded in any Java application.
In this user guide, we'll describe the jPDL process language in persistent execution mode. Persistent execution mode means that process definitions, process executions and process history is stored in a relational DB. This is the common way of how jBPM is used.
This user guide explains the supported way on how to use jBPM. The developers guide explains more advanced customization options that are not part of the support offerings.
A process definition is description of the steps in a procedure.
For example, an insurance company could have a loan
process definition that describes the steps of how the company deals
with loan requests.
One process instance represents one particular run of a process definition. For example, the loan request of John Doe last Friday to finance his new boat is represented in one process instance of the loan process definition.
A process instance contains all the runtime state. The most prominent property is the pointer that keeps track of the current activity.
Suppose that wiring the money and archiving can be done in parallel. Then the main process instance will have two child executions to keep track of the state like this:
More general, a process instance is the root of a tree of executions. When a new process instance is started, the process instance is in fact the root execution scope. Only leaf executions can be active.
The motivation to work with a tree structure like this is that
this conceptually remains simple in the case where there is only one path
of execution. The services API doesn't need to make a functional difference
between process instances and executions. Therefore, the API has only
one Execution type to refer to both ProcessInstance
s and
Execution
s.
This chapter describes how to install jBPM in different application environments.
Start the installer on a command line like this:
java -jar jbpm-installer-${version}.jar
The installer has 2 purposes
The lib directory you'll find the jbpm libs and the third party libs. Including all the libraries in the lib directory, is the simplest way to get started. To minimize the number of jars that you need to include, see the developers guide.
TODO: this will contain a description of how to use the installer to install jbpm on tomcat
TODO: this will contain a description of how to use the installer to install jbpm on jboss
jBPM works with your database of choice. That way, jBPM's data tables can be placed next to your application tables in the same database. In case you're working outside an application server you can combine your application updates with jBPM's process updates in a single transaction by using the same JDBC connection.
TODO
In the meantime, see in the examples for a set of example configuration files. Central file is jbpm.cfg.xml. All other files are referenced from that file.
Eclipse is used as the platform to host the jPDL graphical process designer. This section will describe how to obtain Eclipse, install it the GPD.
You'll need Eclipse 3.4.1.
Download the Eclipse IDE for Java Developers (85 MB) or Eclipse IDE for Java EE Developers (163 MB).
The classic version of eclipse will not be sufficient as it does not have an XML editor.
The installation of the GPD uses the Eclipse Software Update
mechanism and is pretty straightforward. There is an archived update
site included in the runtime installation of jBPM when you unzip it
at gpd/jbpm-gpd-site.zip
To add the update site to eclipse:
Help
--> Software Updates
Available Software
Add Site...
Add Site
dialog, click Archive...
gpd/jbpm-gpd-site.zip
OK
will bring you back to the dialog 'Software Updates and Add-ons'jPDL 4 GPD Update Site
that has appearedInstall...
Install
, select Flow Common Feature
and jPDL 4 Feature
Next
Accept
and click Finish
This section shows how to define a user library for your workspace that is a placeholder for the jBPM library as well as its dependencies. If you create a new Java project, it will be sufficient to add this user library to the build path.
Window
--> Preferences
Java
--> Build Path
--> User Libraries
New...
jBPM Libraries
Add JARs...
Open
jBPM Libraries
entryAdd JARs...
againjbpm.jar
file in the root of your jBPM installationOpen
Source attachment
under jbpm.jar
Edit
Source Attachment Configuration
, click External Folder...
src
folder in your jBPM installationChoose
OK
twice to close all the open dialogsIn case you want to edit the process XML sources directly, it is best to specify your schema in the XML catalog. This will give you better code completion while editing the process sources.
Window
--> Preferences
XML
--> XML Catalog
jpdl.xsd
in the src directory of the jBPM installation root.In this section we will import the examples project in the installation in Eclipse
File
--> Import...
General
--> Existing Projects into Workspace
Next
Browse...
to select a root directoryOK
examples
project is automatically found and selectedFinish
You're all set to start playing with the coolest Java process technology!
This chapter will explain how to work with the Graphical Process Designer. After installing the GPD and setting up the examples, you'll see that the jPDL process files will get a special icon. Double clicking such a file in the package view will open up the jPDL process in the GPD.
CRTL+N will open up the wizard selector.
Select jBPM --> jPDL 4 File. Click 'Next >'. Then the 'New jPDL 4 File' wizard opens.
Select the parent directory, enter a file name and click 'Finish'. Voila, you've created your first jPDL process file.
The GPD contains a 'Source' tab with the XML sources. These are directly editable in this tab and the graphical view will reflect the changes when you switch back to the diagram.
You can of course still edit the process XML sources using the built in XML editor. To do this, select the process file, right click and select 'Open with...' --> 'XML editor'
After you've done this, eclipse will remember that and the next time you double click it, eclipse will open your process file in the XML Editor automatically. To open up the process file in the GPD, perform the same Open-with-action, but select 'jBPM jPDL 4 Editor'
Interacting with jBPM occurs through services.
The service interfaces can be obtained from the ProcessEngine
which is build from a Configuration
.
A ProcessEngine
is thread safe and can be stored in a
static member field or even better in JNDI or some other central location.
One ProcessEngine
object can be used by all requests and
threads in an application. Here's how you can obtain a ProcessEngine
The code snippets in this section and the next section about process
deployments are taken from example
org.jbpm.examples.services.ServicesTest
ProcessEngine processEngine = new Configuration() .buildProcessEngine();
The previous code snippet shows how to build a ProcessEngine
from the default configuration file jbpm.cfg.xml
which is
expected in the root of the classpath. If you want to specify another
resource location, use the setResource
method like this:
ProcessEngine processEngine = new Configuration() .setResource("my-own-configuration-file.xml") .buildProcessEngine();
There are other setXxxx
methods that allow to specify
the configuration content as an InputStream
, an
xmlString
, InputSource
,
URL
or File
.
From a ProcessEngine
the following services
can be obtained:
RepositoryService repositoryService = processEngine.getRepositoryService(); ExecutionService executionService = processEngine.getExecutionService(); TaskService taskService = processEngine.getTaskService(); HistoryService historyService = processEngine.getHistoryService(); ManagementService managementService = processEngine.getManagementService();
Process engine objects defined in the configuration can also be retrieved by
type (processEngine.get(Class<T>)
)
or by name (processEngine.get(String)
)
The RepositoryService
groups all methods to manage
the repository of deployments. In this first example, we'll deploy one process
resource from the classpath with the RepositoryService
:
long deploymentDbid = repositoryService.createDeployment() .addResourceFromClasspath("org/jbpm/examples/services/Order.jpdl.xml") .deploy();
Analogue to the addResourceFromClasspath
method above,
the source of the processes definitions XML can be picked up from a file, url, string,
input stream or zip input stream.
Each deployment is composed of a set of named resources. The content
of each resource is a byte array. jPDL process files are recognized by their extension
.jpdl.xml
. Other resource types are task forms and java classes.
A deployment works with a set of named resources and can potentially contain
multiple process descriptions and multiple other artifact types. The jPDL deployer
will recognise process files based on the .jpdl.xml
extension automatically.
During deployment, an id
is assigned to the process
definitions. The id
will have format
{key}-{version}
with a dash between key and version
If key
is not provided, it is generated automatically
based on the name. All non alpha numeric characters in the name will be replaced
by underscores to generate the key.
The same name
can only be associated to one
key
and vice verca.
If version
is not provided, a version
will be automatically be assigned. For version
assignment, the versions of all deployed process definitions with the same name will
be taken into account. The assigned version
will be one higher
then the highest version
number of deployed process definitions
with the same key
. If no process definitions with a similar
key
have been deployed, version number 1 is assigned.
In this first example, we'll supply a name and nothing else.
<process name="Insurance claim"> ... </process>
Let's assume that this is the first time that this process gets deployed. Then it will get the following properties:
Table 4.1. Process properties without key
Property | Value | Source |
---|---|---|
name | Insurance claim | process xml |
key | Insurance_claim | generated |
version | 1 | generated |
id | Insurance_claim:1 | generated |
And as a second example, we'll show how you can get shorter ids by specifying a process key:
<process name="Insurance claim" key="ICL"> ... </process>
Then the process definition properties look like this:
Table 4.2. Process properties with key
Property | Value | Source |
---|---|---|
name | Insurance claim | process xml |
key | ICL | process xml |
version | 1 | generated |
id | ICL:1 | generated |
Deleting a deployment will remove it from the DB.
repositoryService.deleteDeployment(deploymentDbid);
That method will throw an exception when there are still active process executions for process definitions in that deployment.
If you want to cascade deletion of a deployment to all
the process instances of all the process definitions, use
deleteDeploymentCascade
.
Simplest and most common way to start a new process instance for a process definition is like this:
executionService.startProcessInstanceByKey("ICL");
In this case, the service method will first look up the latest version of
the processes with key ICL
. Then a new
process instance is started in that latest process definition.
When a new version of the insurance claim process
is deployed, all invocations of startProcessInstanceByKey
will automatically switch to the newly deployed version.
If instead you want to start a new process instance in a very specific version, you can use the id of the process definition like this:
executionService.startProcessInstanceById("ICL-1");
A new process instance can optionally be given a key. A key is a user defined reference to the execution. A key must be unique within the scope of all versions of a process definition. Typically it is easy to find such a key in the domain of the business process. For example, an order id or an insurance claim number.
executionService.startProcessInstanceByKey("ICL", "CL92837");
The key is used to create the id of the process instance.
The format used is {process-key}.{execution-id}
.
With a dot between process-key and execution-id.
So execution created in the previous code snippet will have id
ICL.CL92837
.
If no user defined key is provided, the DB primary key is taken as the key. In that case, the id can be retrieved like this:
Execution execution = executionService.startProcessInstanceByKey("ICL"); String executionId = execution.getId();
We recommend the use of a user defined keys. Typically in your application code, you'll have the key available. By providing a user defined key, you can then compose the id of the execution, rather then performing a query based on the process variables.
A map of named parameter objects can be provided when starting a new process instance. These parameters will be set as variables on the process instance between creation and start of the process instance.
Map<String,Object> variables = new HashMap<String,Object>(); variables.put("customer", "John Doe"); variables.put("type", "Accident"); variables.put("amount", new Float(763.74)); executionService.startProcessInstanceByKey("ICL", variables);
A process definition describes what must be done in terms of activities. Each activity in a process is either to be performed by the process system or by an external participant. When an activity is to be performed by an external participant, then the execution must wait until the external participant notifies the process system that the activity is completed. So an execution is either executing or waiting on an external participant. Typically, you'll see that the processes are mostly waiting for external participations. Especially humans tend to be slow :-) The time consumed by the process system between two wait states is typically very small.
A state
is the basic activity that represents
something has to be done by an external participant and the execution
must wait until a signal (aka external trigger) is given.
When an execution is in a wait state, it can be given an external trigger
with one of the signal methods. The recommended way to reference an execution
is by using the process definition and execution key. In the next code snippet,
ICL
refers to the process definition key and 82436
refers to the execution key.
executionService.signalExecutionByKey("ICL", "82436");
Alternatively, the execution that must be signaled can be referenced
by a unique execution id. In the next snippet, ICL.82436
refers to the executionId.
executionService.signalExecutionById("ICL.82436");
Optionally some data can be passed along with a signal: a signalName
and parameters
. How the signalName gets used depends on
the execution's current activity. The parameters get stored as process variables.
Map<String,Object> parameters = new HashMap<String,Object>(); parameters.put("quality", "a+"); parameters.put("target", "profit"); executionService.signalExecutionById("ICL/82436", "Accept", parameters);
The primary purpose of the TaskService is to provide access to
task lists. The code sample will show how to get the task list for
the user with id johndoe
.
List<Task> taskList = taskService.findAssignedTasks("johndoe");
Typically tasks are associated with a form and displayed in some user interface. The form needs to be able to read and write data related to the task.
long taskDbid = task.getDbid(); Set<String> variableNames = taskService.getVariableNames(taskDbid); variables = taskService.getVariables(taskDbid, variableNames); variables = new HashMap<String, Object>(); variables.put("category", "small"); variables.put("lires", 923874893); taskService.setVariables(taskDbid, variables);
and complete tasks
taskService.completeTask(taskDbid);
Tasks can also be offered to a set of candidates. Candidates can be users or groups. Users can take tasks for which they are a candidate. Taking a task means that this user will be set as the assignee. After that, other users will be blocked from taking the task.
People should not work on a task unless they are assigned to that task. The user interface should display forms and allow users to complete tasks if they are assigned to it. For unassigned tasks for which the user is a candidate, the only action that should be exposed is 'take'. Since taking a task boiles down to setting the assignee to the current user, we didn't introduce a separate method for that.
More on tasks in ???
This chapter will explain the jPDL file format for describing process definitions. The schemadocs can also serve as a quick reference for this information.
An example jPDL process file looks like this:
<?xml version="1.0" encoding="UTF-8"?> <process name="Purchase order" xmlns="http://jbpm.org/4/jpdl"> <start> <transition to="Verify supplier" /> </start> <state name="Verify supplier"> <transition name="Supplier ok" to="Check supplier data" /> <transition name="Supplier not ok" to="Error" /> </state> <exclusive name="Check supplier data"> <transition name="nok" to="Error" /> <transition name="ok" to="Completed" /> </exclusive> <end name="Completed" /> <end name="Error" /> </process>
The top level element representing one process definition.
Table 5.1. process
attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
name | any text | required | name or label of the process used to display to the process name in user interactions. | |
key | alpha numeric characters and underscores | if omitted, the key will be generated based on the name by replacing all non-alpha-numeric characters with underscores | optional | identification to distinct different process definitions. Multiple versions of a process with the same key can be deployed. The key:name combination must remain exactly the same for all deployed versions. |
version | integer | one higher then highest version number starting with 1 if no other process is deployed with the same name/key. | optional | version number of this process |
Table 5.2. process
elements:
Element | Multiplicity | Description |
---|---|---|
description | 0..1 | description text |
activities | 1..* | a list of any activity type can be placed here. At least
one start activity must be present.
|
(BPMN note: when we mention activities here, we are not only refering to BPMN activities, but also to BPMN events and BPMN gateways.)
Indicates where an execution for this process starts. Typically there is exactly one start activity in a process. A process has to have at least one start activity. A start activity must have exactly one outgoing transition and that transition is taken when a process execution starts.
Known limitation: for now, a process can not have more then
one start
.
Table 5.3. start
attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
name | any text | optional | name of the activity. Since a start activity cannot have incoming transitions, the name is optional. |
A wait state. Process execution will wait until an external trigger is
provided through the API. Apart from the
common activity content, state
doesn't have any extra
attributes or elements.
Let's look at an example which shows states connected with transitions as a sequence
<process name="StateSequence" xmlns="http://jbpm.org/4/jpdl"> <start> <transition to="a" /> </start> <state name="a"> <transition to="b" /> </state> <state name="b"> <transition to="c" /> </state> <state name="c" /> </process>
After you start an execution like this:
Execution execution = executionService.startProcessInstanceByKey("StateSequence");
the created process instance will be positioned in
state a
. Providing an external trigger can
be done with the signalExecution
methods.
String executionId = execution.getId(); execution = executionService.signalExecutionById(executionId);
In this second example with states, we'll show how you can use a
state
can be used to feed in an external choice of
the path to take.
<process name="StateChoice" xmlns="http://jbpm.org/4/jpdl"> <start> <transition to="wait for response" /> </start> <state name="wait for response"> <transition name="accept" to="submit document" /> <transition name="reject" to="try again" /> </state> <state name="submit document" /> <state name="try again" /> </process>
Let's start a new process instance for this process definition:
Execution execution = executionService.startProcessInstanceByKey("StateSequence");
Now, the execution is arrived in the wait for response
.
The execution will wait there until an external trigger is given. In case
a state
has multiple outgoing transitions, the signalName given
in the external trigger will be matched against the name of the outgoing transition
to take. So when we provide signalName accept
like this:
executionService.signalExecutionById(executionId, "accept");
Then the execution will continue over the outgoing transition named
accept
. Analogue, when signalName reject
is given in the signalExecutionXxx methods, the execution will continue over
the outgoing transition named reject.
Takes one path of many alternatives. Also known as a decision. An exclusive activity has multiple outgoing transitions and when an execution arrives in an exclusive activity, an automatic evaluation will decide which outgoing transition is taken.
An exclusive activity should be configured in one of the three following ways:
An exclusive with conditions on the transitions evaluates the condition in each transition. The first transition for which the nested condition expression resolves to true or which does not have a condition is taken.
Table 5.5. exclusive.transition.condition
attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
expr | expression | required | script that will be evaluated in the specified expression language. | |
lang | expression language | the default-expression-language taken from the script-manager configuration | optional | the language in which expr is
to be evaluated.
|
Example:
<process name="ExclusiveConditions" > <start> <transition to="evaluate document" /> </start> <exclusive name="evaluate document"> <transition to="submit document"> <condition expr="#{content=="good"}" /> </transition> <transition to="try again"> <condition expr="#{content=="not so good"}" /> </transition> <transition to="give up" /> </exclusive> <state name="submit document" /> <state name="try again" /> <state name="give up" /> </process>
An exclusive expression evaluates to a String representing the name of an outgoing transition.
Table 5.6. exclusive
attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
expr | expression | required | script that will be evaluated in the specified expression language. | |
lang | expression language | the default-expression-language taken from the script-manager configuration | optional | the language in which expr is
to be evaluated.
|
Example:
<process name="Poolcar">
<start>
<transition to="How far?" />
</start>
<exclusive name="How far?" expr="#{distance}">
<transition name="far" to="Big car" />
<transition name="nearby" to="Small car" />
</exclusive>
<state name="Big car" />
<state name="Small car" />
</process>
When you start an new process instance like this
Map<String, Object> variables = new HashMap<String, Object>(); variables.put("distance", "far"); Execution execution = executionService.startProcessInstanceByKey("Poolcar", variables);
then the new execution will go to activity Big car
.
An exclusive handler is a java class that implements the
ExclusiveHandler
interface. The exclusive handler
will be responsible for selecting the name of the outgoing transition.
public interface ExclusiveHandler { String select(OpenExecution execution); }
The handler is specified as a sub element of the exclusive
Table 5.7. exclusive.handler
attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
class | classname | required | fully qualified classname of the handler implementation class. |
Here's an example process of an exclusive using an ExclusiveHandler:
<process name="ExclusiveHandler"> <start> <transition to="evaluate document" /> </start> <exclusive name="evaluate document"> <handler class="org.jbpm.examples.exclusive.handler.ContentEvaluation" /> <transition name="good" to="submit document" /> <transition name="bad" to="try again" /> <transition name="ugly" to="give up" /> </exclusive> <state name="submit document" /> <state name="try again" /> <state name="give up" /> </process>
The ContentEvaluation class looks like this
public class ContentEvaluation implements ExclusiveHandler { public String select(OpenExecution execution) { String content = (String) execution.getVariable("content"); if (content.equals("you're great")) { return "good"; } if (content.equals("you gotta improve")) { return "bad"; } return "ugly"; } }
Now, when we start a process instance and supply value
you're great
for variable content, then the
ContentEvaluation will return String good
and
the process instance will arrive in activity Submit document
.
With the fork
and join
activities,
concurrent paths of executions can be modeled.
For example:
<process name="ConcurrencyGraphBased" xmlns="http://jbpm.org/4/jpdl"> <start> <transition to="fork"/> </start> <fork name="fork"> <transition to="send invoice" /> <transition to="load truck"/> <transition to="print shipping documents" /> </fork> <state name="send invoice" > <transition to="final join" /> </state> <state name="load truck" > <transition to="shipping join" /> </state> <state name="print shipping documents"> <transition to="shipping join" /> </state> <join name="shipping join" > <transition to="drive truck to destination" /> </join> <state name="drive truck to destination" > <transition to="final join" /> </state> <join name="final join" > <transition to="end"/> </join> <end name="end" /> </process>
Ends the execution.
By default, an end activity will end the complete process instance. In case multiple concurrent executions are still active within the same process instance, all of them will be ended.
<process name="EndProcessInstance" xmlns="http://jbpm.org/4/jpdl"> <start> <transition to="end" /> </start> <end name="end" /> </process>
When a new process instance is created, it immediately ends.
Only the execution that arrives in the
end activity will be ended and other concurrent executions
should be left active. To get this behaviour, set
attribute ends="execution"
Table 5.8. end
execution attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
ends | {processinstance|execution} | processinstance | optional | specifies if the whole process instance should be ended or just the path of execution that arrives in the end activity. |
A process can have multiple end events. This can be handy to indicate different outcomes of a process instance. For example
<process name="EndMultiple" xmlns="http://;jbpm.org/4/jpdl"> <start> <transition to="get return code" /> <start> <state name="get return code"> <transition name="200" to="ok"/> <transition name="400" to="bad request"/> <transition name="500" to="internal server error"/> </state> <end name="ok"/> <end name="bad request"/> <end name="internal server error"/> </process>
Now if we would start an execution and signal it to move out of the get return code
wait state with the
following code, the execution would end with the bad request
end event.
Execution execution = executionService.startProcessInstanceByKey("EndMultiple"); String executionId = execution.getId(); execution = executionService.signalExecutionById(executionId, "400");
Likewise, using the value 200
or 500
would cause the execution
to end with the ok
or with the internal server error
end events
respectively.
An execution can also end with different states. It is another way to specify the outcome of a process.
It is indicated by the state
attribute of the end event or by the end-cancel
and end-error
shortcut notations.
Table 5.9. end
execution attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
state | String | optional | the state assigned to the execution. |
Take for example the following process.
<process name="EndState" xmlns="http://jbpm.org/4/jpdl"> <start> <transition to="get return code"/> </start> <state name="get return code"> <transition name="200" to="ok"/> <transition name="400" to="bad request" /> <transition name="500" to="internal server error"/> </state> <end name="ok" state="completed"/> <end-cancel name="bad request"/> <end-error name="internal server error"/> </process>
This time, if we would start an execution and signal it to move out of the get return code
wait state with the
following code, the execution would end with the cancel
state.
Execution execution = executionService.startProcessInstanceByKey("EndState"); String executionId = execution.getId(); execution = executionService.signalExecutionById(executionId, "400");
Similarly as above, using the value 200
or 500
would cause the execution
to end with the completed
or with the error
states
respectively.
The Java task. A process execution will execute the method of the class that is configured in this activity.
Table 5.10. java
attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
class | classname | required | The fully qualified classname. | |
method | methodname | required | The name of the method to invoke | |
var | variablename | optional | The name of the variable in which the return value should be stored. |
Table 5.11. java
elements:
Element | Multiplicity | Description |
---|---|---|
field | 0..* | describes a configuration value to inject in a memberfield before the method is invoked. |
arg | 0..* | method parameters |
Consider the following example.
<process name="Java" xmlns="http://jbpm.org/4/jpdl"> <start> <transition to="invoke java method" /> </start> <java name="invoke java method" class="org.jbpm.examples.java.JohnDoe" method="hello" var="answer"> <field name="state"><string value="fine"/></field> <field name="session"><env type="org.hibernate.Session"/></field> <arg><string value="Hi, how are you?"/></arg> <transition to="wait" /> </java> <state name="wait"> </process>
The java task specifies that during its execution an instance of the class org.jbpm.examples.java.JohnDoe
will be instantiated and the method hello
of this class will be invoked on the resulting object. The variable named
answer
will contain the result of the invocation. Let's look at the class JohnDoe
below.
package org.jbpm.examples.java; import org.hibernate.Session; public class JohnDoe { String state; Session session; public String hello(String msg) { if ( (msg.indexOf("how are you?")!=-1) && (session.isOpen()) ) { return "I'm "+state+", thank you."; } return null; } }
The class above reveals that it contains two fields named state
and session
and that the method hello
accepts one argument. During the execution the values specified in the
field
and arg
configuration elements will be used. The expected result of creating
a process instance is that the process variable answer
contains the string
I'm fine, thank you.
.
Creates a task for a person in the task component.
A simple task that will be assigned to a specific user
Table 5.12. task
attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
assignee | expression | optional | userId referring to the person that is responsible for completing this task. |
<process name="TaskAssignee">
<start>
<transition to="review" />
</start>
<task name="review"
assignee="#{order.owner}">
<transition to="wait" />
</task>
<state name="wait" />
</process>
This process shows 2 aspects of task assignment. First, that the
attribute assignee
is used to indicate the user that is
responsible for completing the task. The assignee is a String property
of a task and refers to a user.
Secondly, this attribute is by default evaluated as an expression.
In this case the task is assigned to #{order.owner}
.
Which means that first an object is searched for with name order. One of
the places where this object is looked up is the process variables
associated to the task. Then the getOwner()
getter
will be used to get the userId that references the user that is
responsible for completing this task.
Here's the Order class used in our example:
public class Order implements Serializable { String owner; public Order(String owner) { this.owner = owner; } public String getOwner() { return owner; } public void setOwner(String owner) { this.owner = owner; } }
Next a new process instance is created with an order as a process variable.
Map<String, Object> variables = new HashMap<String, Object>(); variables.put("order", new Order("johndoe")); Execution execution = executionService .startProcessInstanceByKey("TaskAssignee", variables);
Then the task list for johndoe
can be obtained like this.
List<Task> taskList = taskService.findAssignedTasks("johndoe");
Note that it is also possible to put plain text like
assignee="johndoe"
. In that case
the task will be assigned to johndoe.
A task that will be offered to a group of users. One of the users should then take the task in order to complete it.
Table 5.13. task
attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
candidate-groups | expression | optional | resolves to a comma separated list of groupIds. All the people in the groups will be candidates for this task. | |
candidate-users | expression | optional | resolves to a comma separated list of userIds. All the users will be candidates for this task. |
Here's an example process using task candidates:
<process name="TaskCandidates">
<start>
<transition to="review" />
</start>
<task name="review"
candidate-groups="sales-dept">
<transition to="wait" />
</task>
<state name="wait"/>
</process>
After starting, a task will be created. The task will not show up in anyone's personal task list. Following task lists will be empty.
taskService.getAssignedTasks("johndoe"); taskService.getAssignedTasks("joesmoe");
But the task will show up in the takable task list of all members of the sales-dept
group.
The in our example, the sales-dept
has two members: johndoe and joesmoe
identityService.createGroup("sales-dept"); identityService.createUser("johndoe", "johndoe", "John", "Doe"); identityService.createMembership("johndoe", "sales-dept"); identityService.createUser("joesmoe", "joesmoe", "Joe", "Smoe"); identityService.createMembership("joesmoe", "sales-dept");
So after the process is created, the task will appear in both the takable tasks for users johndoe and joesmoe
taskService.findTakableTasks("johndoe"); taskService.findTakableTasks("joesmoe");
Candidates must take a task before they can work on it. This will prevent that two candides start working on the same task. The user interface must only offer the action 'Take' for the tasks in the takable task list.
taskService.takeTask(task.getDbid(), "johndoe");
When a user takes a task, the assignee of that task will be set to the given user. The task will disappear from all the candidate's takable task list and it will appear in the user's assigned tasks.
Users are only allowed to work on tasks in their personal task list. This should be enforced by the user interface.
Similarly, the attribute candidate-users
can be used that
resolves to a comma separated list of userIds. The candidate-users
attribute can be used in combination with other assignment options.
An AssignmentHandler
can be used to calculate the
assignee and the candidates for a task programmatically.
public interface AssignmentHandler extends Serializable {
/** sets the actorId and candidates for the given assignable. */
void assign(Assignable assignable, OpenExecution execution) throws Exception;
}
Assignable
is a common interface for Tasks and
Swimlanes. So AssignmentHandlers can be used for tasks as well as swimlanes
(see later).
assignment-handler
is a sub element of the task element.
It specifies a user code object. So the attributes and elements of assignment-handler
are documented in Section 5.3, “User code”
Let's look at the task assignment example process.
<process name="TaskAssignmentHandler" xmlns="http://jbpm.org/4/jpdl">
<start g="20,20,48,48">
<transition to="review" />
</start>
<task name="review" g="96,16,127,52">
<assignment-handler class="org.jbpm.examples.task.assignmenthandler.AssignTask">
<field name="assignee">
<string value="johndoe" />
</field>
</assignment-handler>
<transition to="wait" />
</task>
<state name="wait" g="255,16,88,52" />
</process>
The referenced class AssignTask
looks like this:
public class AssignTask implements AssignmentHandler { String assignee; public void assign(Assignable assignable, OpenExecution execution) { assignable.setAssignee(assignee); } }
Please note that potentially, AssignmentHandler implementations can use the process variables and any other Java API to access resources like your application database to calculate the assignee and candidate users and groups.
Starting a new process instance of the TaskAssignmentHandler
process will immediately bring the new execution to the task activity. A new
review
task is created and at that point, the AssignTask
assignment handler is called. That will set johndoe
as
the assignee. So John Doe will find the task in his personal task list.
Multiple tasks in a process should be assigned to the same user or candidates. Multiple tasks in a process can be associated to a single swimlane. The process instance will remember the candidates and user that performed the first task in the swimlane. And subsequent tasks in the same swimlane will be assigned to those user and candidates.
A swimlane can also be considered as a process role. In some cases, this might boil down to authorization roles in the identity component. But bare in mind that it is not always the same thing.
Table 5.14. task
attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
swimlane | swimlane (string) | optional | refers to a swimlane that is declared in the process |
Swimlanes can be declared inside a process element:
Table 5.15. swimlane
attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
name | swimlane (string) | required | Name for this swimlane. This is the name that will be referenced by task swimlane attributes. | |
assignee | expression | optional | userId referring to the person that is responsible for completing this task. | |
candidate-groups | expression | optional | resolves to a comma separated list of groupIds. All the people in the groups will be candidates for this the tasks in this swimlane. | |
candidate-users | expression | optional | resolves to a comma separated list of userIds. All the users will be candidates for the tasks in this swimlane. |
The task swimlane example has the following process file :
<process name="TaskSwimlane" xmlns="http://jbpm.org/4/jpdl"> <swimlane name="sales representative" candidate-groups="sales-dept" /> <start> <transition to="enter order data" /> </start> <task name="enter order data" swimlane="sales representative"> <transition to="calculate quote"/> </task> <task name="calculate quote" swimlane="sales representative"> </task> </process>
In this example we create the following information in the identity component:
identityService.createGroup("sales-dept"); identityService.createUser("johndoe", "johndoe", "John", "Doe"); identityService.createMembership("johndoe", "sales-dept");
After starting a new process instance, user johndoe
will
be a candidate for task enter order data
. Again like in the
previous task candidates example, John Doe can now take this task like this:
taskService.takeTask(taskDbid, "johndoe");
Taking the task will make Litjohndoe
the assignee for
the task. And since this task is coupled to the swimlane
sales representative
, assignee johndoe
will
also be propagated as the assignee in the swimlane.
Next, John Doe can complete the task like this:
taskService.completeTask(taskDbid);
Completing the task will bring the process execution to the
next task, which is calculate quote
. Also
this task is linked to the swimlane. Therefore, the task will be
assigned to johndoe
. Also the candidate users
and candidate groups of the initial assignment will be copied from
the swimlane to the task. This is relevant in case user johndoe
would release the task and offer it back to the other candidates.
Tasks can see and update process variables. Later tasks will have the option to declare task-local process variables. Task variables are an important part of the task forms. Task forms typically show data that comes from the task and the process instance. Then input from the user is translated in setting task variables.
Getting task variables can be done like this:
List<Task> taskList = taskService.findAssignedTasks("johndoe"); Task task = taskList.get(0); long taskDbid = task.getDbid(); Set<String> variableNames = taskService.getVariableNames(taskDbid); Map<String, Object> variables = taskService.getVariables(taskDbid, variableNames);
And setting task variables can be done like this:
variables = new HashMap<String, Object>(); variables.put("category", "small"); variables.put("lires", 923874893); taskService.setVariables(taskDbid, variables);
A script activity evaluates a script. Scripts can be specified in any language for which there is a JSR-223 compliant scripting engine. Configuration of scripting engines is explained below.
There are 2 ways of specifying a script:
The script is provided with the expr
attribute.
This is for short expressions that are easier expressed in an attribute
then in a text element. If no lang
is specified,
the default-expression-language is used.
Table 5.16. script
expression attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
expr | text | required | the expression text to evaluate. | |
lang | scripting language name as defined in Chapter 7, Scripting | the default expression language as defined in Chapter 7, Scripting | optional | the language in which the expression is specified. |
var | variablename | optional | name of the variable in which the return value should be stored. |
In the next example, we'll see how a script activity with an expression and how the result is stored in a variable.
<process name="ScriptExpression" xmlns="http://jbpm.org/4/jpdl"> <start> <transition to="invoke script" /> </start> <script name="invoke script" expr="Send packet to #{person.address}" var="text"> <transition to="wait" /> </script> <state name="wait"/> </process>
This example uses a Person
class that looks like this.
public class Person implements Serializable { String address; public Person(String address) { this.address = address; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
When starting a process instance for this process, we supply a person
with a given address property as variable person
.
Map<String, Object> variables = new HashMap<String, Object>(); variables.put("person", new Person("Honolulu")); executionService.startProcessInstanceByKey("ScriptText", variables);
After the execution of the script activity, variable text
will contain 'Send packet to Honolulu'.
The second way of specifying a script is with a text
element.
This is convenient when the script text spans multiple lines.
Table 5.17. script
text attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
lang | scripting language name as defined in Chapter 7, Scripting | the default scripting language as defined in Chapter 7, Scripting | optional | the language in which the script is specified. |
var | variablename | optional | name of the variable in which the return value should be stored. |
For example
<process name="ScriptText" xmlns="http://jbpm.org/4/jpdl"> <start> <transition to="invoke script" /> </start> <script name="invoke script" var="text"> <text> Send packet to #{person.address} </text> <transition to="wait" /> </script> <state name="wait"/> </process>
Execution of this process is exactly the same as with the script expression above.
An esb
activity sends a message to a service over the ESB.
The attributes category
and service
identify the
service in the esb repository. The message is composed with the
part
elements.
Table 5.19. esb
attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
category | string | required | the esb category where the service is defined. | |
service | string | required | the esb name of the service |
For example
<process name="Esb" xmlns="http://jbpm.org/4/jpdl"> <start > <transition to="invoke esb service" /> </start> <esb name="invoke esb service" category="orderProcessing" service="bookSold"> <part name="bookTitle" expr="#{title}" /> <part name="goal"> <string value="deliver asap" /> </part> <transition to="wait" /> </esb> <state name="wait" /> </process>
When a new process is started, a message will be sent to the esb
service bookSold
in category orderProcessing
.
The message will have 2 parts: one named bookTitle
containing
the title process variable. And one named goal
which
contains the text 'deliver asap'.
With thehql
activity, a HQL query can be performed
on the database and the result is stored in a process variable.
Table 5.21. hql
attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
var | variablename | required | the name of the variable in which the result is stored. | |
unique | {true, false} | false | optional | a value of true means that the result from the hibernate
query should be obtained with method uniqueResult() .
The default is false and in that case the list()
method will be used to get the result.
|
Table 5.22. hql
elements:
Element | Multiplicity | Description |
---|---|---|
query | 1 | The HQL query. |
parameter | 0..* | The query parameters |
For example:
<process name="Hql" xmlns="http://jbpm.org/4/jpdl"> <start> <transition to="get process names" /> </start> <hql name="get process names" var="activities with o"> <query> select activity.name from org.jbpm.pvm.internal.model.ActivityImpl as activity where activity.name like :activityName </query> <parameters> <string name="activityName" value="%o%" /> </parameters> <transition to="count activities" /> </hql> <hql name="count activities" var="activities" unique="true"> <query> select count(*) from org.jbpm.pvm.internal.model.ActivityImpl </query> <transition to="wait" /> </hql> <state name="wait"/> </process>
The sql
activity is exactly the same as the
hql activity, with the only difference that
session.createSQLQuery(...)
is used.
Unless specified otherwise above, all activities also include this content model:
Table 5.23. Common activity attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
name | any text | required | name of the activity |
Table 5.24. Common activity elements:
Element | Multiplicity | Description |
---|---|---|
transition | 0..* | the outgoing transitions |
Various elements in the jPDL process language refer to a an
object on which an interface method will be invoked. See for example
Section 5.2.7.3, “task
assignment handler”. This section describes the
attributes and sub elements of those type of user code objects.
Table 5.25. attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
class | classname | required | The fully qualified classname. |
Table 5.26. sub elements:
Element | Multiplicity | Description |
---|---|---|
field | 0..* | describes a configuration value to be injected directly in a memberfield before this user class is used. |
property | 0..* | describes a configuration value to injected through a setter method before this user object is used. |
Table 5.27. field
attributes:
Attribute | Type | Default | Required? | Description |
---|---|---|---|---|
name | string | required | the name of the field |
Scripting in jBPM is based on JSR 223: Scripting for the JavaTM Platform. Scripting engines can be configured like this:
<script-manager default-expression-language="juel" default-script-language="juel" read-contexts="execution, environment, process-engine" write-context=""> <script-language name="juel" factory="com.sun.script.juel.JuelScriptEngineFactory" /> </script-manager>
A jPDL process definition can contain scripts and expressions. All of the configured scripting engines can be used in each situation. But scripts and expressions each have their own default.
The default jBPM identity component is based on JBoss IDM. Configuration is like this:
<jbpm-configuration xmlns="http://jbpm.org/xsd/cfg"> <process-engine-context> ... <identity-service /> ... </process-engine-context> <transaction-context> ... <identity-session realm="realm://jbpm-identity" /> </transaction-context> </jbpm-configuration>
To replace the identity component, keep the identity-service declaration, implement org.jbpm.session.IdentitySession and configure your identity session in the transaction context like this:
<jbpm-configuration xmlns="http://jbpm.org/xsd/cfg"> ... <transaction-context> ... <object class="your.package.YourIdentitySession" /> </transaction-context> </jbpm-configuration>
jBPM provides integration with JBoss 4.2.x and JBoss 5.0.0.GA. As part of the installation, the ProcessEngine and a deployer for jBPM archives will be installed as a JBoss service.
After a successful installation you should see that the ProcessEngine has been started and bound to JNDI:
[...] 14:12:09,301 INFO [JBPMService] jBPM 4 - Integration JBoss 4 14:12:09,301 INFO [JBPMService] 4.0.0.Beta1 14:12:09,301 INFO [JBPMService] ProcessEngine bound to: java:/ProcessEngine
When jBPM is deployed on a JBoss instance, process deployments are treated like any other deployment artifact (i.e. *.war, *.ear) and processed by the JBPMDeployer. In order to deploy a process archive simply create a *.jpdl archive (zip file) that contains the process definition (*.jpdl.xml) and all required resources to execute the process (i.e. classes, property files):
Bonanova:Desktop hbraun$ jar -tf OrderProcess.jpdl META-INF/MANIFEST.MF OrderProcess.jpdl.xml org/mycompany/order/*.class
In order to deploy a process archive simply copy it to $JBOSS_HOME/server/<config>/deploy:
(1) cp OrderProcess.jpdl $JBOSS_HOME/server/default/deploy (2) less $JBOSS_HOME/server/default/log [...] 2009-04-08 14:12:21,947 INFO [org.jbpm.integration.jboss4.JBPMDeployer] Deploy file:/Users/hbraun/dev/prj/jboss/tags/JBoss_4_2_2_GA /build/output/jboss-4.2.2.GA/server/default/deploy/OrderProcess.jpdl
In order to remove a process simply remove the process archive from the deploy directory.
TBD: A prelimenary explanation cn be found here
As described above the ProcessEngine will be installed as JBoss service and bound to JNDI. This means that any EE component (i.e. servlet, ejb) can access it doing a JNDI lookup:
private ProcessEngine processEngine; [...] try { InitialContext ctx = new InitialContext(); this.processEngine = (ProcessEngine)ctx.lookup("java:/ProcessEngine"); } catch (Exception e) { throw new RuntimeException("Failed to lookup process engine"); }
Once you obtained an instance of the ProcessEngine you can invoke on it as described in chapter services
UserTransaction tx = (UserTransaction)ctx.lookup("UserTransaction"); (1) Environment env = ((EnvironmentFactory)processEngine).openEnvironment(); try { ExecutionService execService = (ExecutionService) this.processEngine.get(ExecutionService.class); // begin transaction tx.begin(); // invoke on process engine executionService.signalExecutionById("ICL.82436"); // commit transaction tx.commit(); } catch (Exception e) { if(tx!=null) { try { tx.rollback(); } catch (SystemException e1) {} } throw new RuntimeException("...", e); } finally { env.close(); }
(1) Wrapping the call in a UserTransaction is not necessary if the invocation comes a CMT component, i.e. an EJB.