`
ronghao
  • 浏览: 448736 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
E9473dd5-1985-3883-ac98-962354ca10b3
张小庆,在路上
浏览量:8538
社区版块
存档分类
最新评论

jBPM4与Spring的集成

阅读更多

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

 

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

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

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


      下面分别讨论:

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

ProcessEngine processEngine = new Configuration()
      .buildProcessEngine();
 

 

RepositoryService repositoryService = processEngine.getRepositoryService();
ExecutionService executionService = processEngine.getExecutionService();
TaskService taskService = processEngine.getTaskService();
HistoryService historyService = processEngine.getHistoryService();
ManagementService managementService = processEngine.getManagementService();
 



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

<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。

environment.setContext(new SpringContext(applicationContext));

 


SpringContext是对applicationContext的简单封装。

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

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

    <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里:

<command-service>
      <retry-interceptor />
      <environment-interceptor />
      <standard-transaction-interceptor />
    </command-service>

 


2、    原有的事务实现

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

 

jBPM4的事务是基于Command的。

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

 

使jBPM4使用Spring提供的事务:

<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)强制从当前线程中获取:

<hibernate-session current="true"/>

 


并干掉原有的事务实现:

<transaction />

 


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

  • 大小: 28.7 KB
  • 大小: 38.4 KB
  • 大小: 95 KB
  • 大小: 48.7 KB
分享到:
评论
7 楼 wwyinhuabin 2009-08-04  
谢谢楼主啊 , 楼主帮忙看下,这个错误很奇怪,我看了源代码的 ,是有这个构造函数的,并且我这个jbpm.cfg.xml文件是肯定存在的,跟Spring 的这个配置文件在同一目录下
6 楼 wwyinhuabin 2009-08-04  
Error creating bean with name 'jbpmConfiguration' defined in file [C:\Tomcat 5.5\webapps\ants\WEB-INF\classes\applicationContext-base.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.jbpm.pvm.internal.cfg.SpringConfiguration]: Constructor threw exception; nested exception is java.lang.NoSuchMethodError: org.apache.log4j.Logger.trace(Ljava/lang/Object;)
我的可能出错配置是
<bean id="jbpmConfiguration" class="org.jbpm.pvm.internal.cfg.SpringConfiguration">
   <constructor-arg value="jbpm.cfg.xml"/>
</bean>
楼主知道是为什么吗
5 楼 小开ye 2009-07-02  
最初参考的一个资料,全部用bean来指定jbpm.cfg.xml文件中的定义。
也就是说,没有用到jbpm.cfg.xml这个文件。
现在使用了这个文件,比较方便了。
4 楼 lovexiu111 2009-07-01  
去掉jbpm.cfg.xml配置文件中的<transaction />后,JOB不可用。
有没有好的解决方案。
3 楼 ronghao 2009-06-22  
小开ye 写道
整合成功了,是我之前版本整合的有些错误,走了弯路。

之前碰到什么问题?
2 楼 小开ye 2009-06-22  
整合成功了,是我之前版本整合的有些错误,走了弯路。
1 楼 小开ye 2009-06-22  
请问是否JBPM4.0cr1版本?

能否提供jbpm.cfg.xml和相关bean的配置信息?谢谢。

整合过之前的版本,这个版本折腾了一天,还没有搞定,呵呵。

相关推荐

Global site tag (gtag.js) - Google Analytics