一 代码
/**
* Copyright (C), 2020-2020, 软件公司
* FileName: TaskQuery.java
* Author: cakin
* Date: 2020/5/3
* Description: 查询历史任务
*/
package com.syc.activiti.chapter10;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.HistoricTaskInstance;
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: TaskQuery
* @description: 查询历史任务
* @date: 2020/5/3
* @author: cakin
*/
@Slf4j
public class TaskQuery {
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/TaskQuery.bpmn").deploy();
ProcessDefinition define = repositoryService.createProcessDefinitionQuery()
.deploymentId(deploy.getId()).singleResult();
// 初始化参数
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Map<String, Object> vars = new HashMap<String, Object>();
vars.put("varDate1", sdf.parseObject("2020-10-10 06:00:00"));
vars.put("varDate2", sdf.parseObject("2021-10-10 06:00:00"));
// 开始流程
ProcessInstance pi1 = runtimeService.startProcessInstanceByKey("testProcess",
"businessKey1", vars);
ProcessInstance pi2 = runtimeService.startProcessInstanceByKey("testProcess",
"businessKey2", vars);
// 完成流程1
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_DEF_ID_, TASK_DEF_KEY_ ,PROC_INST_ID_,EXECUTION_ID_ , NAME_ , PARENT_TASK_ID_ , DESCRIPTION_ , OWNER_ ,ASSIGNEE_ ,START_TIME_, CLAIM_TIME_ ,END_TIME_ from act_hi_taskinst;
+--------+----------------------+---------------+---------------+---------------+------------+-----------------+--------------+--------+-----------+-------------------------+-------------+-------------------------+
| ID_ | PROC_DEF_ID_ | TASK_DEF_KEY_ | PROC_INST_ID_ | EXECUTION_ID_ | NAME_ | PARENT_TASK_ID_ | DESCRIPTION_ | OWNER_ | ASSIGNEE_ | START_TIME_ | CLAIM_TIME_ | END_TIME_ |
+--------+----------------------+---------------+---------------+---------------+------------+-----------------+--------------+--------+-----------+-------------------------+-------------+-------------------------+
| 242511 | testProcess:7:242504 | usertask1 | 242505 | 242508 | First Task | NULL | NULL | NULL | angus | 2020-05-03 19:53:10.659 | NULL | 2020-05-03 19:53:39.461 |
| 242519 | testProcess:7:242504 | usertask1 | 242513 | 242516 | First Task | NULL | NULL | NULL | angus | 2020-05-03 19:53:10.827 | NULL | NULL |
| 242522 | testProcess:7:242504 | usertask2 | 242505 | 242508 | End Task | NULL | NULL | NULL | crazyit | 2020-05-03 19:53:39.512 | NULL | 2020-05-03 19:54:35.898 |
+--------+----------------------+---------------+---------------+---------------+------------+-----------------+--------------+--------+-----------+-------------------------+-------------+-------------------------+
***********************************************************************************************************/
// 流程2完成一个任务
task = taskService.createTaskQuery().processInstanceId(pi2.getId()).singleResult();
taskService.complete(task.getId());
/*************************************************************************************************************
mysql> select ID_, PROC_DEF_ID_, TASK_DEF_KEY_ ,PROC_INST_ID_,EXECUTION_ID_ , NAME_ , PARENT_TASK_ID_ , DESCRIPTION_ , OWNER_ ,ASSIGNEE_ ,START_TIME_, CLAIM_TIME_ ,END_TIME_ from act_hi_taskinst;
+--------+----------------------+---------------+---------------+---------------+------------+-----------------+--------------+--------+-----------+-------------------------+-------------+-------------------------+
| ID_ | PROC_DEF_ID_ | TASK_DEF_KEY_ | PROC_INST_ID_ | EXECUTION_ID_ | NAME_ | PARENT_TASK_ID_ | DESCRIPTION_ | OWNER_ | ASSIGNEE_ | START_TIME_ | CLAIM_TIME_ | END_TIME_ |
+--------+----------------------+---------------+---------------+---------------+------------+-----------------+--------------+--------+-----------+-------------------------+-------------+-------------------------+
| 242511 | testProcess:7:242504 | usertask1 | 242505 | 242508 | First Task | NULL | NULL | NULL | angus | 2020-05-03 19:53:10.659 | NULL | 2020-05-03 19:53:39.461 |
| 242519 | testProcess:7:242504 | usertask1 | 242513 | 242516 | First Task | NULL | NULL | NULL | angus | 2020-05-03 19:53:10.827 | NULL | 2020-05-03 19:55:20.384 |
| 242522 | testProcess:7:242504 | usertask2 | 242505 | 242508 | End Task | NULL | NULL | NULL | crazyit | 2020-05-03 19:53:39.512 | NULL | 2020-05-03 19:54:35.898 |
| 242526 | testProcess:7:242504 | usertask2 | 242513 | 242516 | End Task | NULL | NULL | NULL | crazyit | 2020-05-03 19:55:20.434 | NULL | NULL |
+--------+----------------------+---------------+---------------+---------------+------------+-----------------+--------------+--------+-----------+-------------------------+-------------+-------------------------+
*************************************************************************************************************/
//历史数据查询
List<HistoricTaskInstance> datas = historyService.createHistoricTaskInstanceQuery()
.finished().list();
log.info("使用finished方法查询:" + datas.size()); // 结果3
datas = historyService.createHistoricTaskInstanceQuery()
.processDefinitionId(define.getId()).list();
log.info("使用processDefinitionId方法查询:" + datas.size()); // 结果4
datas = historyService.createHistoricTaskInstanceQuery()
.processDefinitionKey("testProcess").list();
log.info("使用processDefinitionKey方法查询:" + datas.size()); // 结果4
datas = historyService.createHistoricTaskInstanceQuery()
.processDefinitionName("testProcess2").list();
log.info("使用processDefinitionName方法查询:" + datas.size()); // 结果4
datas = historyService.createHistoricTaskInstanceQuery()
.processFinished().list();
log.info("使用processFinished方法查询:" + datas.size()); // 结果2
datas = historyService.createHistoricTaskInstanceQuery()
.processInstanceId(pi2.getId()).list();
log.info("使用processInstanceId方法查询:" + datas.size()); // 结果2
datas = historyService.createHistoricTaskInstanceQuery()
.processUnfinished().list();
log.info("使用processUnfinished方法查询:" + datas.size()); // 结果2
datas = historyService.createHistoricTaskInstanceQuery()
.taskAssignee("crazyit").list();
log.info("使用taskAssignee方法查询:" + datas.size()); // 结果2
datas = historyService.createHistoricTaskInstanceQuery()
.taskAssigneeLike("%zy%").list();
log.info("使用taskAssigneeLike方法查询:" + datas.size()); // 结果2
datas = historyService.createHistoricTaskInstanceQuery()
.taskDefinitionKey("usertask1").list();
log.info("使用taskDefinitionKey方法查询:" + datas.size()); // 结果2
datas = historyService.createHistoricTaskInstanceQuery()
.taskDueAfter(sdf.parse("2020-10-11 06:00:00")).list();
log.info("使用taskDueAfter方法查询:" + datas.size()); // 结果2
datas = historyService.createHistoricTaskInstanceQuery()
.taskDueBefore(sdf.parse("2022-10-11 06:00:00")).list();
log.info("使用taskDueBefore方法查询:" + datas.size()); // 结果4
datas = historyService.createHistoricTaskInstanceQuery()
.taskDueDate(sdf.parse("2020-10-11 06:00:00")).list();
log.info("使用taskDueDate方法查询:" + datas.size()); // 结果0
datas = historyService.createHistoricTaskInstanceQuery()
.unfinished().list();
log.info("使用unfinished方法查询:" + datas.size()); // 结果1
}
}
二 测试
使用finished方法查询:3
使用processDefinitionId方法查询:4
使用processDefinitionKey方法查询:4
使用processDefinitionName方法查询:4
使用processFinished方法查询:2
使用processInstanceId方法查询:2
使用processUnfinished方法查询:2
使用taskAssignee方法查询:2
使用taskAssigneeLike方法查询:2
使用taskDefinitionKey方法查询:2
使用taskDueAfter方法查询:2
使用taskDueBefore方法查询:4
使用taskDueDate方法查询:0
使用unfinished方法查询:1
来源:oschina
链接:https://my.oschina.net/u/4383937/blog/4263934