流程实例测试:
import java.io.InputStream;
import java.util.List;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
/**
*
* 流程实例管理
*
* 使用API的步骤:
* 1.创建流程引擎ProcessEngine
* 2.获取相关服务对象实例
* 3.使用服务对象相关方法完成流程操作
* @author LANHD
*
*/
public class ProcessInstanceTest {
// 创建流程引擎ProcessEngine
private ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
// 获取相关服务对象实例
private RepositoryService repositoryService=processEngine.getRepositoryService();
private TaskService taskService=processEngine.getTaskService();
private RuntimeService runtimeService=processEngine.getRuntimeService();
//部署流程
@Test
public void deployProcess() throws Exception{
//
DeploymentBuilder deploymentBuilder=repositoryService.createDeployment();
// 2.添加发布的资源文件(“流程规则文件和流程图片”)
InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("LeaveFlow2.bpmn");
deploymentBuilder
.name("请假流程")
.addInputStream("LeaveFlow2.bpmn", in); //添加规则文件
// 3.完成发布:使用deploy()发布流程
deploymentBuilder.deploy();
}
// 1.启动流程
@Test
public void startProcess() throws Exception{
// 按照指定流程的ID启动一个流程
//String processDefinitionId="LeaveFlow:1:104";
//ProcessInstance processInstance= runtimeService.startProcessInstanceById(processDefinitionId);
// 使用流程定义的KEY启动流程,使用这种方式永远启动的是当前key下版本最高的一个规则
String processDefinitionKey="LeaveFlow";
ProcessInstance processInstance= runtimeService.startProcessInstanceByKey(processDefinitionKey);
System.out.println("id:"+processInstance.getId()+",activityId:"+processInstance.getActivityId());
}
// 2.查看任务(私有任务assignee/公有任务candidate)
//2.1查看私有任务
@Test
public void queryMyTask() throws Exception{
String assignee="boss";
List<Task> tasks=taskService.createTaskQuery()
//过滤条件
.taskAssignee(assignee)
// .taskDefinitionKey(key)
// 分页条件
// .listPage(firstResult, maxResults)
// 排序条件
.orderByTaskCreateTime().desc()
.list();
System.out.println("========["+assignee+"]私有任务列表=======================");
for(Task task:tasks){
System.out.println("id:"+task.getId()+",name:"+task.getName()+",create_time:"+task.getCreateTime());
}
}
//2.2查看公有任务
@Test
public void queryCommTask() throws Exception{
String candidateUser="boss";
List<Task> tasks=taskService.createTaskQuery()
//过滤条件
.taskCandidateUser(candidateUser)
// .taskDefinitionKey(key)
// 分页条件
// .listPage(firstResult, maxResults)
// 排序条件
.orderByTaskCreateTime().desc()
.list();
System.out.println("========["+candidateUser+"]可接任务列表=======================");
for(Task task:tasks){
System.out.println("id:"+task.getId()+",name:"+task.getName()+",create_time:"+task.getCreateTime());
}
}
// 3.认领任务
@Test
public void claimTask() throws Exception{
String taskId="1002";
String userId="boss";
taskService.claim(taskId, userId);
}
// 4.办理任务
@Test
public void complete(){
String taskId="1102";
taskService.complete(taskId);
}
// 5.查看流程状态
@Test
public void queryProcessStatue() throws Exception {
String pdId = "401";
ProcessInstance processInstance = runtimeService
.createProcessInstanceQuery().processInstanceId(pdId)
.singleResult();
System.out.println("pid:"+processInstance.getId()+",activityId:"+processInstance.getActivityId());
}
}