Any project, especially open source projects, rise or fall with proper documentation. Following post serves a dual purpose, it’s a blog post (duh) but also a first draft for the devguide documentation of jBPM4. Things might sound a little weird, but that’s the reason.
Design
Transaction Management
A typical Spring application defines transaction management and ORM definition. It follows following scheme:
Applications are accessed from the web tier and enter the transactional boundary by invoking service beans. These service beans will access the jBPM services. Spring users expect that all these operations run in a single transaction, which as we’ll see makes testing a lot easier.
In reality, we replace the standard-transaction-interceptor by a spring-transaction interceptor. Instead of starting and committing transactions, we want to reuse any existing transactions (which was already started by Spring).
The same is true for the hibernate session, which will be available through Spring.
Spring Context
JBPM4 comes with multiple contexts, which contains beans. With the SpringContext, we add the Spring Application Context to this set of contexts.
Configuration
Replace the standard-transaction-interceptor with the spring-transaction-interceptor.
The hibernate session needs the attribute current=”true”. This forces jBPM to search for the current session, which will be provided by Spring.
For the Spring context to be known, we created a SpringConfiguration. This extends the JbpmConfiguration but will add itself as a context. The single constructor take the location of the jBPM configuration.
<bean id="jbpmConfiguration" class=”org.jbpm.pvm.internal.cfg.SpringConfiguration”> <constructor-arg value="be/inze/spring/demo/jbpm.cfg.xml" /> </bean>
The services can also be defined in the Spring applicationContext, as following:
<bean id="processEngine" factory-bean="jbpmConfiguration" factory-method="buildProcessEngine" /> <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" /> <bean id="executionService" factory-bean="processEngine" factory-method="getExecutionService" />
For accessing Spring beans from a process, we need to register the Spring applicationContext. This can be done as following:
<script-manager default-expression-language="juel" default-script-language="juel" read-contexts="execution, environment, process-engine, spring" write-context=""> <script-language name="juel" factory="org.jbpm.pvm.internal.script.JuelScriptEngineFactory" /> </script-manager>
Use
In jBPM4 most development will happen through the client API aka the services. Simple inject these beans into your transactional service methods and start using them.
Invoking Spring beans from a process can be done as following:
<java name="echo" expr="#{echoService}" method="sayHello" >
<transition name="to accept" to="join1"/>
</java>The scripting engine will look into all contexts from the bean named echoService. If you configured the ScriptManager as above, Spring will be the last context to search for. A good practice is to use unique names for the beans.
Testing
With the AbstractTransactionalJbpmTestCase we can test the process in isolation. Extending from AbstractTransactionalDataSourceSpringContextTests we can test like we test our DAO’s.
Any feedback is appreciated.
KR,
Andries


Twitter
LinkedIn
SlideShare
RSS
Hi Andries,
you have forgot the class for jbpmConfiguration bean in your configuration sniplet: <bean id=”jbpmConfiguration” class=”org.jbpm.pvm.internal.cfg.SpringConfiguration”>
How bizar, I could have sworn that I copy pasted it!
Thanks, I’ve edited the post.
HI, what file are you going to insert the snippet below?
Thanks.
sorry i forgot to insert the snippet
”
“
< script-manager …..
jbpm.cfg.xml (check the source code at
http://jbpm4-spring-demo.googlecode.com/svn/trunk/ )
thanks i saw it.
really good stuff
This is great. Would you made support Spring 2.5 or higher in the future?
I have to downgrade my existing webapp to spring 2.0.8 to get this integration works.
It fails on 2.5?
It works well for me on Spring 2.5
hi, i read the Spring Integration with JBPM4 slide 20 regarding Spring Bean Activity, my question is, where is it and how you do it? thank you.
Spring bean activity is the old way.
A better way is using the
Check the demo
ok Andries, thank you.
Hi,
I am attempting to integrate jBPM4 in our existing application that already use hibernate and have already all the hibernate config defined.
I would like to pass our config to jBPM4 and I didn’t find any doc to achieve simply this.
I wonder if the way you integrated Spring with jBPM4 could also be the right way for me.
Unfortunately, I’m not as skilled as you concerning jBPM and especially its configuration.
First, do you think my problem can solved in the same way ?
Could you explain (or give me a link where I can find explanation ) what are the transaction-interceptor , tag and how to use/extends all the jBPM4 configuration stuffs
Thanks a lot for your time.
VG
Take a look at:
http://docs.jboss.com/jbpm/v4.0/devguide/html_single/#springIntegration
Normally if you have any other questions, i’d be happy to add it to the documentation.
Thanks Andries for your reply and the link,
I noticed I forgot a little precision: Our application does not use Spring !!
I think I have to make my own Context the same way you make one for Spring but I don’t know how to do this.
Very easy. Check http://anonsvn.jboss.org/repos/jbpm/jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/env/SpringContext.java and http://anonsvn.jboss.org/repos/jbpm/jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cfg/SpringConfiguration.java.
Bizar, copy past failed!
here are correct links
http://anonsvn.jboss.org/repos/jbpm/jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/env/SpringContext.java
http://anonsvn.jboss.org/repos/jbpm/jbpm4/trunk/modules/pvm/src/main/java/org/jbpm/pvm/internal/cfg/SpringConfiguration.java
Hi Andries,
Can JBPM4 be integrated with Spring 1.3(ya, i know 1.3 is very old… but still) ?
I tried integrating but got the following error when I tried to deploy a new process definition:
java.lang.NoSuchMethodError: org.springframework.transaction.support.TransactionTemplate.(Lorg/springframework/transaction/PlatformTransactionManager;Lorg/springframework/transaction/TransactionDefinition;)V
org.jbpm.pvm.internal.tx.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:77)
org.jbpm.pvm.internal.svc.EnvironmentInterceptor.execute(EnvironmentInterceptor.java:54)
org.jbpm.pvm.internal.svc.RetryInterceptor.execute(RetryInterceptor.java:55)
org.jbpm.pvm.internal.repository.DeploymentImpl.deploy(DeploymentImpl.java:89)
….
…
Manish,
I’m afraid that it’s 2.0 and up only :/
I’m using a new constructor it seems. It’s a small change actually, to make it backwards compatible. Instead of giving a transactionDefinition, one should just invoke the properties directly. Could you make a JIRA for it?
Thanks Andries,
Will change the SpringTransactionInterceptor class(and set the properties directly) and see if it works.
BTW here is the link to the JIRA issue that I created for this problem
https://jira.jboss.org/jira/browse/JBPM-2445.
(P.S. My first issue on the JBoss JIRA. I’m not sure whether I have reported it properly)
Please mail me (andries[AT]inze.be) when you have created a patch. I’ll check it and incorporate it into 4.1 release (1st of September).
KR,
Andries
I find one porblem. When specify continue=”async”
I’ve got
java.lang.NullPointerException
at org.jbpm.pvm.internal.jobexecutor.JobExecutorMessageSession.send(JobExecutorMessageSession.java:59)
field transaction in JobExecutorMessageSession is null.
To fix it I added in .
But now I’ve got another exception during appserver start:
Caused by: org.hibernate.SessionException: Session is closed!
It happens when standardTransaction.complete(); is invoked. Session is closed when T t = (T) template.execute(transactionCallback); is invoked.
Does anybody know how to fix this problem in right way?
Alexander,
Did you specify the without the current=”true”?
Thanks for the nullpointer exception. Could you create a JIRA issue on https://jira.jboss.org/jira/browse/JBPM ? Then I’ll fix it before 4.1 release.
ARF!! xml is deleted by my template….
the spring-transaction-interceptor is missing…
Andries, thanks for fast answer.
> Did you specify the without the current=”true”?
I specify with current=”true”. I need to remove it for correct async work?
>ARF!! xml is deleted by my template….
>the spring-transaction-interceptor is missing…
Sorry, Andries, dont understand this post….
It’s work fine if current set to “false”. One question here: is this correct configuration?
It depends
The current=true forces the spring-transaction-interceptor to use an existing transaction. If no transaction is defined, a new one is created.
So, because you are doing something async, you are running a new thread. This thread has no transaction going on, so a new transaction needs to be created.
Side-effect: when running the JUnit test, a new transaction (which will NOT be rolled back) will be created. This could be a problem…
Is that a sufficient answer?
I assumed, that this cinfiguraton must be correct, but I need to approved from jbpm developer
It’s a sufficient answer, Andries. Thank you, for your help!
Abput NPE bug. I can create JIRA issue
Hi,
It’s a very nice (and important for me) work you have done. I have a problem though. After reading jBPM Dev Guide and inspecting your demo project I still couldn’t figure it out. Maybe you could help me.
I configured all as you described: , .
When trying to deploy a process, with something like:
NewDeployment deployment = repositoryService.createDeployment();
deployment.addResourceFromClasspath(“hello-world.jpdl.xml”);
deployment.deploy();
I get the following exception:
org.jbpm.api.JbpmException: no environment for managing hibernate transaction
at org.jbpm.pvm.internal.tx.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:53)
at org.jbpm.pvm.internal.repository.DeploymentImpl.deploy(DeploymentImpl.java:89)
Do you have any ideea why this happens? I think it is something very obvious but I can’t figure it out.
Thanks a lot.
Bogdan,
Could you create a forum entry for it? http://www.jboss.org/index.html?module=bb&op=viewforum&f=217
Send the link to this blog and I’ll be sure to check it. Don’t forget to paste your spring configuration and jbpm configuration.
Thanks.
Have just created a new topic on the forums
http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4247024
Thank you.
I am hearing rumours that Spring 3.x has problems with the 3.x and 4.x flavors of jBPM. How true is this? Does anyone have jBPM integration running on Spring 3.x? If so, did you encounter problems or configuration challenges? thanks
Larry,
Intresting, but I haven’t picked up any rumor like that. Do you have any reference?
I haven’t tried it, that’s for sure.
The rumour appears to be from a young Chinese group that is concerned about Spring 3.x currently being a pre-release. SInce typically two beans are involved with jBPM+Spring integration, I’m not envisioning any nasty integration problems. But it’s always good to check with the experts.
Alexander Petrov said “To fix it I added in .” for java.lang.NullPointerException when using continue=”async”. I’m having the same issue but I’m not exactly sure what was added in. Any ideas?
Don’t fully understand the question…
Hello Andreis,
you help regarding the jbpm integration with Spring is well appreciated. I’m new in the jbpm world, but quite old in the spring world and I’m trying to integrate both of them. I chose to go with jbpm 4.2, since it is the last release, but I’m facing one big problem:
when spring is trying to instantiate the processEngine bean it fails due a NPE. Here is the stack:
Caused by: java.lang.NullPointerException
at org.jbpm.pvm.internal.cfg.ProcessEngineImpl.checkDb(ProcessEngineImpl.java:177)
at org.jbpm.pvm.internal.cfg.ProcessEngineImpl.buildProcessEngine(ProcessEngineImpl.java:170)
at org.jbpm.pvm.internal.cfg.SpringConfiguration.buildProcessEngine(SpringConfiguration.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:115)
For the integration, I’ve followed you documentation and the 6th Chapter of the dev guide. Please let me know if you have some suggestion about this. Thanks.
Hi Andries,
I’m using JBPM 4.2 and I am able to run my process using your demo configuration, but for some reason there are no execution ids. The tasks have ids, but starting the process returns an execution with a null id. Calling task.getExecutionId() also returns null.
Deploying this into JBoss returns execution ids. Any idea what the issue is?
Thanks
Walter
I have no idea
Maybe you could try the forums.
After looking for this the entire day, I stumbled on the answer – straight after posting the question
I added this to my configuration and it started to work:
Doh
<object class=”org.jbpm.pvm.internal.id.DatabaseIdComposer” init=”eager” />
This is new stuff for me too.
Thanks for the follow up!
Jbpm4.2 integration with Spring:
When setting continue=”async”, it resulted in a NPE below, whatever I set current=”false”, It doesn’t work normally.
Please give me a hand, thanks a lot.
Caused by: java.lang.NullPointerException
at org.jbpm.pvm.internal.jobexecutor.JobExecutorMessageSession.send(JobExecutorMessageSession.java:59)
I’ll need to test this myself.
However, version 4.3 has a reworked integration which should solve problems like that.
Is it possible to work with Spring+JPA+jBPM4
No.