搞了好几个小时总算运行成功了,记录一下吧。
首先下载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进行工作流处理了。
另外如果安装插件有中文乱码问题,或者有控制台乱码问题,可以参考:
Eclipse里做JBPM工作流gpd.xml中文乱码问题解决(包括控制台乱码解决)
这篇文章。
新加spring3.0的配置档一份包含事务AOP处理。
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName"> <context:property-placeholder location="classpath*:database.properties,classpath*:memcached.properties" /> <!-- spring注释自动注入 --> <context:annotation-config/> <context:component-scan base-package="com.myweb" /> <!-- dataSourceproxy 配置代理管理事务 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy" p:targetDataSource-ref="dynamicDataSource" /> <!-- dataSource 多数据源支持 --> <bean id="dynamicDataSource" class="com.myweb.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}" p:acquireRetryAttempts="${c3p0.acquireRetryAttempts}" p:acquireRetryDelay="${c3p0.acquireRetryDelay}" p:testConnectionOnCheckin="${c3p0.testConnectionOnCheckin}" p:automaticTestTable="${c3p0.automaticTestTable}" p:idleConnectionTestPeriod="${c3p0.idleConnectionTestPeriod}" p:checkoutTimeout="${c3p0.checkoutTimeout}" 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.MySQLDialect</prop> <prop key="hibernate.query.factory_class">org.hibernate.hql.classic.ClassicQueryTranslatorFactory</prop> <prop key="hibernate.jdbc.fetch_size">50</prop> <prop key="hibernate.jdbc.batch_size">25</prop> <prop key="hibernate.cache.use_query_cache">true</prop> <prop key="hibernate.cache.use_second_level_cache">true</prop> <prop key="hibernate.max_fetch_depth">1</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.cache.provider_class">com.googlecode.hibernate.memcached.MemcachedCacheProvider</prop> <prop key="hibernate.memcached.servers">${memcached.object.servers}</prop> <prop key="hibernate.memcached.cacheTimeSeconds">86400</prop> <prop key="hibernate.memcached.clearSupported">false</prop> <!--<prop key="hibernate.memcached.operationTimeout">1000</prop>--> <prop key="hibernate.connection.release_mode">after_transaction</prop> <prop key="hibernate.transaction.flush_before_completion">true</prop> <prop key="hibernate.transaction.auto_close_session">true</prop> <prop key="hibernate.cache.use_structured_entries">true</prop> <prop key="hibernate.cache.region_prefix">OOXX_memcached</prop> <prop key="hibernate.connection.isolation">4</prop> </props> </property> <property name="mappingLocations"> <list> <value>classpath*:com/myweb/modal/hibernate/*.hbm.xml</value> </list> </property> <!-- 使用TransactionAwareDataSourceProxy管理事务与ibatis处于同一事务管理下 --> <property name="useTransactionAwareDataSource" value="true"></property> </bean> <!-- hibernateTemplate 并使用查询缓存设置--> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory"><ref bean="sessionFactory"/></property> <property name="cacheQueries" value="true"></property> </bean> <!-- mybatis-spring 配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="mapperLocations" value="classpath*:com/myweb/ibatis/mapper/*.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.myweb.dao.ibatis" /> </bean> <!-- spring transaction 事务管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:advice id="txManager" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" propagation="SUPPORTS" read-only="true" isolation="REPEATABLE_READ"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true" isolation="REPEATABLE_READ"/> <tx:method name="paging*" propagation="SUPPORTS" read-only="true" isolation="REPEATABLE_READ"/> <tx:method name="*" propagation="REQUIRED" isolation="REPEATABLE_READ" rollback-for="Throwable"/> </tx:attributes> </tx:advice> <aop:config> <aop:advisor id="txAdvisor" advice-ref="txManager" pointcut="execution(* com.myweb.*service..*(..))" order="1" /> <aop:aspect id="serviceAspect" ref="serviceLogAspect" order="2" > <aop:pointcut id="servicePointcut" expression="execution(* com.myweb.*service..*.*(..))"/> <aop:around pointcut-ref="servicePointcut" method="serviceIntercept" /> </aop:aspect> </aop:config> <!-- memcache-java --> <bean id="objectCache" class="com.myweb.service.ObjectCache"> <property name="memCachedClient"> <props> <prop key="memcached.servers">${memcached.object.servers}</prop> <prop key="memcached.weights">${memcached.object.weights}</prop> <prop key="memcached.failover">${memcached.failover}</prop> <prop key="memcached.failback">${memcached.failback}</prop> <prop key="memcached.initConn">${memcached.initConn}</prop> <prop key="memcached.minConn">${memcached.minConn}</prop> <prop key="memcached.maxConn">${memcached.maxConn}</prop> <prop key="memcached.maintSleep">${memcached.maintSleep}</prop> <prop key="memcached.nagle">${memcached.nagle}</prop> <prop key="memcached.socketTO">${memcached.socketTO}</prop> <prop key="memcached.aliveCheck">${memcached.aliveCheck}</prop> </props> </property> </bean> </beans>