首先我们要知道activiti中的历史数据:当流程或节点执行完成离开了运行库放入历史库的数据称为历史数据。
HistoryService:
1、管理流程实例结束后的历史数据;
2、构建历史数据的查询对象;
3、根据流程实例Id删除流程历史数据。
HistoryService历史数据实体:
历史数据实体 | 描述 |
HistoryProcessInstance | 历史流程实例实体类 |
HistoryVariableInstance | 流程或任务变量值的实体 |
HistoryActivityInstance | 单个活动节点执行的信息 |
HistoryTaskInstance | 用户任务实例的信息 |
HistoryDetail | 历史流程活动任务详细信息 |
HistoryService构建历史查询对象方法:
1、create[历史数据实体]Query。
2、createNative[历史数据实体]Query。
3、createProcessInstanceHistoryLogQuery,这个方法只能查出一个流程实例的对象,这条数据包含这个流程实体所有的其他数据,包括task、评论等信息。
HistoryService删除历史操作:
deleteHistoricProcessInstance:删除历史流程实例,使用接连操作方式,将流程实例删除时,会将对应的节点信息、task信息、评论信息与变量信息都会统一删除。
deleteHistoricTaskInstance:小范围的删除,删除历史Task实例,只是把对应的HistoryTask对象与Task相关的变量删除掉,没有删除对应的流程实例信息。
下面用代码演示一下HistoryService的操作:
配置数据库信息并且配置流程历史记录级别,这里我选择FULL级别,具体级别配置请看这篇博客:https://blog.csdn.net/JJBOOM425/article/details/85226258
测试类内容如下:
package com.jjf.activiti.coreapi;
import com.google.common.collect.Maps;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.runtime.ProcessInstanceBuilder;
import org.activiti.engine.task.Task;
import org.activiti.engine.test.ActivitiRule;
import org.activiti.engine.test.Deployment;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
/**
* 历史数据管理
*/
public class HistoryServiceTest {
private static final Logger LOGGER = LoggerFactory.getLogger(HistoryServiceTest.class);
@Rule
public ActivitiRule activitiRule = new ActivitiRule("activiti_history.cfg.xml");
@Test
@Deployment(resources = {"my-process.bpmn20.xml"})
public void testHistory(){
HistoryService historyService = activitiRule.getHistoryService(); //获取历史服务类
ProcessInstanceBuilder processInstanceBuilder = activitiRule.getRuntimeService()
.createProcessInstanceBuilder(); //启动运行实例,根据流程实例建造类构建
Map varoables = Maps.newHashMap(); //普通变量
varoables.put("key0","value0");
varoables.put("key1","value1");
varoables.put("key2","value2");
Map transientVariables = Maps.newHashMap(); //瞬时变量
transientVariables.put("tkey1","tvalue1");
ProcessInstance processInstance = processInstanceBuilder.processDefinitionKey("my-process")
.variables(varoables)
.transientVariables(transientVariables).start();//根据builder创建流程实例,指定普通变量与瞬时变量,瞬时变量不会存到历史库中
activitiRule.getRuntimeService().setVariable(processInstance.getId(),"key1","value1_1"); //修改变量,记录会存在HistoryDetail
Task task = activitiRule.getTaskService().createTaskQuery()
.processInstanceId(processInstance.getId()).singleResult(); //获取事件
//activitiRule.getTaskService().complete(task.getId(),varoables); //以前的方法 将Task执行通过
Map properties = Maps.newHashMap();
properties.put("fKey","fValue1");
properties.put("key2","value_2_2");
activitiRule.getFormService().submitTaskFormData(task.getId(),properties); //使用FormService,提交表单将Task执行通过
List historicProcessInstances = historyService
.createHistoricProcessInstanceQuery().listPage(0, 100); //通过historyService来查询相关流程实例变量
for (HistoricProcessInstance historicProcessInstance : historicProcessInstances){ //输出流程实例对象
LOGGER.info("historicProcessInstance = [{}] ",
ToStringBuilder.reflectionToString(historicProcessInstance, ToStringStyle.JSON_STYLE)); //输出流程实例对象
}
List historicActivityInstances = historyService
.createHistoricActivityInstanceQuery().listPage(0, 100); //查询流程节点的对象
for (HistoricActivityInstance historicActivityInstance : historicActivityInstances){
LOGGER.info("historicActivityInstance = [{}]" ,historicActivityInstance ); //输出流程节点
}
List historicTaskInstances = historyService
.createHistoricTaskInstanceQuery().listPage(0, 100); //查询历史事件
for (HistoricTaskInstance historicTaskInstance : historicTaskInstances){
LOGGER.info("historicTaskInstance = [{}]" ,
ToStringBuilder.reflectionToString(historicTaskInstance, ToStringStyle.JSON_STYLE) ); //输出历史事件实例
}
List historicVariableInstances = historyService
.createHistoricVariableInstanceQuery().listPage(0, 100); //查询历史变量信息
for (HistoricVariableInstance historicVariableInstance : historicVariableInstances){
LOGGER.info("historicVariableInstance = [{}]" ,historicVariableInstance ); //输出历史变量信息
}
List historicDetails = historyService
.createHistoricDetailQuery().listPage(0, 100); //查询历史详情信息
for (HistoricDetail historicDetail : historicDetails){
LOGGER.info("historicDetail = [{}]" , historicDetail); //输出历史详情信息
}
ProcessInstanceHistoryLog processInstanceHistoryLog = historyService.createProcessInstanceHistoryLogQuery(processInstance.getId())
.includeVariables()
.includeFormProperties()
.includeComments()
.includeTasks()
.includeActivities()
.includeVariableUpdates().singleResult();
List historicDataList = processInstanceHistoryLog.getHistoricData();
for (HistoricData historicData : historicDataList){
LOGGER.info("historicData = [{}] " , historicData);
}
historyService.deleteHistoricProcessInstance(processInstance.getId()); //删除流程实例
HistoricProcessInstance historicProcessInstance = historyService
.createHistoricProcessInstanceQuery().processInstanceId(processInstance.getId()).singleResult();
LOGGER.info("historicProcessInstance = [{}]" , historicProcessInstance);
}
}
首先获取 HistoryService 对象为演示做准备,然后构建了流程实例建造者,它为了设置流程启动定义的key,然后流程启动以后再对流程变量进行修改(将key1键值从 “value1” 改为 “value1_1”),获取当前执行的Task,通过 submitTaskFormData 提交(验证了通过表单提交的数据和普通变量数据的区别),最后对五种历史实例对象进行查询遍历输出,再根据流程实例ID获取流程实例历史日志信息,通过createProcessInstanceHistoryLogQuery方法,其中包括变量信息(Variables)、表单属性信息(FormProperties)、评论信息(Comments)、事件信息(Tasks)、流程执行结点信息(Activities)与变量修改设计的变量修改信息(VariableUpdates)。
上述测试方法输出结果如下:
Loading XML bean definitions from class path resource [activiti_history.cfg.xml]
Activiti 5 compatibility handler implementation not found or error during instantiation : org.activiti.compatibility.DefaultActiviti5CompatibilityHandler. Activiti 5 backwards compatibility disabled.
performing create on engine with resource org/activiti/db/create/activiti.h2.create.engine.sql
performing create on history with resource org/activiti/db/create/activiti.h2.create.history.sql
performing create on identity with resource org/activiti/db/create/activiti.h2.create.identity.sql
ProcessEngine default created
historicProcessInstance = [{"endActivityId":"end","businessKey":null,"startUserId":null,"startActivityId":"start","superProcessInstanceId":null,"tenantId":"","name":null,"localizedName":null,"description":null,"localizedDescription":null,"processDefinitionKey":"my-process","processDefinitionName":null,"processDefinitionVersion":1,"deploymentId":"1","queryVariables":null,"processInstanceId":"4","processDefinitionId":"my-process:1:3","startTime":"Thu Feb 21 15:24:49 CST 2019","endTime":"Thu Feb 21 15:24:49 CST 2019","durationInMillis":325,"deleteReason":null,"id":"4","isInserted":false,"isUpdated":false,"isDeleted":false}]
historicActivityInstance = [HistoricActivityInstanceEntity[id=12, activityId=start, activityName=null]]
historicActivityInstance = [HistoricActivityInstanceEntity[id=13, activityId=someTask, activityName=Activiti is awesome!]]
historicActivityInstance = [HistoricActivityInstanceEntity[id=21, activityId=end, activityName=null]]
historicTaskInstance = [{"executionId":"11","name":"Activiti is awesome!","localizedName":null,"parentTaskId":null,"description":null,"localizedDescription":null,"owner":null,"assignee":null,"taskDefinitionKey":"someTask","formKey":null,"priority":50,"dueDate":null,"claimTime":null,"category":null,"tenantId":"","queryVariables":null,"processInstanceId":"4","processDefinitionId":"my-process:1:3","startTime":"Thu Feb 21 15:24:49 CST 2019","endTime":"Thu Feb 21 15:24:49 CST 2019","durationInMillis":226,"deleteReason":null,"id":"14","isInserted":false,"isUpdated":false,"isDeleted":false}]
historicVariableInstance = [HistoricVariableInstanceEntity[id=19, name=fKey, revision=0, type=string, textValue=fValue1]]
historicVariableInstance = [HistoricVariableInstanceEntity[id=5, name=key1, revision=1, type=string, textValue=value1_1]]
historicVariableInstance = [HistoricVariableInstanceEntity[id=7, name=key2, revision=1, type=string, textValue=value_2_2]]
historicVariableInstance = [HistoricVariableInstanceEntity[id=9, name=key0, revision=0, type=string, textValue=value0]]
historicDetail = [HistoricDetailVariableInstanceUpdateEntity[id=10, name=key0, type=string, textValue=value0]]
historicDetail = [HistoricDetailVariableInstanceUpdateEntity[id=15, name=key1, type=string, textValue=value1_1]]
historicDetail = [org.activiti.engine.impl.persistence.entity.HistoricFormPropertyEntityImpl@133e019b]
historicDetail = [org.activiti.engine.impl.persistence.entity.HistoricFormPropertyEntityImpl@41382722]
historicDetail = [HistoricDetailVariableInstanceUpdateEntity[id=18, name=key2, type=string, textValue=value_2_2]]
historicDetail = [HistoricDetailVariableInstanceUpdateEntity[id=20, name=fKey, type=string, textValue=fValue1]]
historicDetail = [HistoricDetailVariableInstanceUpdateEntity[id=6, name=key1, type=string, textValue=value1]]
historicDetail = [HistoricDetailVariableInstanceUpdateEntity[id=8, name=key2, type=string, textValue=value2]]
historicData = [HistoricVariableInstanceEntity[id=5, name=key1, revision=1, type=string, textValue=value1_1]]
historicData = [HistoricDetailVariableInstanceUpdateEntity[id=6, name=key1, type=string, textValue=value1]]
historicData = [HistoricVariableInstanceEntity[id=7, name=key2, revision=1, type=string, textValue=value_2_2]]
historicData = [HistoricVariableInstanceEntity[id=9, name=key0, revision=0, type=string, textValue=value0]]
historicData = [HistoricDetailVariableInstanceUpdateEntity[id=10, name=key0, type=string, textValue=value0]]
historicData = [HistoricDetailVariableInstanceUpdateEntity[id=8, name=key2, type=string, textValue=value2]]
historicData = [HistoricActivityInstanceEntity[id=12, activityId=start, activityName=null]]
historicData = [HistoricActivityInstanceEntity[id=13, activityId=someTask, activityName=Activiti is awesome!]]
historicData = [org.activiti.engine.impl.persistence.entity.HistoricTaskInstanceEntityImpl@372b0d86]
historicData = [HistoricDetailVariableInstanceUpdateEntity[id=15, name=key1, type=string, textValue=value1_1]]
historicData = [org.activiti.engine.impl.persistence.entity.HistoricFormPropertyEntityImpl@47747fb9]
historicData = [org.activiti.engine.impl.persistence.entity.HistoricFormPropertyEntityImpl@3113a37]
historicData = [HistoricDetailVariableInstanceUpdateEntity[id=18, name=key2, type=string, textValue=value_2_2]]
historicData = [HistoricVariableInstanceEntity[id=19, name=fKey, revision=0, type=string, textValue=fValue1]]
historicData = [HistoricDetailVariableInstanceUpdateEntity[id=20, name=fKey, type=string, textValue=fValue1]]
historicData = [HistoricActivityInstanceEntity[id=21, activityId=end, activityName=null]]
historicProcessInstance = [null]
输出结果解读:
流程实例——historicProcessInstance:
{"endActivityId":"end","businessKey":null,"startUserId":null,"startActivityId":"start","superProcessInstanceId":null,"tenantId":"","name":null,"localizedName":null,"description":null,"localizedDescription":null,"processDefinitionKey":"my-process","processDefinitionName":null,"processDefinitionVersion":1,"deploymentId":"1","queryVariables":null,"processInstanceId":"4","processDefinitionId":"my-process:1:3","startTime":"Thu Feb 21 15:24:49 CST 2019","endTime":"Thu Feb 21 15:24:49 CST 2019","durationInMillis":325,"deleteReason":null,"id":"4","isInserted":false,"isUpdated":false,"isDeleted":false}]
流程实例节点信息——historicActivityInstance:(包含开始结点,中间的someTask结点,结束结点)
historicActivityInstance = [HistoricActivityInstanceEntity[id=12, activityId=start, activityName=null]]
historicActivityInstance = [HistoricActivityInstanceEntity[id=13, activityId=someTask, activityName=Activiti is awesome!]]
historicActivityInstance = [HistoricActivityInstanceEntity[id=21, activityId=end, activityName=null]]
历史事件实例——historicTaskInstance:
historicTaskInstance = [{"executionId":"11","name":"Activiti is awesome!","localizedName":null,"parentTaskId":null,"description":null,"localizedDescription":null,"owner":null,"assignee":null,"taskDefinitionKey":"someTask","formKey":null,"priority":50,"dueDate":null,"claimTime":null,"category":null,"tenantId":"","queryVariables":null,"processInstanceId":"4","processDefinitionId":"my-process:1:3","startTime":"Thu Feb 21 15:24:49 CST 2019","endTime":"Thu Feb 21 15:24:49 CST 2019","durationInMillis":226,"deleteReason":null,"id":"14","isInserted":false,"isUpdated":false,"isDeleted":false}]
历史变量信息——historicVariableInstance:可以看到之前写的瞬时变量并没有存入进来,key1与key2版本号为1,说明修改过
historicVariableInstance = [HistoricVariableInstanceEntity[id=19, name=fKey, revision=0, type=string, textValue=fValue1]]
historicVariableInstance = [HistoricVariableInstanceEntity[id=5, name=key1, revision=1, type=string, textValue=value1_1]]
historicVariableInstance = [HistoricVariableInstanceEntity[id=7, name=key2, revision=1, type=string, textValue=value_2_2]]
historicVariableInstance = [HistoricVariableInstanceEntity[id=9, name=key0, revision=0, type=string, textValue=value0]]
历史详情信息——historicDetail
historicDetail = [HistoricDetailVariableInstanceUpdateEntity[id=10, name=key0, type=string, textValue=value0]]
historicDetail = [HistoricDetailVariableInstanceUpdateEntity[id=15, name=key1, type=string, textValue=value1_1]]
historicDetail = [org.activiti.engine.impl.persistence.entity.HistoricFormPropertyEntityImpl@133e019b]
historicDetail = [org.activiti.engine.impl.persistence.entity.HistoricFormPropertyEntityImpl@41382722]
historicDetail = [HistoricDetailVariableInstanceUpdateEntity[id=18, name=key2, type=string, textValue=value_2_2]]
historicDetail = [HistoricDetailVariableInstanceUpdateEntity[id=20, name=fKey, type=string, textValue=fValue1]]
historicDetail = [HistoricDetailVariableInstanceUpdateEntity[id=6, name=key1, type=string, textValue=value1]]
historicDetail = [HistoricDetailVariableInstanceUpdateEntity[id=8, name=key2, type=string, textValue=value2]]
历史数据——historicData:跟上述数据相似,将上面数据整合一起输出
historicData = [HistoricVariableInstanceEntity[id=5, name=key1, revision=1, type=string, textValue=value1_1]]
historicData = [HistoricDetailVariableInstanceUpdateEntity[id=6, name=key1, type=string, textValue=value1]]
historicData = [HistoricVariableInstanceEntity[id=7, name=key2, revision=1, type=string, textValue=value_2_2]]
historicData = [HistoricVariableInstanceEntity[id=9, name=key0, revision=0, type=string, textValue=value0]]
historicData = [HistoricDetailVariableInstanceUpdateEntity[id=10, name=key0, type=string, textValue=value0]]
historicData = [HistoricDetailVariableInstanceUpdateEntity[id=8, name=key2, type=string, textValue=value2]]
historicData = [HistoricActivityInstanceEntity[id=12, activityId=start, activityName=null]]
historicData = [HistoricActivityInstanceEntity[id=13, activityId=someTask, activityName=Activiti is awesome!]]
historicData = [org.activiti.engine.impl.persistence.entity.HistoricTaskInstanceEntityImpl@372b0d86]
historicData = [HistoricDetailVariableInstanceUpdateEntity[id=15, name=key1, type=string, textValue=value1_1]]
historicData = [org.activiti.engine.impl.persistence.entity.HistoricFormPropertyEntityImpl@47747fb9]
historicData = [org.activiti.engine.impl.persistence.entity.HistoricFormPropertyEntityImpl@3113a37]
historicData = [HistoricDetailVariableInstanceUpdateEntity[id=18, name=key2, type=string, textValue=value_2_2]]
historicData = [HistoricVariableInstanceEntity[id=19, name=fKey, revision=0, type=string, textValue=fValue1]]
historicData = [HistoricDetailVariableInstanceUpdateEntity[id=20, name=fKey, type=string, textValue=fValue1]]
historicData = [HistoricActivityInstanceEntity[id=21, activityId=end, activityName=null]]