一 代码
/**
* Copyright (C), 2020-2020, 软件公司
* FileName: ProcessInstanceQuery.java
* Author: cakin
* Date: 2020/5/3
* Description: 查询历史流程实例
*/
package com.syc.activiti.chapter10;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.HistoryService;
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.history.HistoricProcessInstance;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
/**
* @className: ProcessInstanceQuery
* @description: 查询历史流程实例
* @date: 2020/5/3
* @author: cakin
*/
@Slf4j
public class ProcessInstanceQuery {
public static void main( String[] args ) throws Exception {
// 创建流程引擎
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 得到流程存储服务实例
RepositoryService repositoryService = engine.getRepositoryService();
// 得到运行时服务组件
RuntimeService runtimeService = engine.getRuntimeService();
// 得到历史服务组件
HistoryService historyService = engine.getHistoryService();
TaskService taskService = engine.getTaskService();
// 部署流程文件
Deployment deploy = repositoryService.createDeployment()
.addClasspathResource("bpmn/chapter10/ProcessInstanceQuery.bpmn").deploy();
// 查询流程定义
ProcessDefinition define = repositoryService
.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
// 开始流程
ProcessInstance pi1 = runtimeService.startProcessInstanceByKey("testProcess", "businessKey1");
ProcessInstance pi2 = runtimeService.startProcessInstanceByKey("testProcess", "businessKey2");
// 完成第一条流程
Task task = taskService.createTaskQuery().processInstanceId(pi1.getId()).singleResult();
taskService.complete(task.getId());
task = taskService.createTaskQuery().processInstanceId(pi1.getId()).singleResult();
taskService.complete(task.getId());
/******************************************************************************************************************
mysql> select ID_ , PROC_INST_ID_ , BUSINESS_KEY_ , PROC_DEF_ID_ , START_TIME_ , END_TIME_ , DURATION_ , START_USER_ID_ , START_ACT_ID_ , END_ACT_ID_ from act_hi_procinst;
+--------+---------------+---------------+----------------------+-------------------------+-------------------------+-----------+----------------+---------------+-------------+
| ID_ | PROC_INST_ID_ | BUSINESS_KEY_ | PROC_DEF_ID_ | START_TIME_ | END_TIME_ | DURATION_ | START_USER_ID_ | START_ACT_ID_ | END_ACT_ID_ |
+--------+---------------+---------------+----------------------+-------------------------+-------------------------+-----------+----------------+---------------+-------------+
| 235005 | 235005 | businessKey1 | testProcess:4:235004 | 2020-05-03 19:16:44.068 | 2020-05-03 19:16:44.581 | 513 | NULL | startevent1 | endevent1 |
| 235010 | 235010 | businessKey2 | testProcess:4:235004 | 2020-05-03 19:16:44.281 | NULL | NULL | NULL | startevent1 | NULL |
+--------+---------------+---------------+----------------------+-------------------------+-------------------------+-----------+----------------+---------------+-------------+
2 rows in set (0.00 sec)
*****************************************************************************************************************/
// 查询已完成的流程
List<HistoricProcessInstance> datas = historyService
.createHistoricProcessInstanceQuery().finished().list();
log.info("使用finished方法:" + datas.size());
// 根据流程定义ID查询
datas = historyService.createHistoricProcessInstanceQuery()
.processDefinitionId(define.getId()).list();
log.info("使用processDefinitionId方法: " + datas.size());
// 根据流程定义key(流程描述文件的process节点id属性)查询
datas = historyService.createHistoricProcessInstanceQuery()
.processDefinitionKey(define.getKey()).list();
log.info("使用processDefinitionKey方法: " + datas.size());
// 根据业务主键查询
datas = historyService.createHistoricProcessInstanceQuery()
.processInstanceBusinessKey("businessKey1").list();
log.info("使用processInstanceBusinessKey方法: " + datas.size());
// 根据流程实例ID查询
datas = historyService.createHistoricProcessInstanceQuery()
.processInstanceId(pi1.getId()).list();
log.info("使用processInstanceId方法: " + datas.size());
// 查询没有完成的流程实例
historyService.createHistoricProcessInstanceQuery().unfinished().list();
log.info("使用unfinished方法: " + datas.size());
}
}
二 测试
使用finished方法:1
使用processDefinitionId方法: 2
使用processDefinitionKey方法: 2
使用processInstanceBusinessKey方法: 1
使用processInstanceId方法: 1
使用unfinished方法: 1
来源:oschina
链接:https://my.oschina.net/u/4386603/blog/4264026