1、在myeclipse中建立工程
2、添加jbpm所需的jar包支持,这里我用到的是前面提到的在myeclipse中建立用户库的形式
3、建立jpdl定义文件,在工程上右键---->new-----> other----->jboss jbpm--->jbpm 4 process definition,完成后如下图:
jpdl文件的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<process name="holiday" xmlns="http://jbpm.org/4.3/jpdl">
<start name="start1" g="38,16,48,48">
<transition name="to state1" to="state1" g="-59,-17"/>
</start>
<state name="state1" g="16,96,92,52">
<transition name="to task1" to="task1" g="-53,-17"/>
</state>
<task name="task1" assignee="wang01" g="16,180,92,52">
<transition name="to end1" to="end1" g="-47,-17"/>
</task>
<end name="end1" g="38,264,48,48"/>
</process>
4、建立java类,发布流程并开启流程实例执行。
package com.tc.test;
import java.util.Iterator;
import java.util.List;
import org.jbpm.api.Configuration;
import org.jbpm.api.Execution;
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 HolidayTest {
/**
* @param args
*/
public static void main(String[] args) {
//replayHoliday();
executeHoliday();
}
public static void replayHoliday(){
//获得流程引擎对象
ProcessEngine pe=Configuration.getProcessEngine();
//获得流程资源对象
RepositoryService rs=pe.getRepositoryService();
//向流程资源对象中添加jpdl xml并发布流程到jbpm数据库中
String id=rs.createDeployment().addResourceFromClasspath("holiday.jpdl.xml").deploy();
//在控制台下输出已部署的流程id
System.out.println("流程已经部署:"+id);
}
public static void executeHoliday(){
//获得流程引擎对象
ProcessEngine pe=Configuration.getProcessEngine();
//获得流程执行服务接口对象
ExecutionService es=pe.getExecutionService();
//根据流程名字获取流程实例对象
ProcessInstance pi=es.startProcessInstanceByKey("holiday");
//输出流程实例id
System.out.println("新建流程实例的id为:"+pi.getId());
//查找name为state1的state
Execution state1=pi.findActiveExecutionIn("state1");
System.out.println("state1:"+state1.getName());
//通过流程执行对象把该state执行
es.signalExecutionById(state1.getId());
//重新从数据库中获取指定id的流程实例对象
pi=es.findProcessInstanceById(pi.getId());
//查找name为task1的任务
Execution task=pi.findActiveExecutionIn("task1");
System.out.println("task name:"+task.getName());
//获得任务服务接口对象
TaskService ts=pe.getTaskService();
//根据用户名wang01获得该用户的所有应处理任务中的第一个任务
// 这里并不一定是当前流程实例应该处理的Task
Task toDo=ts.findPersonalTasks("wang01").get(0);
System.out.println("ExecutionId"+toDo.getExecutionId());
System.out.println("do id:"+toDo.getId());
System.out.println("to do Name:"+toDo.getName());
//处理该任务
//注:这里处理的任务并不一定是我们刚刚创立的流程实例所应处理的任务,可能是其他流程实例未处理的任务
//因为我们上面代码是:ts.findPersonalTasks("wang01").get(0)
ts.completeTask(toDo.getId());
//获得历史服务接口对象
HistoryService hs=pe.getHistoryService();
//获取所有流程历史记录信息并输出
List<HistoryProcessInstance> hiss=hs.createHistoryProcessInstanceQuery().list();
for (Iterator iterator = hiss.iterator(); iterator.hasNext();) {
HistoryProcessInstance historyProcessInstance = (HistoryProcessInstance) iterator
.next();
System.out.println(historyProcessInstance.getKey()+"====="+historyProcessInstance.getProcessDefinitionId()
+"======"+historyProcessInstance.getProcessInstanceId()+historyProcessInstance.getState());
}
}
}