As promised in my previous post, I’ll show you how easy it is to start using the new jBPM4 API.
I’ll use the mother of all demo’s and implement a process that prints ‘Hello World’ to the screen. One of the core strengths of jBPM has always been the embeddability in several environments (SE, Spring, EJB, etc), so today I’ll show how to create a SE application that uses jBPM as a regular library. I’m going to do the demo with the CR1 release, but the example should be quite protable to the 4.0GA release.
Download the (Maven) example here.
If you want to do it yourself, follow these steps:
- (non-Maven users): in Eclipse, add jbpm.jar the lib folder of the unzipped distribution to the project classpath.
(maven-users): create an empty maven project and add the following dependency to your pom.xml:
1.
<
dependency
>
2.
<
groupId
>org.jbpm.jbpm4</
groupId
>
3.
<
artifactId
>jbpm-jpdl</
artifactId
>
4.
<
version
>4.0.CR1</
version
>
5.
</
dependency
>
- Now we’ll create the ‘logic’ of our process. Create a class called Printer :
01.
package
be.jorambarrez.jbpm4.helloworld;
02.
03.
public
class
Printer {
04.
05.
public
void
printHelloWorld() {
06.
System.out.println(
"<---------------->"
);
07.
System.out.println(
" HELLO WORLD!"
);
08.
System.out.println(
"<---------------->"
);
09.
}
10.
11.
}
- Create the HelloWorld process. The process uses a Java activity here to call the ‘printHelloWorld’ method on our ‘business logic’.
02.
<
start
>
03.
<
transition
to
=
"printHelloWorld"
/>
04.
</
start
>
05.
06.
<
java
class
=
"be.jorambarrez.jbpm4.helloworld.Printer"
method
=
"printHelloWorld"
name
=
"printHelloWorld"
>
07.
<
transition
to
=
"theEnd"
/>
08.
</
java
>
09.
10.
<
end
name
=
"theEnd"
/>
11.
</
process
>
which looks like this if you have installed the GPD:
For JPDL4, one of the main goals is process readability. If you take a look at this process or any of the shipped example processes, you’ll notice that this has worked out quite fine.
- Create a minimal jBPM config (jbpm.cfg.xml)
1.
<
jbpm-configuration
>
2.
<
import
resource
=
"jbpm.default.cfg.xml"
/>
3.
<
import
resource
=
"jbpm.tx.hibernate.cfg.xml"
/>
4.
<
import
resource
=
"jbpm.jpdl.cfg.xml"
/>
5.
</
jbpm-configuration
>
The imports shown here contain a default configuration. If needed, you can remove the imports and configure jBPM for your own environment.
- Create a basic Hibernate config called jbpm.hibernate.cfg.xml(I’m using HSQLDB, but any DB supported by Hibernate will do):
01.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02.
03.
<!DOCTYPE hibernate-configuration PUBLIC
04.
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
06.
07.
<
hibernate-configuration
>
08.
<
session-factory
>
09.
10.
<
property
name
=
"hibernate.dialect"
>org.hibernate.dialect.HSQLDialect</
property
>
11.
<
property
name
=
"hibernate.connection.driver_class"
>org.hsqldb.jdbcDriver</
property
>
12.
<
property
name
=
"hibernate.connection.url"
>jdbc:hsqldb:mem:.</
property
>
13.
<
property
name
=
"hibernate.connection.username"
>sa</
property
>
14.
<
property
name
=
"hibernate.connection.password"
></
property
>
15.
16.
<
property
name
=
"hibernate.format_sql"
>true</
property
>
17.
<
property
name
=
"hibernate.hbm2ddl.auto"
>create-drop</
property
>
18.
19.
<
mapping
resource
=
"jbpm.repository.hbm.xml"
/>
20.
<
mapping
resource
=
"jbpm.execution.hbm.xml"
/>
21.
<
mapping
resource
=
"jbpm.history.hbm.xml"
/>
22.
<
mapping
resource
=
"jbpm.task.hbm.xml"
/>
23.
<
mapping
resource
=
"jbpm.jpdl.hbm.xml"
/>
24.
<
mapping
resource
=
"jbpm.identity.hbm.xml"
/>
25.
26.
</
session-factory
>
27.
</
hibernate-configuration
>
Note: if you are using 4.0GA (instead of CR1), remove the ‘jbpm.jpdl.hbm.xml’ line from the hibernate mapping.
and add the correct driver to your classpath. With Maven and HSQLDB, add this dependency to your pom.xml.
1.
<
dependency
>
2.
<
groupId
>hsqldb</
groupId
>
3.
<
artifactId
>hsqldb</
artifactId
>
4.
<
version
>1.8.0.7</
version
>
5.
</
dependency
>
- Finally, create a main method which constructs the ProcessEngine
1.
ProcessEngine processEngine =
new
Configuration().setResource(
"my.jbpm.cfg.xml"
).buildProcessEngine();
and retrieve the needed services:
1.
RepositoryService repositoryService = processEngine.getRepositoryService();
2.
ExecutionService executionService = processEngine.getExecutionService();
which we can use to deploy our hello world process and start a process instance
1.
repositoryService.createDeployment()
2.
.addResourceFromClasspath(
"hello_world.jpdl.xml"
)
3.
.deploy();
4.
executionService.startProcessInstanceByKey(
"helloWorld"
);
After the Hibernate loading, you’ll see the Hello World message from the Printer class.
That’s it, your very first jBPM4 Hello World process. With this setup, you can start playing with the different activities or change the configuration. Do note how easy it is to embed this code into other environments (Spring, EJB, …) due to the fact that jBPM is ‘just another Java library’. Although the hello world process is extremely simple, all the setup around it (jBPM and Hibernate config) are exactly the same as for any arbitrary complex process.
Stay tuned for more posts about jBPM4!
from: http://www.jorambarrez.be/blog