HistoryService历史服务
1.功能:
管理流程结束后的历史数据
构建历史数据的查询对象
根据流程实例ID删除流程历史数据
2.HistoryService历史数据实体
HistoryProcessInstance -> 历史流程实例实体类
HistoricVariableInstance -> 流程或任务变量值的实体
HistoricActivityInstance -> 单个活动结点执行的信息
HistoricTaskInstance -> 用户任务实例的信息
HistoricDetail -> 历史流程活动任务详细信息(Form属性信息)
-->HistoryService构建历史查询对象
create[历史数据实体]Query
createNative[历史数据实体]Query
createProcessInstanceHistoryLogQuery(查出一个流程实例的一个对象,每次只能查出一条记录)
-->HistoryService删除历史操作
deleteHistoricProcessInstance
deleteHistoricTaskInstance
具体运用
package com.syc.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.omg.CORBA.PUBLIC_MEMBER;
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 variables = Maps.newHashMap();
variables.put("key0", "value0");
variables.put("key1", "value1");
variables.put("key2", "value2");
Map transientVariables = Maps.newHashMap();
transientVariables.put("tkey1", "tvalue1");
ProcessInstance processInstance = processInstanceBuilder.processDefinitionKey("my-process").variables(variables).transientVariables(transientVariables).start();//启动流程的定义Key
//修改变量
activitiRule.getRuntimeService().setVariable(processInstance.getId(),"key1", "value1_1");
Task task = activitiRule.getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
// activitiRule.getTaskService().complete(task.getId(),variables);
Map properties = Maps.newHashMap();
properties.put("fkey1", "fvalue1");
properties.put("key2", "value_2_2");
activitiRule.getFormService().submitTaskFormData(task.getId(), properties);
List historicProcessInstances = historyService.createHistoricProcessInstanceQuery().listPage(0,100);
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().includeVariables().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);
}
}
1、Activiti核心API中的其他管理服务
管理服务ManagementService
动态流程定义服务DynamicBpmnService(实际业务开发中很少用到)
管理服务ManagementService
Job任务管理
数据库相关通用操作
执行流程引擎命令(Command)
---> Job任务查询
工作查询对象
---> 数据库相关操作
查询表结构元数据(TableMetaData)
通用表查询(TablePageQuery)
执行自定义的Sql查询(executeCustomSql)
需要先在my-process-job.bpmn20.xml流程定义文件中的“开始事件”中配置定时器事件定义
R5/PT10S
在activiti_job.cfg.xml中配置启动异步任务
com.imooc.activiti.mapper.MyCustomMapper
ManagementService核心代码
package com.syc.activiti.coreapi;
import com.imooc.activiti.mapper.MyCustomMapper;
import org.activiti.engine.ManagementService;
import org.activiti.engine.impl.cmd.AbstractCustomSqlExecution;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.management.TablePage;
import org.activiti.engine.runtime.DeadLetterJobQuery;
import org.activiti.engine.runtime.Job;
import org.activiti.engine.runtime.JobQuery;
import org.activiti.engine.runtime.SuspendedJobQuery;
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 sun.misc.Resource;
import java.util.List;
import java.util.Map;
public class ManagementServiceTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ManagementServiceTest.class);
@Rule
public ActivitiRule activitiRule = new ActivitiRule("activiti_job.cfg.xml");
/**
* Job任务管理测试
*/
@Test
@Deployment(resources = {"my-process-job.bpmn20.xml"})
public void testJobQuery(){
ManagementService managementService = activitiRule.getManagementService();
List TimerJobList = managementService.createTimerJobQuery().listPage(0, 100);
for(Job timerJob : TimerJobList){
LOGGER.info("timerJob = {}", timerJob);
}
//获取其他三种工作查询对象
JobQuery jobQuery = managementService.createJobQuery();
SuspendedJobQuery suspendedJobQuery = managementService.createSuspendedJobQuery();
DeadLetterJobQuery deadLetterJobQuery = managementService.createDeadLetterJobQuery();
}
/**
* 数据库相关的工作管理测试
*/
@Test
@Deployment(resources = {"my-process-job.bpmn20.xml"})
public void testTablePageQuery(){
ManagementService managementService = activitiRule.getManagementService();
TablePage tablePage = managementService.createTablePageQuery().tableName(managementService.getTableName(ProcessDefinitionEntity.class)).listPage(0, 100);
List
ActivitiException异常与描述(所有的异常都是ActivitiException的子类,而ActivitiException又继承自Java 中的 RuntimeException)
异常1:流程启动的时候发生;
异常2:并发量较高时发生;
异常3:类异常,损失较大;
异常4:操作对象的时候,这个对象已经被别人执行了;
异常5:参数问题比较常见;
异常6:异常抛出的方式,提醒客户这次调用时失败的;
异常7:流程定义文件可以配置对这个异常的监听,可以做一些特定的跳转