Activiti 流程实例、任务的执行
2019-10-09 本文已影响0人
__元昊__
09【掌握】流程实例、任务的执行
1.流程图
image.png
2.部署流程定义
/**
* 流程部署
* 使用addClasspathResource
*/
@Test
public void deployProcessDefinition() {
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deploy = repositoryService.createDeployment().addClasspathResource("HelloWorld.bpmn")
.addClasspathResource("HelloWorld.png")
.name("请假流程004")
.deploy();
System.out.println("部署成功:流程部署ID:"+deploy.getId()+" 流程部署名称:"+deploy.getName());
}
3.启动流程
/**
* 启动流程
*/
@Test
public void startProcess() {
//1,得到启动流程的service
RuntimeService runtimeService = this.processEngine.getRuntimeService();
// String processDefinitionId="HelloWorld:1:4";
// runtimeService.startProcessInstanceById(processDefinitionId);
String processDefinitionKey="HelloWorld";
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);//启动的永远是最新版本的流程
System.out.println("流程启动成功");
System.out.println("部署ID:"+processInstance.getDeploymentId());//指act_re_deployment的id
System.out.println("流程定义ID:"+processInstance.getProcessDefinitionId());//act_re_procdef里面的id
System.out.println("流程实例ID:"+processInstance.getId());//act_ru_execution这个表里面的id
}
4.查询个人任务
//4,查询个人任务
@Test
public void queryMyPersonTask() {
TaskService taskService = this.processEngine.getTaskService();
String assignee="张三";
List<Task> list = taskService.createTaskQuery()
//条件
.taskAssignee(assignee) //根据任务的办理人查询
// .deploymentId(deploymentId)根据部署ID查询任务
// .deploymentIdIn(deploymentIds)根据部署ID的集合查询
// .executionId(executionId)根据执行实例查询
// .processInstanceId(processInstanceId)//根据流程实例ID查询
// .processInstanceBusinessKey(processInstanceBusinessKey)//根据业务ID查询
// .processDefinitionId(processDefinitionId)//根据流程定义ID查询
// .processDefinitionKey(processDefinitionKey)根据流程定义的KEY查询
// .processDefinitionKeyIn(processDefinitionKeys)
// .processDefinitionKeyLike(processDefinitionKeyLike)
// .processDefinitionKeyLikeIgnoreCase(processDefinitionKeyLikeIgnoreCase)
// .processDefinitionName(processDefinitionName)
//排序
// .orderByExecutionId()
// .orderByProcessDefinitionId()
// .orderByProcessInstanceId()
//结果集
.list();
for (Task task : list) {
System.out.println("任务ID:"+task.getId());
System.out.println("执行实例ID:"+task.getExecutionId());
System.out.println("流程实例ID:"+task.getProcessInstanceId());
System.out.println("任务名称:"+task.getName());
System.out.println("任务定义的Key:"+task.getTaskDefinitionKey());
System.out.println("任务办理人:"+task.getAssignee());
System.out.println("#####################");
}
}
5.办理任务
// 5,办理任务方式1 已知任务ID 2505
@Test
public void completeTask() {
String taskId="2505";
TaskService taskService = this.processEngine.getTaskService();
taskService.complete(taskId); //直接完成任务
// taskService.complete(taskId, variables);//完成任务并设置流程变量
System.out.println("任务完成成功");
}
6.查询流程状态(判断流程正在执行,还是结束)
// 6,查询流程状态(判断流程正在执行,还是结束)
@Test
public void isProcessOver() {
//1,已知流程实例ID
RuntimeService runtimeService = this.processEngine.getRuntimeService();
String processInstanceId="2501";
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
if(null!=processInstance) {
System.out.println("流程没有结束");
}else {
System.out.println("流程已结束");
}
//2,已知执行实例ID
String executionId="5002";
Execution execution = runtimeService.createExecutionQuery().executionId(executionId).singleResult();
if(null!=execution) {
System.out.println("流程没有结束");
}else {
System.out.println("流程已结束");
}
//3,已知任务ID
String taskId="5005";
TaskService taskService = this.processEngine.getTaskService();
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
String processInstanceId2 = task.getProcessInstanceId();
String executionId2 = task.getExecutionId();
//可以使用上面的两种方法再去查询
}
7.附加功能,查询历史任务act_hi_taskinst
// 7,附加功能,查询历史任务 act_hi_taskinst
@Test
public void queryHistoryTask() {
HistoryService historyService = this.processEngine.getHistoryService();
List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().list();
for (HistoricTaskInstance hti : list) {
System.out.println("任务ID:"+hti.getId());
System.out.println("办理人:"+hti.getAssignee());
System.out.println("执行实例ID:"+hti.getExecutionId());
System.out.println("流程实例ID:"+hti.getProcessInstanceId());
System.out.println("流程定义ID:"+hti.getProcessDefinitionId());
System.out.println("任务名称 :"+hti.getName());
System.out.println("任务定义的key:"+hti.getTaskDefinitionKey());
System.out.println("任务开始时间:"+hti.getStartTime());
System.out.println("任务办结时间:"+hti.getEndTime());
System.out.println("任务耗时:"+hti.getDurationInMillis());
System.out.println("#########################");
}
}
8.附加功能,查询历史流程实例
// 8,附加功能,查询历史流程实例
@Test
public void queryHistoryProcessInstance() {
HistoryService historyService = this.processEngine.getHistoryService();
List<HistoricProcessInstance> list = historyService.createHistoricProcessInstanceQuery().list();
for (HistoricProcessInstance hti : list) {
System.out.println("流程实例ID:"+hti.getId());
System.out.println("流程定义ID:"+hti.getProcessDefinitionId());
System.out.println("任务名称 :"+hti.getName());
System.out.println("流程启动时间:"+hti.getStartTime());
System.out.println("流程结束时间:"+hti.getEndTime());
System.out.println("整个流程耗时耗时:"+hti.getDurationInMillis());
System.out.println("#########################");
}
}