jbpm4.4整合spring2.5(ibatis與hibernate全整合)
搞了好幾個小時總算運行成功了,記錄一下吧。
首先下載jbpm4.4,解壓出來備用。然后工程自己導入hibernate和spring的jar包以及配置文件。好了可以開始整合了!
整合的先后順序是關鍵,下面按照順序一步一步來設置:
1.首先jbpm是依賴數據庫的,所以可以先創建數據庫,我是mysql數據庫,所以到jbpm-4.4\install\src\db\create中找到jbpm.mysql.create.sql創建文件導入mysql執行!
2.設置spring中hibernate的配置:
可以參考:jbpm-4.4\install\src\cfg\hibernate\spring中的mysql.hibernate.cfg.xml(具體見下面的spring配置文件applicationContext.xml)
3.設置事務,這里由于hibernate和ibatis必須用一個事務,所以使用了TransactionAwareDataSourceProxy來管理事務。(具體見下面的spring配置文件applicationContext.xml)
4.配置默認jbpm的cfg文件,在classpath下創建一個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.tx.hibernate.cfg.xml" /> --> <import resource="jbpm.tx.spring.cfg.xml" /> <import resource="jbpm.jpdl.cfg.xml" /> <import resource="jbpm.bpmn.cfg.xml" /> <import resource="jbpm.identity.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>
5.在spring配置文件中注入工作流引擎。(具體見下面的spring配置文件applicationContext.xml)
<!--jbpm4.4工作流 --> <bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper"/> <bean id="processEngine" factory-bean="springHelper" factory-method="createProcessEngine" />
6.搞定以上配置文件后添加jbpm.jar, juel-api.jar,juel-engine.jar,juel-impl.jar,mail.jar,這里注意一下,可能有的朋友之后運行還會有錯誤如下:
java.lang.NoSuchMethodError: antlr.collections.AST.getLine()I
這是因為struts2的anltr-2.7.2.jar過舊導致的,我們為了省掉麻煩這里最好直接把struts2的antlr.jar去掉(windows---preferences---在文本框中搜索struts 2(中間有空格)---選擇struts 2---選擇antlr---remove),然后重新到發布到服務器的lib目錄下刪除anltr-2.7.2.jar即可。
7.成功的關鍵就是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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-autowire="byName">
<context:property-placeholder location="classpath*:database.properties" />
<!--jbpm4.4工作流 -->
<bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper"/>
<bean id="processEngine" factory-bean="springHelper" factory-method="createProcessEngine" />
<!-- dataSourceproxy 配置代理管理事務 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy"
p:targetDataSource-ref="dynamicDataSource" />
<!-- dataSource 多數據源支持 -->
<bean id="dynamicDataSource" class="com.xuyi.support.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="dataSource" value-ref="dataSourceJDBC" />
</map>
</property>
</bean>
<!-- c3p0數據源配置 -->
<bean id="dataSourceJDBC" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close" p:driverClass="${jdbc.driverClass}" p:jdbcUrl="${jdbc.jdbcUrl}"
p:user="${jdbc.user}" p:password="${jdbc.password}" p:initialPoolSize="${c3p0.initialPoolSize}"
p:minPoolSize="${c3p0.minPoolSize}" p:maxPoolSize="${c3p0.maxPoolSize}"
p:acquireIncrement="${c3p0.acquireIncrement}" p:maxIdleTime="${c3p0.maxIdleTime}"
p:maxStatements="${c3p0.maxStatements}" lazy-init="true" />
<!-- hibernate-spring 基本配置 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath*:com/xuyi/modal/Creater.hbm.xml</value>
<value>classpath*:com/xuyi/modal/Month.hbm.xml</value>
<value>classpath*:com/xuyi/modal/Thing.hbm.xml</value>
<value>classpath*:jbpm.repository.hbm.xml</value>
<value>classpath*:jbpm.execution.hbm.xml</value>
<value>classpath*:jbpm.history.hbm.xml</value>
<value>classpath*:jbpm.task.hbm.xml</value>
<value>classpath*:jbpm.identity.hbm.xml</value>
</list>
</property>
<!-- 使用TransactionAwareDataSourceProxy管理事務與ibatis處于同一事務管理下 -->
<property name="useTransactionAwareDataSource" value="true"></property>
</bean>
<!-- ibatis-spring 配置 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:sql-map-config.xml"></property>
</bean>
<!-- spring transaction 事務管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 事務代理攔截器的配置 -->
<bean id="transactionProxy" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>database.properties:jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3306/myweb jdbc.user=root jdbc.password=pwd c3p0.initialPoolSize=1 c3p0.minPoolSize=1 c3p0.maxPoolSize=10 c3p0.acquireIncrement=5 c3p0.maxIdleTime=1800 c3p0.maxStatements=0注意:這里會遇到一個類:com.xuyi.support.DynamicDataSource這類實現了多數據源管理。如下:
package com.xuyi.support;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
private static ThreadLocal<String> local = new ThreadLocal<String>();
@Override
protected Object determineCurrentLookupKey() {
return local.get() == null ? "dataSource" : local.get();
}
// ---------------------------------------------------------------------------------------------------
/**
* 設置數據源路徑
*/
public static void setRoute(String route) {
if (route==null || route.equals("")){
route = "dataSource";
}
local.set(route);
}
}
8.終于可以開始測試了,先創建一個發布用的xml放入classpath:
測試用的流程swing.jpdl.xml<?xml version="1.0" encoding="UTF-8"?>
<process name="swing" xmlns="http://jbpm.org/4.3/jpdl">
<start g="94,64,48,48" name="start1">
<transition g="-52,-22" name="A" to="A"/>
</start>
<task assignee="A" g="73,195,92,52" name="A">
<transition g="-52,-22" name="B" to="B"/>
</task>
<task assignee="B" g="266,192,92,52" name="B">
<transition g="-40,-21" name="end" to="end1"/>
</task>
<end g="290,327,48,48" name="end1"/>
</process> 然后寫一個測試類進行測試:package com.xuyi.test;
import java.util.List;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.TaskService;
import org.jbpm.api.task.Task;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestJbpm{
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
applicationContext.start();
ProcessEngine processEngine = (ProcessEngine)applicationContext.getBean("processEngine");
ExecutionService executionService = processEngine.getExecutionService();
TaskService taskService = processEngine.getTaskService();
//發布流程
String deploymentId = processEngine.getRepositoryService().createDeployment()
.addResourceFromClasspath("swing.jpdl.xml").deploy();
System.out.println("流程發布ID:"+deploymentId);
//啟動一個流程實例
ProcessInstance processInstance = executionService.startProcessInstanceByKey("swing");
System.out.println("流程實例ID:" + processInstance.getId());
//A處理任務
List<Task> taskList_A = taskService.findPersonalTasks("A");
System.out.println("A待處理任務數:" + taskList_A.size());
if(taskList_A.size() > 0){
for(Task task : taskList_A){
System.out.println(task.getId());
taskService.completeTask(task.getId());
}
}
//B處理任務
List<Task> taskList_B = taskService.findPersonalTasks("B");
System.out.println("B待處理任務數:" + taskList_B.size());
if(taskList_B.size() > 0){
for(Task task : taskList_B){
System.out.println(task.getId());
taskService.completeTask(task.getId());
}
}
}
} 一切順利的話就可以看到具體打印了!!關于jbpm的xml專用設計插件,可以看我以前的在Myeclipse8.6安裝JBPM插件
這篇文章。這樣設計好xml就可以發布給jbpm進行工作流處理了。
轉自:http://jsczxy2.iteye.com/blog/1236418