首页 > 代码库 > jbpm4.4 spring整合

jbpm4.4 spring整合

jBPM-4.4与Spring集成配置比较容易,这里我使用的是Spring-2.5.6,数据库连接池使用C3P0,将相关的两个jar文件加入到CLASSPATH中。

jBPM-4.4与Spring集成的配置过程说明如下。

 

配置

 

1、修改jbpm.cfg.xml配置文件

将jbpm.cfg.xml文件中<import resource="jbpm.tx.hibernate.cfg.xml" />一行,修改为 <import resource="jbpm.tx.spring.cfg.xml" />,我们使用Spring的事务管理器,修改后配置文件内容为:

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <jbpm-configuration>  
  4.   
  5.   <import resource="jbpm.default.cfg.xml" />  
  6.   <import resource="jbpm.businesscalendar.cfg.xml" />  
  7.   <!-- <import resource="jbpm.tx.hibernate.cfg.xml" /> -->  
  8.   <import resource="jbpm.tx.spring.cfg.xml" />  
  9.   <import resource="jbpm.jpdl.cfg.xml" />  
  10.   <import resource="jbpm.bpmn.cfg.xml" />  
  11.   <import resource="jbpm.identity.cfg.xml" />  
  12.   
  13.   <!-- Job executor is excluded for running the example test cases. -->  
  14.   <!-- To enable timers and messages in production use, this should be included. -->  
  15.   <!-- 
  16.   <import resource="jbpm.jobexecutor.cfg.xml" /> 
  17.   -->  
  18.   
  19. </jbpm-configuration>  

 

2、修改Spring的applicationContext.xml配置文件

配置文件内容如下所示:

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  7.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
  8.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">  
  9.   
  10.     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
  11.         destroy-method="close">  
  12.         <property name="driverClass" value=http://www.mamicode.com/"com.mysql.jdbc.Driver" />  
  13.         <property name="jdbcUrl" value=http://www.mamicode.com/"jdbc:mysql://localhost:3306/jbpmdb" />  
  14.         <property name="user" value=http://www.mamicode.com/"jbpm" />  
  15.         <property name="password" value=http://www.mamicode.com/"jbpm" />  
  16.         <property name="initialPoolSize" value=http://www.mamicode.com/"3" />  
  17.         <property name="minPoolSize" value=http://www.mamicode.com/"3" />  
  18.         <property name="maxPoolSize" value=http://www.mamicode.com/"50" />  
  19.         <property name="maxIdleTime" value=http://www.mamicode.com/"600" />  
  20.         <property name="maxStatements" value=http://www.mamicode.com/"100" />  
  21.         <property name="acquireIncrement" value=http://www.mamicode.com/"3" />  
  22.     </bean>  
  23.   
  24.     <bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper" />  
  25.   
  26.     <bean id="processEngine" factory-bean="springHelper"  
  27.         factory-method="createProcessEngine" />  
  28.   
  29.     <!--Hibernate SessionFatory -->  
  30.     <bean id="sessionFactory"  
  31.         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">  
  32.         <property name="dataSource" ref="dataSource" />  
  33.         <property name="mappingResources">  
  34.             <list>  
  35.                 <value>jbpm.repository.hbm.xml</value>  
  36.                 <value>jbpm.execution.hbm.xml</value>  
  37.                 <value>jbpm.history.hbm.xml</value>  
  38.                 <value>jbpm.task.hbm.xml</value>  
  39.                 <value>jbpm.identity.hbm.xml</value>  
  40.             </list>  
  41.         </property>  
  42.         <property name="hibernateProperties">  
  43.             <props>  
  44.                 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>  
  45.                 <prop key="hibernate.hbm2ddl.auto">create-drop</prop>  
  46.                 <prop key="hibernate.show_sql">true</prop>  
  47.             </props>  
  48.         </property>  
  49.     </bean>  
  50.   
  51.     <!-- Transaction Manager -->  
  52.     <bean id="transactionManager"  
  53.         class="org.springframework.orm.hibernate3.HibernateTransactionManager">  
  54.         <property name="sessionFactory" ref="sessionFactory" />  
  55.         <property name="dataSource" ref="dataSource" />  
  56.     </bean>     
  57.   
  58. </beans>  

这里使用MySQL作为流程数据库,需要在dataSource的bean中配置,其他的配置内容非常容易理解。这里面,我们最终的目标是通过 Spring的IOC容器来管理jBPM引擎的创建,即在应用中获取到一个org.jbpm.api.ProcessEngine实例即可,在一个应用中 通常只有一个流程引擎实例,然后通过这个实例就可以获取到流程提供的6种基本服务:

org.jbpm.api.RepositoryService
org.jbpm.api.ExecutionService
org.jbpm.api.ManagementService
org.jbpm.api.TaskService
org.jbpm.api.HistoryService
org.jbpm.api.IdentityService
一般情况下,在开发中不要将org.jbpm.api.ProcessEngine实例暴露给实际开发者,而只是暴露出引擎提供的服务即可。

 

测试验证

 

我们测试用例的流程定义如图所示:

技术分享

 对应的流程定义文件内容如下所示:

[xhtml] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <process name="MyExample" xmlns="http://jbpm.org/4.4/jpdl">  
  4.   
  5.     <on event="start">  
  6.         <event-listener class="com.umpay.workflow.jbpm.listener.StartupListener">  
  7.             <field name="status">  
  8.                 <string value=http://www.mamicode.com/"startup check..."/>  
  9.             </field>  
  10.         </event-listener>  
  11.     </on>  
  12.   
  13.     <start g="354,6,48,48">  
  14.         <transition g="-102,-16" name="to start decision" to="start decision">  
  15.             <event-listener class="com.umpay.workflow.jbpm.listener.InitializationListener">  
  16.                 <field name="initialized">  
  17.                     <string value=http://www.mamicode.com/"initialize..."/>  
  18.                 </field>  
  19.             </event-listener>  
  20.         </transition>  
  21.     </start>  
  22.   
  23.     <decision g="413,107,48,48" name="start decision">  
  24.         <handler class="com.umpay.workflow.jbpm.decision.StartDecision" />  
  25.         <transition g="-83,-17" name="to check user" to="check user"/>  
  26.         <transition g="-48,-25" name="to error" to="error"/>  
  27.     </decision>  
  28.   
  29.     <state g="216,183,104,52" name="check user">  
  30.         <on event="start">  
  31.             <event-listener class="com.umpay.workflow.jbpm.listener.CheckUserListener">  
  32.                 <field name="state">  
  33.                     <string value=http://www.mamicode.com/"Check user state..."/>  
  34.                 </field>  
  35.             </event-listener>  
  36.         </on>  
  37.         <transition g="-128,-8" name="to decide check user" to="decide check user result"/>  
  38.     </state>  
  39.   
  40.     <decision g="359,271,48,48" name="decide check user result">  
  41.         <handler class="com.umpay.workflow.jbpm.decision.CheckUserDecision" />  
  42.         <transition g="-95,-17" name="to check mobile" to="check mobile"/>  
  43.         <transition g="-41,14" name="to error" to="error"/>  
  44.     </decision>  
  45.   
  46.     <state g="215,349,104,52" name="check mobile">  
  47.         <on event="start">  
  48.             <event-listener class="com.umpay.workflow.jbpm.listener.CheckMobileListener">  
  49.                 <field name="online">  
  50.                     <string value=http://www.mamicode.com/"Check mobile number..."/>  
  51.                 </field>  
  52.             </event-listener>  
  53.         </on>  
  54.         <transition g="-146,-6" name="to decide check mobile" to="decide check mobile result"/>  
  55.     </state>  
  56.   
  57.     <decision g="337,435,48,48" name="decide check mobile result">  
  58.         <handler class="com.umpay.workflow.jbpm.decision.CheckMobileDecision" />  
  59.         <transition g="-98,-21" name="to do transaction" to="do transaction"/>  
  60.         <transition g="-39,9" name="to error" to="error"/>  
  61.     </decision>  
  62.   
  63.     <state g="217,516,112,52" name="do transaction">  
  64.         <on event="start">  
  65.             <event-listener class="com.umpay.workflow.jbpm.listener.DoTransactionListener">  
  66.                 <field name="operation">  
  67.                     <string value=http://www.mamicode.com/"Do transaction..."/>  
  68.                 </field>  
  69.             </event-listener>  
  70.         </on>  
  71.         <transition g="-101,-17" name="to last decision" to="last decision"/>  
  72.     </state>  
  73.   
  74.     <decision g="312,599,48,48" name="last decision">  
  75.         <handler class="com.umpay.workflow.jbpm.decision.DoTransactionDecision" />  
  76.         <transition g="-41,-17" name="to end" to="end"/>  
  77.         <transition g="-41,-1" name="to error" to="error"/>  
  78.     </decision>  
  79.   
  80.     <end-error g="475,675,48,48" name="error"/>  
  81.     <end g="242,675,48,48" name="end">  
  82.         <on event="start">  
  83.             <event-listener class="com.umpay.workflow.jbpm.listener.ShutdownHookListener">  
  84.                 <field name="saveThings">  
  85.                     <string value=http://www.mamicode.com/"S:shutdown..."/>  
  86.                 </field>  
  87.             </event-listener>  
  88.         </on>  
  89.     </end>  
  90.   
  91. </process>  

 

基于该流程定义,实现的测试用例如下所示:

[java] view plaincopy
    1. package com.umpay.workflow.jbpm.process;  
    2.   
    3. import junit.framework.TestCase;  
    4.   
    5. import org.jbpm.api.Execution;  
    6. import org.jbpm.api.ProcessEngine;  
    7. import org.jbpm.api.ProcessInstance;  
    8. import org.springframework.context.support.ClassPathXmlApplicationContext;  
    9.   
    10. public class MyProcessWithSpringTest extends TestCase {  
    11.     private ProcessEngine processEngine;  
    12.     String deploymentId;  
    13.   
    14.     protected void setUp() throws Exception {  
    15.         super.setUp();  
    16.         ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(  
    17.                 "applicationContext.xml");  
    18.         ctx.start();  
    19.   
    20.         this.processEngine = (ProcessEngine) ctx.getBean("processEngine");  
    21.         deploymentId = this.processEngine  
    22.                 .getRepositoryService()  
    23.                 .createDeployment()  
    24.                 .addResourceFromClasspath(  
    25.                         "com/umpay/workflow/jbpm/process/process.jpdl.xml")  
    26.                 .deploy();  
    27.     }  
    28.   
    29.     protected void tearDown() throws Exception {  
    30.         this.processEngine.getRepositoryService().deleteDeploymentCascade(  
    31.                 deploymentId);  
    32.         super.tearDown();  
    33.     }  
    34.   
    35.     public void testMyProcess() {  
    36.         // start a process instance  
    37.         ProcessInstance processInstance = this.processEngine  
    38.                 .getExecutionService().startProcessInstanceByKey("MyExample");  
    39.         // state : check user  
    40.         assertEquals("check user", processInstance.findActiveActivityNames()  
    41.                 .iterator().next());  
    42.         Execution execution = processInstance  
    43.                 .findActiveExecutionIn("check user");  
    44.   
    45.         // state : check mobile  
    46.         processInstance = this.processEngine.getExecutionService()  
    47.                 .signalExecutionById(execution.getId());  
    48.         execution = processInstance.findActiveExecutionIn("check mobile");  
    49.         assertEquals("check mobile", processInstance.findActiveActivityNames()  
    50.                 .iterator().next());  
    51.   
    52.         // state : do transaction  
    53.         processInstance = this.processEngine.getExecutionService()  
    54.                 .signalExecutionById(execution.getId());  
    55.         execution = processInstance.findActiveExecutionIn("do transaction");  
    56.         assertEquals("do transaction", processInstance  
    57.                 .findActiveActivityNames().iterator().next());  
    58.   
    59.         processInstance = this.processEngine.getExecutionService()  
    60.                 .signalExecutionById(execution.getId());  
    61.         assertEquals(true, processInstance.findActiveActivityNames().isEmpty());  
    62.   
    63.         // ended  
    64.         assertEquals(Execution.STATE_ENDED, processInstance.getState());  
    65.     }  

jbpm4.4 spring整合