准备工作:开启mysql数据库服务并已生成jbpm数据库。
1、建立工程并在工程中建立工作流程,流程图如下:
流程定义的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<process name="leaveHoliday" xmlns="http://jbpm.org/4.3/jpdl">
<start g="120,0,92,52" name="start1">
<transition name="to 申请" to="申请"/>
</start>
<task g="96,96,92,52" name="申请">
<transition g="-53,-17" name="to 经理审批" to="经理审批"/>
</task>
<decision expr="#{content}" g="120,324,92,52" name="exclusive1">
<transition g="-47,-17" name="to end1" to="end1"/>
<transition g="349,348:-53,-17" name="to 老板审批" to="老板审批"/>
</decision>
<end g="120,504,48,48" name="end1"/>
<task g="300,384,92,52" name="老板审批">
<transition g="351,528:-47,-17" name="to end1" to="end1"/>
</task>
<task g="96,216,92,52" name="经理审批">
<transition g="-83,-17" name="to exclusive1" to="exclusive1"/>
<transition g="313,243;314,120:-53,-17" name="to 申请" to="申请"/>
</task>
</process>
2、编写测试代码,如下:
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;
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();
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();
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=new Configuration().buildProcessEngine();
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();
//获取某个特定的流程实例
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();
//查找指定流程实例的任务
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());
}
}
}