activiti历史行为查询

淺唱寂寞╮ 提交于 2020-05-04 09:57:37

一 代码

/**
* Copyright (C), 2020-2020, 软件公司
* FileName: ActivityQuery.java
* Author:   cakin
* Date:     2020/5/3
* Description: 历史行为查询
*/
package com.syc.activiti.chapter10;


import java.util.List;


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.HistoricActivityInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;


/**
* @className: ActivityQuery
* @description: 历史行为查询
* @date: 2020/5/3
* @author: cakin
*/
public class ActivityQuery {
    public static void main( String[] args ) {
        // 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
        // 得到流程存储服务实例
        RepositoryService repositoryService = engine.getRepositoryService();
        // 得到运行时服务组件
        RuntimeService runtimeService = engine.getRuntimeService();
        // 得到历史服务组件
        HistoryService historyService = engine.getHistoryService();
        // 任务组件
        TaskService taskService = engine.getTaskService();
        // 部署流程文件
        repositoryService.createDeployment()
                .addClasspathResource("bpmn/chapter10/ActivityQuery.bpmn").deploy();
        // 开始两条流程
        ProcessInstance pi1 = runtimeService.startProcessInstanceByKey("testProcess");
        ProcessInstance pi2 = runtimeService.startProcessInstanceByKey("testProcess");
        // 完成第一个流程
        Task task = taskService.createTaskQuery().processInstanceId(pi1.getId()).singleResult();
        taskService.complete(task.getId());
        runtimeService.signalEventReceived("mySignal");
        /**
mysql> select  ID_ ,PROC_DEF_ID_ ,PROC_INST_ID_ ,EXECUTION_ID_, ACT_ID_  , TASK_ID_, CALL_PROC_INST_ID_,ACT_NAME_ ,ACT_TYPE_ ,ASSIGNEE_,START_TIME_ ,END_TIME_ from act_hi_actinst;
+--------+----------------------+---------------+---------------+-------------------------------+----------+--------------------+------------------+------------------------+-----------+-------------------------+-------------------------+
| ID_    | PROC_DEF_ID_         | PROC_INST_ID_ | EXECUTION_ID_ | ACT_ID_                       | TASK_ID_ | CALL_PROC_INST_ID_ | ACT_NAME_        | ACT_TYPE_              | ASSIGNEE_ | START_TIME_             | END_TIME_               |
+--------+----------------------+---------------+---------------+-------------------------------+----------+--------------------+------------------+------------------------+-----------+-------------------------+-------------------------+
| 245007 | testProcess:8:245004 | 245005        | 245006        | startevent1                   | NULL     | NULL               | Start            | startEvent             | NULL      | 2020-05-03 20:12:02.268 | 2020-05-03 20:12:02.271 |
| 245008 | testProcess:8:245004 | 245005        | 245006        | usertask1                     | 245009   | NULL               | First Task       | userTask               | crazyit   | 2020-05-03 20:12:02.274 | 2020-05-03 20:12:02.865 |
| 245013 | testProcess:8:245004 | 245011        | 245012        | startevent1                   | NULL     | NULL               | Start            | startEvent             | NULL      | 2020-05-03 20:12:02.721 | 2020-05-03 20:12:02.721 |
| 245014 | testProcess:8:245004 | 245011        | 245012        | usertask1                     | 245015   | NULL               | First Task       | userTask               | crazyit   | 2020-05-03 20:12:02.722 | NULL                    |
| 245017 | testProcess:8:245004 | 245005        | 245006        | signalintermediatecatchevent1 | NULL     | NULL               | SignalCatchEvent | intermediateCatchEvent | NULL      | 2020-05-03 20:12:02.865 | 2020-05-03 20:12:03.005 |
| 245019 | testProcess:8:245004 | 245005        | 245006        | endevent1                     | NULL     | NULL               | End              | endEvent               | NULL      | 2020-05-03 20:12:03.006 | 2020-05-03 20:12:03.006 |
+--------+----------------------+---------------+---------------+-------------------------------+----------+--------------------+------------------+------------------------+-----------+-------------------------+-------------------------+
6 rows in set (0.00 sec)
         */
        // 第二个流程实例完成第一个任务
        task = taskService.createTaskQuery().processInstanceId(pi2.getId()).singleResult();
        taskService.complete(task.getId());
        /**
mysql> select  ID_ ,PROC_DEF_ID_ ,PROC_INST_ID_ ,EXECUTION_ID_, ACT_ID_  , TASK_ID_, CALL_PROC_INST_ID_,ACT_NAME_ ,ACT_TYPE_ ,ASSIGNEE_,START_TIME_ ,END_TIME_ from act_hi_actinst;
+--------+----------------------+---------------+---------------+-------------------------------+----------+--------------------+------------------+------------------------+-----------+-------------------------+-------------------------+
| ID_    | PROC_DEF_ID_         | PROC_INST_ID_ | EXECUTION_ID_ | ACT_ID_                       | TASK_ID_ | CALL_PROC_INST_ID_ | ACT_NAME_        | ACT_TYPE_              | ASSIGNEE_ | START_TIME_             | END_TIME_               |
+--------+----------------------+---------------+---------------+-------------------------------+----------+--------------------+------------------+------------------------+-----------+-------------------------+-------------------------+
| 245007 | testProcess:8:245004 | 245005        | 245006        | startevent1                   | NULL     | NULL               | Start            | startEvent             | NULL      | 2020-05-03 20:12:02.268 | 2020-05-03 20:12:02.271 |
| 245008 | testProcess:8:245004 | 245005        | 245006        | usertask1                     | 245009   | NULL               | First Task       | userTask               | crazyit   | 2020-05-03 20:12:02.274 | 2020-05-03 20:12:02.865 |
| 245013 | testProcess:8:245004 | 245011        | 245012        | startevent1                   | NULL     | NULL               | Start            | startEvent             | NULL      | 2020-05-03 20:12:02.721 | 2020-05-03 20:12:02.721 |
| 245014 | testProcess:8:245004 | 245011        | 245012        | usertask1                     | 245015   | NULL               | First Task       | userTask               | crazyit   | 2020-05-03 20:12:02.722 | 2020-05-03 20:16:13.142 |
| 245017 | testProcess:8:245004 | 245005        | 245006        | signalintermediatecatchevent1 | NULL     | NULL               | SignalCatchEvent | intermediateCatchEvent | NULL      | 2020-05-03 20:12:02.865 | 2020-05-03 20:12:03.005 |
| 245019 | testProcess:8:245004 | 245005        | 245006        | endevent1                     | NULL     | NULL               | End              | endEvent               | NULL      | 2020-05-03 20:12:03.006 | 2020-05-03 20:12:03.006 |
| 245020 | testProcess:8:245004 | 245011        | 245012        | signalintermediatecatchevent1 | NULL     | NULL               | SignalCatchEvent | intermediateCatchEvent | NULL      | 2020-05-03 20:16:13.142 | NULL                    |
+--------+----------------------+---------------+---------------+-------------------------------+----------+--------------------+------------------+------------------------+-----------+-------------------------+-------------------------+
7 rows in set (0.00 sec)
         */
        //查询数据
        List<HistoricActivityInstance> datas = historyService.createHistoricActivityInstanceQuery()
                .activityId("endevent1").list();
        System.out.println("使用activityId查询:" + datas.size()); // 结果1
        datas = historyService.createHistoricActivityInstanceQuery()
                .activityInstanceId(datas.get(0).getId()).list();
        System.out.println("使用activityInstanceId查询:" + datas.size()); // 结果1
        datas = historyService.createHistoricActivityInstanceQuery()
                .activityType("intermediateCatchEvent").list();
        System.out.println("使用activityType查询:" + datas.size()); // 结果2
        datas = historyService.createHistoricActivityInstanceQuery().finished().list();
        System.out.println("使用finished查询:" + datas.size()); // 结果6
        datas = historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(pi2.getId()).list();
        System.out.println("使用processInstanceId查询:" + datas.size()); // 结果3
        datas = historyService.createHistoricActivityInstanceQuery()
                .taskAssignee("crazyit").list();
        System.out.println("使用taskAssignee查询:" + datas.size()); // 结果2
        datas = historyService.createHistoricActivityInstanceQuery().unfinished().list();
        System.out.println("使用unfinished查询:" + datas.size()); //结果1
    }
}

二 测试

使用activityId查询:1
使用activityInstanceId查询:1
使用activityType查询:2
使用finished查询:6
使用processInstanceId查询:3
使用taskAssignee查询:2
使用unfinished查询:1

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!