最近一直在学习JBPM方面的东西,下面是借鉴网上例子完成的JBPM4.3整合Spring2.5.6的DEMO。
JBPM4.3导入到lib包:
activation.jar antlr.jar commons-collections.jar dom4j.jar hibernate-core.jar javassist.jar jboss-j2ee.jar jbpm.jar juel.jar junit.jar livetribe-jsr223.jar mail.jar slf4j-api.jar slf4j-jdk14.jar
Spring2.5导入的包:
aspectjrt.jar aspectjweaver.jar cglib-nodep-2.1_3.jar common-annotations.jar commons-dbcp.jar commons-logging.jar commons-pool.jar mysql-connector-java-5.1.8-bin.jar spring.jar
流程图:见附件
jbpm测试类:
package com.ceopen.appServer; import java.util.HashMap; import java.util.List; import java.util.Map; import org.jbpm.api.Execution; import org.jbpm.api.ProcessDefinition; import org.jbpm.api.ProcessInstance; import org.jbpm.api.history.HistoryProcessInstance; import org.jbpm.api.history.HistoryTask; import org.jbpm.api.task.Task; import org.jbpm.test.JbpmTestCase; public class AppServerTest extends JbpmTestCase { String deploymentId; @Override protected void setUp() throws Exception { // JBPM4单元测试约定。在setUp中,执行流程定义的部署工作 super.setUp(); // 部署流程 deploymentId = repositoryService.createDeployment() .addResourceFromClasspath("appServer.jpdl.xml").deploy(); } public void testAppServer() { // 查询已发布流程定义 List<ProcessDefinition> list = repositoryService .createProcessDefinitionQuery().list(); for (ProcessDefinition pd : list) { System.out.println("\n流程定义ID:" + pd.getId() + "\t名称:" + pd.getName() + "\t版本:" + pd.getVersion() + "\t部署ID:" + deploymentId + "\n"); Map<String, Object> map = new HashMap<String, Object>(); map.put("owner", "chenminjie"); // 启动流程 ProcessInstance processInstance = executionService .startProcessInstanceByKey("app", map, "IDC20110615001"); // 验证流程是否启动成功 assertNotNull("流程实例为空,发起流程不成功!", processInstance); // 验证该流程实例的业务健(工单号)是否正确 assertEquals("IDC20110615001", processInstance.getKey()); // 用户自定义任务往下一个节点推进 Execution execution = processInstance.findActiveExecutionIn("测试"); executionService.signalExecutionById(execution.getId()); // 验证执行ID是否等于过程实例ID assertEquals("执行ID不等于过程实例ID", execution.getId(), processInstance.getId()); // 验证当前活动节点是否在‘填写工单’ assertActivityActive(execution.getId(), "填写工单"); List<Task> taskList = taskService.findPersonalTasks("chenminjie"); for (Task task : taskList) { System.out.println("\n任务定义ID:" + task.getId() + "\t名称:" + task.getName() + "\t分配人:" + task.getAssignee() + "\texecutionID:" + execution.getId() + "\n"); // 添加流程变量 map.put("number", 2); map.put("use", "eoss使用"); map.put("engineRoom", "联通亦庄机房"); map.put("managerEmail", "[email protected]"); map.put("majordomoEmail", "[email protected]"); map.put("managerReason", ""); map.put("majordomoReason", ""); map.put("assignReason", ""); map.put("userReason", ""); map.put("assignPerson", ""); // 完成填写工单节点 taskService.completeTask(task.getId(), "提交", map); } // 验证节点是否到达‘经理审批’ assertActivityActive(execution.getId(), "经理审核"); taskList = taskService.findPersonalTasks("sungang"); for (Task task : taskList) { System.out.println("\n任务定义ID:" + task.getId() + "\t名称:" + task.getName() + "\t分配人:" + task.getAssignee() + "\texecutionID:" + execution.getId() + "\n"); // 添加流程变量 map.put("owner", "sungang"); map.put("managerReason", "同意"); // 完成经理审批节点 taskService.completeTask(task.getId(), "批准", map); } // 验证节点是否到达‘总监审核’ assertActivityActive(execution.getId(), "总监审核"); try { Thread.sleep(2046); } catch (InterruptedException e) { e.printStackTrace(); } taskList = taskService.findPersonalTasks("dongyue"); for (Task task : taskList) { System.out.println("\n任务定义ID:" + task.getId() + "\t名称:" + task.getName() + "\t分配人:" + task.getAssignee() + "\texecutionID:" + execution.getId() + "\n"); // 添加流程变量 map.put("owner", "dongyue"); map.put("majordomoReason", "同意"); // 完成总监审核节点 taskService.completeTask(task.getId(), "批准", map); } // 验证节点是否到达‘分配服务器’ assertActivityActive(execution.getId(), "分配服务器"); // assertExecutionEnded(processInstance.getId()); taskList = taskService.findPersonalTasks("zhutiepeng"); for (Task task : taskList) { System.out.println("\n任务定义ID:" + task.getId() + "\t名称:" + task.getName() + "\t分配人:" + task.getAssignee() + "\texecutionID:" + execution.getId() + "\n"); // 添加流程变量 map.put("owner", "chenminjie"); map.put("assignReason", "分配IDC-D-1983"); map.put("result", "批准");// 如果判断节点的参数result给的值不对,则流程自动结束 // 完成分配服务器节点 taskService.completeTask(task.getId(), map); } // 验证节点是否到达‘用户确认’ assertActivityActive(execution.getId(), "用户确认"); // 如果判断节点的参数result给的值为“执行”,流程自动结束 // assertExecutionEnded(processInstance.getId()); taskList = taskService.findPersonalTasks("chenminjie"); for (Task task : taskList) { System.out.println("\n任务定义ID:" + task.getId() + "\t名称:" + task.getName() + "\t分配人:" + task.getAssignee() + "\texecutionID:" + execution.getId() + "\n"); // 添加流程变量 map.put("owner", "chenminjie"); map.put("userReason", "确认执行完毕"); map.put("result", "确认"); // 完成用户确认节点 taskService.completeTask(task.getId(), "确认", map); } // 流程已结束,则excution为null // assertActivityActive(execution.getId(), "结束工单"); // 验证节点是否到达‘结束工单’,结束流程 assertExecutionEnded(processInstance.getId()); } // 以下为流程历史部分 // 历史流程实例总数 long hpiCount = historyService.createHistoryProcessInstanceQuery() .count(); assertEquals(1, hpiCount); // 获取流程定义ID号为“app-1”的所有历史流程实例 List<HistoryProcessInstance> historyProcessInstances = historyService .createHistoryProcessInstanceQuery() .processDefinitionId("app-1").list(); for (HistoryProcessInstance hpi : historyProcessInstances) { System.out.println("\n结束流程时最后到达的节点名称:" + hpi.getEndActivityName() + "\t流程实例业务健:" + hpi.getKey() + "\t流程实例ID:" + hpi.getProcessInstanceId() + "\t流程实例toString():" + hpi.toString() + "\t流程实例持续时间:" + hpi.getDuration() + "\t流程实例开始:" + hpi.getStartTime() + "\t流程实例结束时间:" + hpi.getEndTime() + "\t流程实例状态:" + hpi.getState() + "\n"); } // 获取某个流程定义下的所有节点的平均执行时间 java.util.Map<java.lang.String, java.lang.Long> avgActExcMap = historyService .avgDurationPerActivity("app-1"); System.out.println(avgActExcMap); // 获取某个流程定义下transition(辨识流转的线)被选择的次数 java.util.Map<java.lang.String, java.lang.Integer> avgChoise = historyService .choiceDistribution("app-1", "用户确认"); System.out.println(avgChoise); // 获取'chenminjie'历史任务总数 long taskCount = historyService.createHistoryTaskQuery() .assignee("chenminjie").count(); assertEquals(2, taskCount); // 获取所有历史流程任务实例 List<HistoryTask> historyTasks = historyService .createHistoryTaskQuery().list(); for (HistoryTask ht : historyTasks) { System.out.println("\n任务分配人:" + ht.getAssignee() + "\t任务持续时间 :" + ht.getDuration() + "\t任务执行id :" + ht.getExecutionId() + "\t任务id :" + ht.getId() + "\t任务流出 :" + ht.getOutcome() + "\t任务状态:" + ht.getState() + "\t任务toString() :" + ht.toString() + "\t任务创建时间:" + ht.getCreateTime() + "\t任务结束时间 :" + ht.getEndTime() + "\n"); } } @Override protected void tearDown() throws Exception { // JBPM4单元测试约定。在tearDown中清除所有流程定义及相关所有资源 repositoryService.deleteDeploymentCascade(deploymentId); super.tearDown(); } }
Spring结合Jbpm4的测试类
package com.ceopen.appServer; import java.util.List; import org.jbpm.api.ExecutionService; import org.jbpm.api.ProcessDefinition; import org.jbpm.api.ProcessEngine; import org.jbpm.api.ProcessInstance; import org.jbpm.api.RepositoryService; import org.jbpm.api.TaskService; import org.jbpm.api.task.Task; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppServer { ProcessEngine processEngine; RepositoryService repositoryService; ExecutionService executionService; TaskService taskService; // 部署流程定义 public String deployProcDef(String path) { String deploymentId = null; deploymentId = repositoryService.createDeployment() .addResourceFromClasspath(path).deploy(); return deploymentId; } public void setProcessEngine(ProcessEngine processEngine) { this.processEngine = processEngine; } public void setRepositoryService(RepositoryService repositoryService) { this.repositoryService = repositoryService; } public void setExecutionService(ExecutionService executionService) { this.executionService = executionService; } public void setTaskService(TaskService taskService) { this.taskService = taskService; } public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "applicationContext.xml"); AppServer appServer = (AppServer) applicationContext .getBean("appServer"); appServer.deployProcDef("appServer.jpdl.xml"); } }
其他配置文件见appServer附件,有什么建议或者运行不了的可以一起讨论,呵呵