JBPM4事务

jBPM4与Spring的集成

关键字: jbpm4

      现在流行抱大腿,不过对眼光的要求也颇高。要不就如高也,即使四眼,一样无用。对Java企业开发而言,Spring的腿则是一定要抱的。而所谓抱Spring的腿,无外乎三点:

 

一是通过Spring暴露出服务,将服务配置到Spring的IOC容器里;

二是在自己的运行环境里访问到Spring的IOC容器,能够轻松使用Spring容器里所配置的服务;

三是对于具有事务管理特性的项目来说,将事务管理与Spring的事务管理进行合并。


      下面分别讨论:

一、    通过Spring暴露服务
还记得在jBPM4的运行期环境里提到的JbpmConfiguration吗?它是整个jBPM4的入口,并且是整个应用独此一份的。通过它可以获取processEngine,并藉此获得工作流引擎所提供的各种服务:

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ronghao.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=ProcessEngine%20processEngine%20%3D%20new%20Configuration()%0A%20%20%20%20%20%20.buildProcessEngine()%3B" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. ProcessEngine processEngine =  new  Configuration()  
  2.       .buildProcessEngine();  
ProcessEngine processEngine = new Configuration()
      .buildProcessEngine();
 

 

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ronghao.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=RepositoryService%20repositoryService%20%3D%20processEngine.getRepositoryService()%3B%0AExecutionService%20executionService%20%3D%20processEngine.getExecutionService()%3B%0ATaskService%20taskService%20%3D%20processEngine.getTaskService()%3B%0AHistoryService%20historyService%20%3D%20processEngine.getHistoryService()%3B%0AManagementService%20managementService%20%3D%20processEngine.getManagementService()%3B" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. RepositoryService repositoryService = processEngine.getRepositoryService();  
  2. ExecutionService executionService = processEngine.getExecutionService();  
  3. TaskService taskService = processEngine.getTaskService();  
  4. HistoryService historyService = processEngine.getHistoryService();  
  5. ManagementService managementService = processEngine.getManagementService();  
RepositoryService repositoryService = processEngine.getRepositoryService();
ExecutionService executionService = processEngine.getExecutionService();
TaskService taskService = processEngine.getTaskService();
HistoryService historyService = processEngine.getHistoryService();
ManagementService managementService = processEngine.getManagementService();
 



通过Spring暴露这些服务,配置如下:

Xml代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ronghao.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3Cbean%20id%3D%22jbpmConfiguration%22%20class%3D%22org.jbpm.pvm.internal.cfg.SpringConfiguration%22%3E%0A%20%20%20%20%20%20%20%20%3Cconstructor-arg%20value%3D%22be%2Finze%2Fspring%2Fdemo%2Fjbpm.cfg.xml%22%20%2F%3E%0A%20%20%20%20%3C%2Fbean%3E%0A%20%20%20%0A%20%20%20%20%3Cbean%20id%3D%22processEngine%22%20factory-bean%3D%22jbpmConfiguration%22%20factory-method%3D%22buildProcessEngine%22%20%2F%3E%0A%20%20%20%20%3Cbean%20id%3D%22repositoryService%22%20factory-bean%3D%22processEngine%22%20factory-method%3D%22getRepositoryService%22%20%2F%3E%0A%20%20%20%20%3Cbean%20id%3D%22executionService%22%20factory-bean%3D%22processEngine%22%20factory-method%3D%22getExecutionService%22%20%2F%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. < bean   id = "jbpmConfiguration"   class = "org.jbpm.pvm.internal.cfg.SpringConfiguration" >   
  2.         < constructor-arg   value = "be/inze/spring/demo/jbpm.cfg.xml"   />   
  3.     </ bean >   
  4.      
  5.     < bean   id = "processEngine"   factory-bean = "jbpmConfiguration"   factory-method = "buildProcessEngine"   />   
  6.     < bean   id = "repositoryService"   factory-bean = "processEngine"   factory-method = "getRepositoryService"   />   
  7.     < bean   id = "executionService"   factory-bean = "processEngine"   factory-method = "getExecutionService"   />   
<bean id="jbpmConfiguration" class="org.jbpm.pvm.internal.cfg.SpringConfiguration">
        <constructor-arg value="be/inze/spring/demo/jbpm.cfg.xml" />
    </bean>
   
    <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" />
 



细心的你会发现,配置时使用了JbpmConfiguration 的子类SpringConfiguration。SpringConfiguration相比JbpmConfiguration有哪些增强呢,下面再 讲。总之,现在,就可以使用Spring来获取或注入这些Jbpm4所提供的服务了。

二、在environment里加入SpringContext
jBPM4 的environment(运行期环境)提供Engine IOC(process-engine-context)和Transaction IOC(transaction-context)。要想在运行期方便地访问到Spring里所配置的服务,最直接的方法就是在environment里 加入Spring IOC(applicationContext)的引用。


SpringConfiguration即是对JbpmConfiguration增强了对Spring IOC的一个引用。
 

 
SpringConfiguration是如何做到的呢?简单,实现Spring的ApplicationContextAware接口,自动持有applicationContext,然后openEnvironment时将其加入environment。

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ronghao.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=environment.setContext(new%20SpringContext(applicationContext))%3B" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. environment.setContext( new  SpringContext(applicationContext));  
environment.setContext(new SpringContext(applicationContext));

 


SpringContext是对applicationContext的简单封装。

那么什么从Engine IOC移民到Spring IOC了呢?是的,最重要的就是Hibernate Session Factory

在jbpm.cfg.xml的process-engine-context里干掉:

Xml代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ronghao.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%20%20%20%20%3Chibernate-configuration%3E%0A%20%20%20%20%20%20%3Ccfg%20resource%3D%22jbpm.hibernate.cfg.xml%22%20%2F%3E%20%20%20%20%0A%20%20%20%20%3C%2Fhibernate-configuration%3E%0A%0A%20%20%20%20%3Chibernate-session-factory%20%2F%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. < hibernate-configuration >   
  2.   < cfg   resource = "jbpm.hibernate.cfg.xml"   />       
  3. </ hibernate-configuration >   
  4.   
  5. < hibernate-session-factory   />   
    <hibernate-configuration>
      <cfg resource="jbpm.hibernate.cfg.xml" />    
    </hibernate-configuration>

    <hibernate-session-factory />

 

 
相关配置挪动至Spring配置文件。

三、    事务

哪里有数据库操作,哪里就有事务。对于嵌入式工作流而言,最重要的集成就是事务的集成。这里先分析jBPM4的事务实现,然后再介绍集成入Spring的事务实现。

1、    Command模式
jBPM4的逻辑实现采用了Command模式。
 

 
采用Command模式后,jBPM4对CommandService构造拦截器(Interceptor)链,配置在jbpm.cfg.xml的process-engine-context里:

Xml代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ronghao.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3Ccommand-service%3E%0A%20%20%20%20%20%20%3Cretry-interceptor%20%2F%3E%0A%20%20%20%20%20%20%3Cenvironment-interceptor%20%2F%3E%0A%20%20%20%20%20%20%3Cstandard-transaction-interceptor%20%2F%3E%0A%20%20%20%20%3C%2Fcommand-service%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. < command-service >   
  2.       < retry-interceptor   />   
  3.       < environment-interceptor   />   
  4.       < standard-transaction-interceptor   />   
  5.     </ command-service >   
<command-service>
      <retry-interceptor />
      <environment-interceptor />
      <standard-transaction-interceptor />
    </command-service>

 


2、    原有的事务实现

jBPM4原有的事务通过StandardTransactionInterceptor实现,在CommandService执行Command之前打开事务(实际委派Hibernate的事务管理),完成后提交/回滚。
 

 

jBPM4的事务是基于Command的。

3、    集成入Spring的事务实现
Spring的事务是基于服务调用的。
 

 

使jBPM4使用Spring提供的事务:

Xml代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ronghao.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3Ccommand-service%3E%0A%20%20%20%20%20%20%3Cretry-interceptor%20%2F%3E%0A%20%20%20%20%20%20%3Cenvironment-interceptor%20%2F%3E%0A%20%20%20%20%20%20%3Cspring-transaction-interceptor%20current%3D%22true%22%20%2F%3E%0A%3C%2Fcommand-service%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. < command-service >   
  2.       < retry-interceptor   />   
  3.       < environment-interceptor   />   
  4.       < spring-transaction-interceptor   current = "true"   />   
  5. </ command-service >   
<command-service>
      <retry-interceptor />
      <environment-interceptor />
      <spring-transaction-interceptor current="true" />
</command-service>
 



拦截器换用SpringTransactionInterceptor ,SpringTransactionInterceptor从environment 提供的Spring IOC获取PlatformTransactionManager,使用事务模板回调Command,事务传播模式强制加入当前事务。

同时,对hibernate session的配置(jbpm.cfg.xml的transaction-context)强制从当前线程中获取:

Xml代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ronghao.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3Chibernate-session%20current%3D%22true%22%2F%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. < hibernate-session   current = "true" />   
<hibernate-session current="true"/>

 


并干掉原有的事务实现:

Xml代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://ronghao.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3Ctransaction%20%2F%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. < transaction   />   
<transaction />

 


参考文档:
http://www.slideshare.net/guest8d4bce/spring-integration-with-jbpm4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值