一、前言
本章继续学习activiti6.0的核心api之HistoryService
二、HistoryService
1、创建配置文件activiti_history.cfg.xml
2、测试代码
/**
* 测试HistoryService
* HistoryService的作用:
* 1、管理流程实例结束后的历史数据
* 2、构建历史数据的查询对象
* a)create[历史数据实体]Query
* b)CreateNative[历史数据实体]Query
* c)createProcessInstanceHistoryLogQuery
* 3、根据流程实例id删除流程历史数据
* a)deleteHistoricProcessInstance
* b)deleteHistoricTaskInstance
*
* 历史数据实体:
* HistoricProcessInstance 历史流程实例实体类
* HistoricVariableInstance 流程或任务变量值的实体
* HistoricActivityInstance 单个活动节点执行的信息
* HistoricTaskInstance 单个任务实例的信息
* HistoricDetail 历史流程活动任务详细信息
*
* @author chaoge
* @since 2018/10/9 13:45
*/
public class HistoryServiceTest {
private static final LoggerLOGGER = LoggerFactory.getLogger(HistoryServiceTest.class);
@Rule
public ActivitiRulerule =new ActivitiRule("activiti_history.cfg.xml");
@Test
@Deployment(resources ="processes/testProcess.bpmn20.xml")
public void testHistoryService(){
HistoryService historyService =rule.getHistoryService();
ProcessInstanceBuilder processInstanceBuilder =rule.getRuntimeService().createProcessInstanceBuilder();
Map variables = Maps.newHashMap();
variables.put("key1", "value1");
variables.put("key2", "value2");
variables.put("key3", "value3");
Map transientVariables = Maps.newHashMap();
transientVariables.put("tkey1", "tvalue1");
ProcessInstance processInstance = processInstanceBuilder.processDefinitionKey("myProcess")
.variables(variables)//持久变量,会存入数据库表
.transientVariables(transientVariables)//瞬时变量,不会存入数据库表
.start();
rule.getRuntimeService().setVariable(processInstance.getId(), "key1","value1-1");
Task task =rule.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
//完成任务
//rule.getTaskService().complete(task.getId(),variables );
Map properties = Maps.newHashMap();
properties.put("tkey1","tvalue1" );
properties.put("key2","value2-2" );
rule.getFormService().submitTaskFormData(task.getId(),properties );
List historicProcessInstanceList = historyService.createHistoricProcessInstanceQuery().listPage(0,100 );
historicProcessInstanceList.forEach(historicProcessInstance ->
LOGGER.info("historicProcessInstance = {}", historicProcessInstance));
List historicActivityInstances = historyService.createHistoricActivityInstanceQuery().listPage(0, 100);
historicActivityInstances.forEach(historicActivityInstance ->
LOGGER.info("historicActivityInstance = {}", historicActivityInstance));
List historicDetails = historyService.createHistoricDetailQuery().listPage(0, 100);
historicDetails.forEach(historicDetail ->
LOGGER.info("historicDetail = {}", historicDetail));
List historicTaskInstances = historyService.createHistoricTaskInstanceQuery().listPage(0, 100);
historicTaskInstances.forEach(historicTaskInstance ->
LOGGER.info("historicTaskInstance = {}", ToStringBuilder.reflectionToString(historicTaskInstance,ToStringStyle.JSON_STYLE)));
List historicVariableInstances = historyService.createHistoricVariableInstanceQuery().listPage(0, 100);
historicVariableInstances.forEach(historicVariableInstance ->
LOGGER.info("historicVariableInstance = {}", ToStringBuilder.reflectionToString(historicVariableInstance,ToStringStyle.JSON_STYLE)));
ProcessInstanceHistoryLog processInstanceHistoryLog = historyService.createProcessInstanceHistoryLogQuery(processInstance.getId())
.includeActivities()
.includeComments()
.includeFormProperties()
.includeTasks()
.includeVariables()
.includeVariableUpdates()
.singleResult();
List historicData = processInstanceHistoryLog.getHistoricData();
historicData.forEach(historicData1 ->LOGGER.info("historicData1 = {}",historicData1 ));
//删除流程实例历史信息
historyService.deleteHistoricProcessInstance(processInstance.getId());
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstance.getId()).singleResult();
LOGGER.info("historicProcessInstance = {}",historicProcessInstance );
}
}