一、历史管理服务
HistoryService
- 管理流程实例结束后的历史数据
- 构建历史数据的查询对象
- 根据流程实例id删除流程历史数据
HistoryService历史数据实体
| 历史数据实体 | 描述 | | ------------------------ | ------------------------ | | HistoricProcessInstance | 历史流程实例实体类 | | HistoricVariableInstance | 流程或任务变量值的实体 | | HistoricActivityInstance | 单个活动节点执行的信息 | | HistoricTaskInstance | 用户任务实例的信息 | | HisroticDetail | 历史流程活动任务详情信息 |
HistoryService构建历史查询对象
- create[历史数据实体]Query
- createNative[历史数据实体]Query
- createProcessInstanceHistoryLogQuery
HistoryService删除历史操作
- deleteHistoricProcessInstance
- deleteHistoricTaskInstance
相关API
相关详解
- HistoryService只提供了历史数据查询和删除的API。
- 历史流程实例查询
- 使用HistoryService的createHistoricProcesslnstanceQuery方法可以得到HistoricProcessInstanceQuery对象,该对象主要用于流程实例的历史数据查询,流程实例的历史数据保存在ACT_HI_PROCINST表中,不管流程是否完成,只要创建了流程实例(启动流程),流程实例的数据(历史数据)均会被保存到ACT_HI_PROCINST表中。
- 历史任务查询
- 使用HistoryService的createHistoricTasklnstanceQuery方法可以得到 HistoricTasklnstanceQuery实例,与其他的Query对象一样,该对象提供设置查询条件和排序的方法,到历史任务数据表( ACT_HI_TASKINST)中查询符合条件的数据。
- 历史流程明细查询
- 流程的明细数据包括流程参数和流程表单属性,在流程进行的过程中,会产生相当多的明细数据,会被保存到历史明细表 ACT_HI_DETAIL中。在默认情况下,Activiti不记录这些“过程”数据,当history配置为ful 级别时,才会记录明细数据。流程中的参数除了会被保存到历史明细表中外,还会被保存到历史参数表ACT_HI_VARINST中,需要注意的是,历史参数表中保存的是最终的参数值,而历史明细表中则会保存参数的改变过程。
- 删除历史流程实例和历史任务
- 删除历史流程实例与历史任务,会将与其关联的数据一并删除,例如参数、明细、行为等数据。在删除时需要注意的是,如果一个流程实例没有完成,在调用删除方法时,将会抛出异常。
- 调用deleteHistoricTaskinstance
二、相关代码
测试代码
javapackage com.laogoubi.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.junit.Rule; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.List; import java.util.Map; import java.util.Properties; /** * @ClassName HistoryServiceTest * @Description TODO * @Author eastern * @Date 2020/7/4 上午10:59 * @Version 1.0 **/ 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 = {"diagrams/my-process.bpmn20.xml"}) public void testHistory() { HistoryService historyService = activitiRule.getHistoryService(); ProcessInstanceBuilder processInstanceBuilder = activitiRule.getRuntimeService().createProcessInstanceBuilder(); Map<String, Object> variables = Maps.newHashMap(); variables.put("key0", "value0"); variables.put("key1", "value1"); variables.put("key2", "value2"); Map<String, Object> transientVariables = Maps.newHashMap(); transientVariables.put("tkey1", "tvalue1"); ProcessInstance processInstance = processInstanceBuilder.processDefinitionKey("my-process").variables(variables).transientVariables(transientVariables).start(); activitiRule.getRuntimeService().setVariable(processInstance.getId(), "key1", "value1_"); Task task = activitiRule.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult(); // activitiRule.getTaskService().complete(task.getId(), variables); Map<String, String> properties = Maps.newHashMap(); properties.put("fKey1", "fValue1"); properties.put("key2", "value_2_2"); activitiRule.getFormService().submitTaskFormData(task.getId(), properties); List<HistoricProcessInstance> historicProcessInstances = historyService.createHistoricProcessInstanceQuery().listPage(0, 100); for (HistoricProcessInstance historicProcessInstance : historicProcessInstances) { logger.info("historicProcessInstance = {}", historicProcessInstance); } List<HistoricActivityInstance> historicActivityInstances = historyService.createHistoricActivityInstanceQuery().listPage(0, 100); for (HistoricActivityInstance historicActivityInstance : historicActivityInstances) { logger.info("historicActivityInstance = {}", historicActivityInstance); } List<HistoricTaskInstance> historicTaskInstances = historyService.createHistoricTaskInstanceQuery().listPage(0 , 100); for (HistoricTaskInstance historicTaskInstance : historicTaskInstances) { logger.info("historicTaskInstance = {}", historicTaskInstance); } List<HistoricVariableInstance> historicVariableInstances = historyService.createHistoricVariableInstanceQuery().listPage(0, 100); for (HistoricVariableInstance historicVariableInstance : historicVariableInstances) { logger.info("historicVariableInstance = {}", historicVariableInstance); } List<HistoricDetail> 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<HistoricData> historicData = processInstanceHistoryLog.getHistoricData(); for (HistoricData historicDatum : historicData) { logger.info("historicDatum = {}", historicDatum); } historyService.deleteHistoricProcessInstance(processInstance.getId()); HistoricProcessInstanceQuery historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstance.getId()); logger.info("historicProcessInstance = {}", historicProcessInstance); } }