新Jbpm4.4与spring整合方式

背景:最近项目要用到工作流,在网上搜了一些jbpm4和spring的整合方案,发现大部分都是官方文档提供的方案(本文不详细描述官方方案)。
但官方整合方式有其缺陷,例如遇到如下情况,系统架构是通过web.xml加载项目的所有spring文件,并出现了多个SessionFactory的情况。中途客户提出新的需求,需要引入工作流引擎(这里用Jbpm4.4) ,我们需要新建DataSource和SesionFacotory,此时我们用官方的方案就会出现架构混乱的状况。

解决方案:

一、定义自己的ProcessEngine工厂

 

public class ProcessEngineFactoryBean extends SpringHelper {

	protected SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public ProcessEngine createProcessEngine() {
		processEngine = new ConfigurationImpl().springInitiated(
				applicationContext).setResource(jbpmCfg)
				.setHibernateSessionFactory(sessionFactory)
				.buildProcessEngine();
		return processEngine;
	}

}

  

 二、创建Spring配置文件,用于新建DataSource和SesionFacotory,此处省略了jdbc.properties(即数据库连接信息)。

 

 

<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="dataSource-jbpm" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName">
			<value>${jbpm.hibernate.connection.driver_class}</value>
		</property>
		<property name="url">
			<value>${jbpm.hibernate.connection.url}</value>
		</property>
		<property name="username">
			<value>${jbpm.hibernate.connection.username}</value>
		</property>
		<property name="password">
			<value>${jbpm.hibernate.connection.password}</value>
		</property>
		<property name="maxActive">
			<value>${jbpm.hibernate.connection.maxActive}</value>
		</property>
		<property name="maxIdle">
			<value>${jbpm.hibernate.connection.maxIdle}</value>
		</property>
		<property name="maxWait">
			<value>${jbpm.hibernate.connection.maxWait}</value>
		</property>
	</bean>

	<bean id="sessionFactory-jbpm"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${jbpm.hibernate.connection.dialect}</prop>
				<prop key="hibernate.show_sql">true</prop>
			</props>
		</property>
		<property name="dataSource">
			<ref bean="dataSource-jbpm" />
		</property>
		<property name="configLocations">
			<list>
				<value>/WEB-INF/hibernate/jbpm/jbpm.hibernate.cfg.xml</value>
			</list>
		</property>
		<property name="lobHandler" ref="lobHandler" />
	</bean>

	<bean id="nativeJdbcExtractor"
		class="org.springframework.jdbc.support.nativejdbc.CommonsDbcpNativeJdbcExtractor"
		lazy-init="true" />

	<bean id="lobHandler" class="org.springframework.jdbc.support.lob.OracleLobHandler"
		lazy-init="true">
		<property name="nativeJdbcExtractor">
			<ref local="nativeJdbcExtractor" />
		</property>
	</bean>

	<bean id="transactionManager-jbpm"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="sessionFactory-jbpm" />
		</property>
	</bean>

	<!-- jbpm配置 com.dawn.jbpm4.cfg.pvm.ProcessEngineFactoryBean上面已经定义-->
	<bean id="springHelper" class="com.dawn.jbpm4.cfg.pvm.ProcessEngineFactoryBean">
		<property name="jbpmCfg" value="com/dawn/jbpm4/cfg/jbpm.cfg.xml"></property>
		<property name="sessionFactory">
			<ref local="sessionFactory-jbpm" />
		</property>
	</bean>

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

	<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
	<bean id="executionService" factory-bean="processEngine" factory-method="getExecutionService" />
	<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
	<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
	<bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" />
	<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />

</beans>

 三、创建我们的jbpm.cfg.xml<==>com/dawn/jbpm4/cfg/jbpm.cfg.xml

 

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

<jbpm-configuration>

	<import resource="jbpm.default.cfg.xml" />
	<import resource="jbpm.businesscalendar.cfg.xml" />
	<import resource="jbpm.jpdl.cfg.xml" />
	<import resource="jbpm.bpmn.cfg.xml" />
	<import resource="jbpm.identity.cfg.xml" />
	<import resource="jbpm.console.cfg.xml" />	
	
	<import resource="com/dawn/jbpm4/cfg/jbpm.tx.spring.cfg.xml" />
	
	<!--此处为官方配置  
	<import resource="jbpm.tx.spring.cfg.xml" />
	<process-engine-context>
	 	<string name="spring.cfg" value="com/dawn/jbpm4/cfg/spring-jbpm.xml" />   
	 </process-engine-context>
	-->
	<!--  <import resource="jbpm.tx.hibernate.cfg.xml" />-->
	<!-- Job executor is excluded for running the example test cases. -->
	<!--
		To enable timers and messages in production use, this should be
		included.
	-->
	<!--
  	<import resource="jbpm.jobexecutor.cfg.xml" />
  	-->

</jbpm-configuration>

 

 

 

 四、创建jbpm.tx.spring.cfg.xml<==>com/dawn/jbpm4/cfg/jbpm.tx.spring.cfg.xml,修改了spring事务拦截方式

 

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

<jbpm-configuration spring="enabled">

  <process-engine-context>
  
    <command-service name="newTxRequiredCommandService">
      <retry-interceptor />
      <environment-interceptor policy="requiresNew" />
      <spring-transaction-interceptor policy="requiresNew" />
    </command-service>
    
    <!-- Default command service has a Spring transaction interceptor-->
    <command-service name="txRequiredCommandService">
      <retry-interceptor />
      <environment-interceptor />
      <spring-transaction-interceptor transaction-manager="transactionManager-jbpm"/>
    </command-service>
    
  </process-engine-context>

  <transaction-context>
    <transaction type="spring" />
    <hibernate-session current="true" />
  </transaction-context>

</jbpm-configuration>

 

OK!你可以试试效果了,希望对遇到同样需求的朋友有帮助!各位多多指教!

你可能感兴趣的:(spring,Hibernate,bean,xml,jbpm)