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"?>

<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd

http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<beanid="propertyConfigurer"

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<propertyname="location">

<value>classpath:/appContext/c3p0.properties</value>

</property>

</bean>

<beanid="dataSource"class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close">

<propertyname="driverClass"value="${c3p0.driverClassName}"/>

<propertyname="jdbcUrl"value="${c3p0.url}"/>

<propertyname="user"value="${c3p0.username}"/>

<propertyname="password"value="${c3p0.password}"/>

<propertyname="checkoutTimeout"value="${c3p0.checkoutTimeout}"/>

<propertyname="idleConnectionTestPeriod"value="${c3p0.idleConnectionTestPeriod}"/>

<propertyname="initialPoolSize"value="${c3p0.initialPoolSize}"/>

<propertyname="maxIdleTime"value="${c3p0.maxIdleTime}"/>

<propertyname="maxPoolSize"value="${c3p0.maxPoolSize}"/>

<propertyname="minPoolSize"value="${c3p0.minPoolSize}"/>

<propertyname="maxStatements"value="${c3p0.maxStatements}"/>

<propertyname="acquireIncrement"value="${c3p0.acquireIncrement}"/>

</bean>

<beanid="oracleLobHandle"class="org.springframework.jdbc.support.lob.DefaultLobHandler">

</bean>

<beanid="sessionFactory"

class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<propertyname="lobHandler"ref="oracleLobHandle"/>

<propertyname="dataSource">

<reflocal="dataSource"/>

</property>

<propertyname="hibernateProperties">

<props>

<propkey="hibernate.dialect">

${hibernate.dialect}

</prop>

    <prop key="hibernate.show_sql">${showsql}</prop>

   </props>

</property>

<propertyname="mappingDirectoryLocations">

<list>

<value>

classpath:com/tjtt/tdm/model

</value>

</list>

  </property> 

映射jbpm的一些服务,必须要配置

<propertyname="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>

<beanid="transactionManager"

class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<propertyname="sessionFactory"ref="sessionFactory"/>

</bean>

<aop:config>

<aop:pointcutid="jdbcServiceMethod"expression="execution(**..service.*Service.*(..))"/>

<aop:advisorpointcut-ref="jdbcServiceMethod"advice-ref="txAdvice"/>

</aop:config>

<tx:adviceid="txAdvice"transaction-manager="transactionManager">

<tx:attributes>

<tx:methodname="insert*"rollback-for="ServiceException"/>

<tx:methodname="save*"rollback-for="ServiceException"/>

<tx:methodname="add*"rollback-for="ServiceException"/>

<tx:methodname="update*"rollback-for="ServiceException"/>

<tx:methodname="del*"rollback-for="ServiceException"/>

<tx:methodname="delete*"rollback-for="ServiceException"/>

<tx:methodname="remove*"rollback-for="ServiceException"/>

<tx:methodname="batchUpdate*"rollback-for="ServiceException"/>

<tx:methodname="*"rollback-for="ServiceException"/>

</tx:attributes>

</tx:advice>

</beans>

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

<?xml version="1.0" encoding="UTF-8"?>

<beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd">

<!--将ProcessEngine对象交给Spring管理-->

<beanid="springHelper"class="org.jbpm.pvm.internal.processengine.SpringHelper">

<propertyname="jbpmCfg"value="jbpm.cfg.xml"></property>

</bean>

<beanid="processEngine"factory-bean="springHelper"factory-method="createProcessEngine"/>

<!--各种JBPM服务对象的配置-->

<beanid="repositoryService"factory-bean="processEngine"factory-method="getRepositoryService"/>

<beanid="executionService"factory-bean="processEngine"factory-method="getExecutionService"/>

<beanid="taskService"factory-bean="processEngine"factory-method="getTaskService"/>

<beanid="historyService"factory-bean="processEngine"factory-method="getHistoryService"/>

</beans>

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

相关推荐