jbpm4事务和spring事务的整合

我们知道,支持嵌入到各种架构环境中使用一直是 jbpm工作流引擎的核心竞争力之一,自jbpm3版本开始,jbpm工作流引擎就在很多应用中被集成到spring等架构中使用,从jbpm4.4开始,jbpm工作流引擎可以支持开发者很自然的将其集成到spring架构中使用; spring架构集成jbpm4,只要达成两个目标,就可以基本成功了:

 

 

 1.持久化集成:默认地,jbpm4为每个客户端操作开启一个事务,在此事务中调用服务api,而在通常的spring应用中,应用的访问一般来自web的http请求,然后在此http请求线程中,通过调用spring bean的方法进入事务边界,这与标准的jbpm4事务处理方式是不同的。因此jbpm4提供了相应的工具将自身的持久化事务管理权交给了spring框架

 

 2.服务集成: 默认地,jbpm4的客户端通过硬编码获取各种工作流服务接口。现在,需要将这些jbpm4服务接口集成到spring的IOC架构中,作为spring Bean,经由依赖注入等方式供客户端应用调用

 

 

  1. 具体步骤如下:

 

 1.首先需要将jbpm4默认的hibernate事务管理配置替换为spring事务管理配置: jbpm4.4源码中的 'jbpm.tx.spring.cfg.xml'文件复制到工程的src下面,然后在jbpm.cfg.xml中将'<import resource="jbpm.tx.hibernate.cfg.xml" />  '  修改为'<import resource="jbpm.tx.spring.cfg.xml" />'

 

2.然后在spring配置文件的applicationContext.xml文件中,作如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="
http://www.springframework.org/schema/beans"
 xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="
http://www.springframework.org/schema/tx"
 xsi:schemaLocation="
   
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 <bean id="propertyConfigurer"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location">
   <value>classpath:/appContext/c3p0.properties</value>
  </property>
 </bean>
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
  destroy-method="close">
  <property name="driverClass" value="${c3p0.driverClassName}" />
  <property name="jdbcUrl" value="${c3p0.url}" />
  <property name="user" value="${c3p0.username}" />
  <property name="password" value="${c3p0.password}" />
  <property name="checkoutTimeout" value="${c3p0.checkoutTimeout}" />
  <property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}" />
  <property name="initialPoolSize" value="${c3p0.initialPoolSize}" />
  <property name="maxIdleTime" value="${c3p0.maxIdleTime}" />
  <property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
  <property name="minPoolSize" value="${c3p0.minPoolSize}" />
  <property name="maxStatements" value="${c3p0.maxStatements}" />
  <property name="acquireIncrement" value="${c3p0.acquireIncrement}" />
 </bean>
 <bean id="oracleLobHandle" class="org.springframework.jdbc.support.lob.DefaultLobHandler">
 </bean>
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="lobHandler" ref="oracleLobHandle" />
  <property name="dataSource">
   <ref local="dataSource" /> 
  
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     ${hibernate.dialect}
    </prop>
    <prop key="hibernate.show_sql">${showsql}</prop>

   </props>
  </property>
  <property name="mappingDirectoryLocations">
   <list>
    <value>
     classpath:com/tjtt/tdm/model
    </value>
   </list>
  </property> 

映射jbpm的一些服务,必须要配置
  <property name="mappingResources">  
            <list>  
                <value>jbpm.repository.hbm.xml</value>    
                <value>jbpm.execution.hbm.xml</value>    
                <value>jbpm.history.hbm.xml</value>    
                <value>jbpm.task.hbm.xml</value>    
                <value>jbpm.identity.hbm.xml</value>    
            </list>  
        </property>
 </bean>
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 <aop:config>
  <aop:pointcut id="jdbcServiceMethod" expression="execution(* *..service.*Service.*(..))" />
  <aop:advisor pointcut-ref="jdbcServiceMethod" advice-ref="txAdvice" />
 </aop:config>
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="insert*" rollback-for="ServiceException" />
   <tx:method name="save*" rollback-for="ServiceException" />
   <tx:method name="add*" rollback-for="ServiceException" />
   <tx:method name="update*" rollback-for="ServiceException" />
   <tx:method name="del*" rollback-for="ServiceException" />
   <tx:method name="delete*" rollback-for="ServiceException" />
   <tx:method name="remove*" rollback-for="ServiceException" />
   <tx:method name="batchUpdate*" rollback-for="ServiceException" />
   <tx:method name="*" rollback-for="ServiceException" />
  </tx:attributes>
 </tx:advice>
</beans>

 

 

3.在src下面添加 applicationContext-jbpm.xml 文件:配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="
http://www.springframework.org/schema/beans"
 xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="
http://www.springframework.org/schema/context"
 xsi:schemaLocation="
 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 <!-- 将ProcessEngine对象交给Spring管理 -->
  <bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper" >
     <property name="jbpmCfg" value="jbpm.cfg.xml"></property>
  </bean>
   <bean id="processEngine" factory-bean="springHelper" factory-method="createProcessEngine" />
   <!-- 各种JBPM服务对象的配置 -->
   <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
 <bean id="executionService" factory-bean="processEngine" factory-method="getExecutionService" />
 <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
  <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
</beans>

 

至此,大功告成,jbpm的事务就和spring的事务集成到一起了!

你可能感兴趣的:(jbpm)