1、在上篇《jbpm--api简单使用》博客中所定义的请假流程基础上整合。依次添加spring3、hibernate3、struts2,由于这里我让spring管理hibernate所以需要在项目中引入commons-pool、commons-dbcp包为数据源提供支持。在添加struts2时候别忘记添加struts-spring-plugin包,提供struts2、spring的集成。
2、修改jbpm.cfg.xml 中的部分内容,将“<import resource="jbpm.tx.hibernate.cfg.xml" />”替换为“<import resource="jbpm.tx.spring.cfg.xml" />”。将控制权交给spring。jbpm.tx.spring.cfg.xml文件在jbpm.jar的根目录下。
3、由于我这里是使用spring管理hibernate,所有需要将jbpm.hibernate.cfg.xml中的
<mapping resource="jbpm.repository.hbm.xml" />
<mapping resource="jbpm.execution.hbm.xml" />
<mapping resource="jbpm.history.hbm.xml" />
<mapping resource="jbpm.task.hbm.xml" />
<mapping resource="jbpm.identity.hbm.xml" />
信息加入到spring配置文件applicationContext.xml中,代码如下:
<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>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/tc/entity/Leaveinfo.hbm.xml</value>
<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>
4、在spring配置文件加入一个事物管理器,这里创建ProcessEngine时候要使用到,代码如下:
<bean id="myHibTxManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
5、在spring配置文件中配置SpringHelper,该bean负责创建ProcessEngine对象,代码如下:
<bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper"></bean>
6、配置ProcessEngineWireContext用来产生各种service对象,(至于为什么使用ProcessEngineWireContext定义bean我在上一篇博客中已经记录,这里就不在阐述)代码如下:
<bean id="processEngineWireContext" factory-bean="ProcessEngine"
factory- method="getProcessEngineWireContext" />
7、在Spring配置文件中利用ProcessEngineWireContext来产生各种Service对象,代码如下:
<!-- 利用processEngineWireContext产生人工任务管理服务接口 -->
<bean id="TaskService" factory-bean="processEngineWireContext" factory-method="get">
<constructor-arg>
<value type="java.lang.Class">org.jbpm.api.TaskService</value>
</constructor-arg>
</bean>
<!-- 利用processEngineWireContext产生流程执行服务对象 -->
<bean id="executionService" factory-bean="processEngineWireContext" factory-method="get">
<constructor-arg>
<value type="java.lang.Class">org.jbpm.api.ExecutionService</value>
</constructor-arg>
</bean>
<!-- 利用processEngineWireContext产生 历史记录服务对象-->
<bean id="HistoryService" factory-bean="processEngineWireContext" factory-method="get">
<constructor-arg>
<value type="java.lang.Class">org.jbpm.api.HistoryService</value>
</constructor-arg>
</bean>
<!-- 利用processEngineWireContext产生流程管理控制服务对象-->
<bean id="ManagementService" factory-bean="processEngineWireContext" factory-method="get">
<constructor-arg>
<value type="java.lang.Class">org.jbpm.api.ManagementService</value>
</constructor-arg>
</bean>
<!-- 利用processEngineWireContext产生身份认证服务接口对象-->
<bean id="IdentityService" factory-bean="processEngineWireContext" factory-method="get">
<constructor-arg>
<value type="java.lang.Class">org.jbpm.api.IdentityService</value>
</constructor-arg>
</bean>
<!-- 利用processEngineWireContext产生流程资源服务对象-->
<bean id="RepositoryService" factory-bean="processEngineWireContext" factory-method="get">
<constructor-arg>
<value type="java.lang.Class">org.jbpm.api.RepositoryService</value>
</constructor-arg>
</bean>
8、将之前写的JbpmTest类中的各个Service对象改为从spring容器中获得对象,并进行测试,代码如下:
package com.tc.test;
import java.util.Iterator;
import java.util.List;
import org.jbpm.api.Configuration;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.HistoryService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
import org.jbpm.api.history.HistoryProcessInstance;
import org.jbpm.api.task.Task;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class JbpmTest {
/**
* @param args
*/
public static void main(String[] args) {
//depaly();
//createExecute();
showHistory();
showExecution();
showTask();
}
/**
* 部署工作流程
*/
public static void depaly(){
//ProcessEngine pe=new Configuration().buildProcessEngine();
//RepositoryService rs=pe.getRepositoryService();
RepositoryService rs=(RepositoryService)new ClassPathXmlApplicationContext("applicationContext.xml").getBean("RepositoryService");
rs.createDeployment().addResourceFromClasspath("leaveHoliday.jpdl.xml").deploy();
}
/**
* 创建工作流程实例
*/
public static void createExecute(){
//leaveHoliday.170001 =null null
//leaveHoliday-1
//ProcessEngine pe=new Configuration().buildProcessEngine();
//ExecutionService es=pe.getExecutionService();
ExecutionService es=(ExecutionService)new ClassPathXmlApplicationContext("applicationContext.xml").getBean("ExecutionService");
ProcessInstance pi=es.startProcessInstanceByKey("leaveHoliday");
System.out.println("pi id:"+pi.getId()+" key:"+pi.getKey()+" name:"+pi.getName()+" =ProcessDefinitionId:"+pi.getProcessDefinitionId());
}
/**
* 展示历史流程信息
*/
public static void showHistory(){
//ProcessEngine pe=(ProcessEngine)new ClassPathXmlApplicationContext("applicationContext.xml").getBean("ProcessEngine");
//ProcessEngine pe=new Configuration().buildProcessEngine();
HistoryService hs=(HistoryService)new ClassPathXmlApplicationContext("applicationContext.xml").getBean("HistoryService");
//HistoryService hs=pe.getHistoryService();
//获取所有流程历史记录信息并输出
System.out.println("****************history********************");
List<HistoryProcessInstance> hiss=hs.createHistoryProcessInstanceQuery().list();
for (Iterator iterator = hiss.iterator(); iterator.hasNext();) {
HistoryProcessInstance historyProcessInstance = (HistoryProcessInstance) iterator
.next();
//null =leaveHoliday-1 leaveHoliday.160001active
System.out.println("key:"+historyProcessInstance.getKey()+" ProcessDefinitionId:"+historyProcessInstance.getProcessDefinitionId()
+" ProcessInstanceId:"+historyProcessInstance.getProcessInstanceId()+" state:"+historyProcessInstance.getState());
}
}
/**
* 展示当前引擎中待处理流程
*/
public static void showExecution(){
//ProcessEngine pe=new Configuration().buildProcessEngine();
//ExecutionService es=pe.getExecutionService();
ExecutionService es=(ExecutionService)new ClassPathXmlApplicationContext("applicationContext.xml").getBean("ExecutionService");
//获取某个特定的流程实例
ProcessInstance res=es.createProcessInstanceQuery().processInstanceId("leaveHoliday.170001").uniqueResult();
System.out.println("----------------------待执行的流程实例信息----------------------------");
List<ProcessInstance> pl=es.createProcessInstanceQuery().list();
for (Iterator iterator = pl.iterator(); iterator.hasNext();) {
ProcessInstance processInstance = (ProcessInstance) iterator.next();
System.out.println("id:"+processInstance.getId()+" key : "+processInstance.getKey()+" name :"+processInstance.getName()
+" ProcessDefinitionId:"+processInstance.getProcessDefinitionId()+" state:"+processInstance.getState());
}
}
/**
* 展示所有待处理任务信息
*/
public static void showTask(){
System.out.println("----------------------------task---------------------------------");
//ProcessEngine pe=new Configuration().buildProcessEngine();
//TaskService ts=pe.getTaskService();
TaskService ts=(TaskService)new ClassPathXmlApplicationContext("applicationContext.xml").getBean("TaskService");
//查找指定流程实例的任务
Task t=ts.createTaskQuery().processInstanceId("leaveHoliday.170001").uniqueResult();
System.out.println(t.getId()+":"+t.getName()+":"+t.getActivityName()+":"+t.getDescription()+":"+t.getExecutionId());
List<Task> ls=ts.createTaskQuery().list();
for (Iterator iterator = ls.iterator(); iterator.hasNext();) {
Task task = (Task) iterator.next();
System.out.println("id:"+task.getId()+" name:"+task.getName()+" getActivityName:"
+task.getActivityName()+" getDescription:"+task.getDescription()+" executionId:"+task.getExecutionId());
}
}
}
此时jbpm中的各个Service对象可以利用spring创建的,我们的jbpm和spring整合成功!下面就是利用struts2实现表单页面效果了,待续。。。。。
附带本人写的测试代码,欢迎拍砖。。。。。