流程定义文件:
<?xml version="1.0" encoding="UTF-8"?> <process key="forever" name="StateChoice" xmlns="http://jbpm.org/4.3/jpdl"> <start g="137,74,48,48" name="start1"> <transition g="-125,-17" name="to wait for response" to="wait for response"/> </start> <state g="85,219,157,52" name="wait for response"> <transition g="-56,-14" name="accept" to="submit document"/> <transition g="9,-6" name="reject" to="try again"/> </state> <state g="26,400,121,52" name="submit document"> <transition name="to end1" to="end1" g="-47,-17"/> </state> <state g="185,394,126,52" name="try again"> <transition name="to end1" to="end1" g="-47,-17"/> </state> <end name="end1" g="145,508,48,48"/> </process>
图:
测试类StateChoice:
package org.forever.jbpm;
import java.util.List;
import java.util.UUID;
import org.jbpm.api.Configuration;
import org.jbpm.api.Deployment;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.HistoryService;
import org.jbpm.api.ManagementService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class StateChoice {
ProcessEngine processEngine = null;
RepositoryService repositoryService = null;
ExecutionService executionService = null;
TaskService taskService = null;
HistoryService historyService = null;
ManagementService managementService = null;
String pid = null;
@Before
public void initialize() {
// 1.获取所有的服务
processEngine = new Configuration().buildProcessEngine();
repositoryService = processEngine.getRepositoryService();
executionService = processEngine.getExecutionService();
taskService = processEngine.getTaskService();
historyService = processEngine.getHistoryService();
managementService = processEngine.getManagementService();
}
@Test
// 部署流程
public void testDeploy() {
// 2.部署流程,获取流程定义id(格式:key-version)
String deployId = repositoryService.createDeployment()
.addResourceFromClasspath(
"org/forever/jbpm/jpdl/StateChoice.jpdl.xml").deploy();
System.out.println("流程定义id:" + deployId);
}
// 启动流程实例
@Test
public void testStart() {
// 3.启动流程实例,绑定业务key,key最好是唯一的
// starts a new process instance in the latest version of the given
// process definition
UUID uuid = UUID.randomUUID();
ProcessInstance processInstance = executionService
.startProcessInstanceByKey("forever", uuid.toString());
pid = processInstance.getId();
System.out.println(pid);
}
// 获取流程实例
public ProcessInstance getProcessInstance() {
//对于用户给的key一般是某种唯一的key,可以这样设计,给每个表的数据生成一个uuid作为唯一标识
//获取指定的流程实例
ProcessInstance processInstance = executionService
.createProcessInstanceQuery().processInstanceKey("8d0e225c-d9b6-488f-abe3-c899ea59c864")
.uniqueResult();
Assert.assertNotNull(processInstance);
System.out.println(processInstance.getId());
System.out.println(processInstance.getName());
System.out.println(processInstance.getKey());
System.out.println(processInstance.getPriority());
System.out.println(processInstance.getState());
return processInstance;
}
//让流程实例向下流转
@Test
public void testSignal() {
ProcessInstance processInstance = getProcessInstance();
Assert.assertNotNull(processInstance);
//根据具体的业务进行流转
processInstance = executionService.signalExecutionById(processInstance.getId(),"accept");
}
//让流程实例继续向下流转
@Test
public void testSignalNext() {
ProcessInstance processInstance = getProcessInstance();
Assert.assertNotNull(processInstance);
//根据具体的业务进行流转
//当流程实例的任务完成之后,数据库会自动删除该实例
processInstance = executionService.signalExecutionById(processInstance.getId());
getProcessInstance();
}
//删除流程实例
@Test
public void testDelDeployment(){
List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
for (Deployment deployment : deployments) {
System.out.println(deployment.getId());
//侧地删除与之相关的内容
repositoryService.deleteDeploymentCascade(deployment.getId());
}
}
//从头执行到尾
public void main(String[] args) {
// 1.获取所有的服务
ProcessEngine processEngine = new Configuration().buildProcessEngine();
RepositoryService repositoryService = processEngine
.getRepositoryService();
ExecutionService executionService = processEngine.getExecutionService();
// TaskService taskService = processEngine.getTaskService();
// HistoryService historyService = processEngine.getHistoryService();
// ManagementService managementService =
// processEngine.getManagementService();
// 2.部署流程,获取流程定义id(格式:key-version)
String deployId = repositoryService.createDeployment()
.addResourceFromClasspath("StateChoice.jpdl.xml").deploy();
System.out.println("流程定义id:" + deployId);
// 3.启动流程实例,绑定业务key
// starts a new process instance in the latest version of the given
// process definition
UUID uuid = UUID.randomUUID();
ProcessInstance processInstance = executionService
.startProcessInstanceByKey("forever", uuid.toString());
//String pid = processInstance.getId();// 获取绑定的key
// 流向到达了wait for response,等待状态
System.out.println(processInstance.isActive("wait for response"));
// 触发到下一个分支状态
String executionId = processInstance.findActiveExecutionIn(
"wait for response").getId();
System.out.println(executionId);
// 向指定的名字外向转移
processInstance = executionService.signalExecutionById(executionId,
"accept");
System.out.println(processInstance.isActive("submit document"));
System.out.println("是否结束:" + processInstance.isEnded());
processInstance = executionService.signalExecutionById(processInstance
.findActiveExecutionIn("submit document").getId());
System.out.println("是否结束:" + processInstance.isEnded());
// 删除流程实例
repositoryService.deleteDeploymentCascade(deployId);
}
}