Skip to content


Documentation Spring jBPM integration

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:

Screenshot-1.png

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

Posted in jboss, jbpm.


53 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Alex says

    Hi Andries,

    you have forgot the class for jbpmConfiguration bean in your configuration sniplet: <bean id=”jbpmConfiguration” class=”org.jbpm.pvm.internal.cfg.SpringConfiguration”>

  2. Andries Inzé says

    How bizar, I could have sworn that I copy pasted it! :)

  3. Andries Inzé says

    Thanks, I’ve edited the post.

  4. gigliglich says

    HI, what file are you going to insert the snippet below?

    Thanks.

  5. gigliglich says

    sorry i forgot to insert the snippet

  6. gigliglich says

    < script-manager …..

  7. Andries Inzé says

    jbpm.cfg.xml (check the source code at
    http://jbpm4-spring-demo.googlecode.com/svn/trunk/ )

  8. gigliglich says

    thanks i saw it.

  9. Anonymous says

    really good stuff

  10. wattanapol says

    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.

  11. Andries Inzé says

    It fails on 2.5?

  12. sridhar says

    It works well for me on Spring 2.5

  13. gigliglich says

    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.

  14. Andries Inzé says

    Spring bean activity is the old way.
    A better way is using the

    Check the demo ;)

  15. gigliglich says

    ok Andries, thank you.

  16. VirageGroup says

    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

  17. Andries Inzé says

    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.

  18. VirageGroup says

    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.

  19. Manish says

    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)
    ….

  20. Andries Inzé says

    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?

  21. Manish says

    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)

  22. Andries Inzé says

    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

  23. Alexander Petrov says

    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?

  24. Andries Inzé says

    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.

  25. Andries Inzé says

    ARF!! xml is deleted by my template….

    the spring-transaction-interceptor is missing…

  26. Alexander Petrov says

    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….

  27. Alexander Petrov says

    It’s work fine if current set to “false”. One question here: is this correct configuration?

  28. Andries Inzé says

    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?

  29. Alexander Petrov says

    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!

  30. Alexander Petrov says

    Abput NPE bug. I can create JIRA issue :-)

  31. Bogdan J says

    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.

  32. Andries Inzé says

    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.

  33. Bogdan J says

    Have just created a new topic on the forums
    http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4247024

    Thank you.

  34. Larry H says

    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

  35. Andries Inzé says

    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.

  36. Larry H says

    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.

  37. Duane M says

    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?

  38. Andries Inzé says

    Don’t fully understand the question…

  39. gio says

    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.

  40. Walter Seymore says

    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

  41. Andries Inzé says

    I have no idea :D
    Maybe you could try the forums.

  42. Walter Seymore says

    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:

  43. Walter Seymore says

    Doh

    <object class=”org.jbpm.pvm.internal.id.DatabaseIdComposer” init=”eager” />

  44. Andries Inzé says

    This is new stuff for me too.
    Thanks for the follow up!

  45. Diego says

    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)

  46. Andries Inzé says

    I’ll need to test this myself.
    However, version 4.3 has a reworked integration which should solve problems like that.

  47. Mymacin says

    Is it possible to work with Spring+JPA+jBPM4

  48. Andries Inzé says

    No.

1 2

Continuing the Discussion

  1. Spring jBPM integration Manual - Java Tutorial linked to this post on June 5, 2011

    [...] good tutorial on Spring jBPM integration Manual: Any project, especially open source projects, rise or fall with [...]



Some HTML is OK

or, reply to this post via trackback.

 



Improve Your Life, Go The myEASY Way™