一、历史记录配置
在流程执行的过程中,会产生一些流程相关的数据,例如流程实例、流程任务和流程参数等 ,随着流程的进行与结束,这些数据将会被从流程数据表中删除,为了能保存这些数据, Activiti 提供了历史数据表,可以将这些数据保存到历史数据表中。
对于这些历史数据,保存到何种粒度, Activiti 提供了 history 属性对其进行配置 。 history 属性有点像 log4j 的日志输出级别,该属性有以下四个值 。
- 配置HistoryLevel
- none:不记录历史流程,性能高,流程结束后不可读取
- activiti:归档流程实例和活动实例,流程变量不同步
- audit:默认值,在activiti基础上同步变量值,保存表单属性
- full:性能较差,记录所有实例和变量细节变化。
代码逻辑实现
javapackage com.laogoubi.config; import com.google.common.collect.Maps; import org.activiti.engine.history.HistoricActivityInstance; import org.activiti.engine.history.HistoricDetail; import org.activiti.engine.history.HistoricTaskInstance; import org.activiti.engine.history.HistoricVariableInstance; import org.activiti.engine.runtime.Execution; import org.activiti.engine.runtime.ProcessInstance; 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; /** * @ClassName ConfigMDCTest * @Description History测试 * @Author eastern * @Date 2020/6/28 上午11:49 * @Version 1.0
**/ public class ConfigHistoryLevelTest { private static final Logger logger = LoggerFactory.getLogger(ConfigHistoryLevelTest.class);
@Rule
public ActivitiRule activitiRule = new ActivitiRule("activiti_history.cfg.xml");
@Test
@Deployment(resources = {"com/laogoubi/my-process.bpmn20.xml"})
public void test() {
// 启动流程
startProcess();
// 修改变量
changeVariable();
// 提交表单 task
submitFommTask();
// 输出历史内容
// 输出历史活动
showHistoricActivity();
// 输出历史变量
showHistoricVariable();
// 输出历史表单
showHistoricTask();
// 输出历史详情表单
showHistoryForm();
// 输出历史详情
showHistoryDetail();
}
private void showHistoryDetail() {
List<HistoricDetail> historicDetails = activitiRule.getHistoryService().createHistoricDetailQuery().listPage(0
, 100);
for (HistoricDetail historicDetail : historicDetails) {
logger.info("historicDetail = {}", toString(historicDetail));
}
logger.info("historicDetails.size", historicDetails.size());
}
private void showHistoryForm() {
List<HistoricDetail> historicDetailsForm = activitiRule.getHistoryService().createHistoricDetailQuery().formProperties().listPage(0
, 100);
for (HistoricDetail historicDetail : historicDetailsForm) {
logger.info("historicDetail = {}", toString(historicDetail));
}
logger.info("historicDetailsForm.size", historicDetailsForm.size());
}
private void showHistoricTask() {
List<HistoricTaskInstance> historicTaskInstances =
activitiRule.getHistoryService().createHistoricTaskInstanceQuery().listPage(0, 100);
for (HistoricTaskInstance historicTaskInstance : historicTaskInstances) {
logger.info("historicTaskInstance = {}", historicTaskInstance);
}
logger.info("historicTaskInstances.size", historicTaskInstances.size());
}
private void showHistoricVariable() {
List<HistoricVariableInstance> historicVariableInstances =
activitiRule.getHistoryService().createHistoricVariableInstanceQuery().listPage(0, 100);
for (HistoricVariableInstance historicVariableInstance : historicVariableInstances) {
logger.info("historicVariableInstance = {}", historicVariableInstance);
}
logger.info("historicVariableInstances.size", historicVariableInstances.size());
}
private void showHistoricActivity() {
List<HistoricActivityInstance> historicActivityInstances =
activitiRule.getHistoryService().createHistoricActivityInstanceQuery().listPage(0, 100);
for (HistoricActivityInstance historicActivityInstance : historicActivityInstances) {
logger.info("historicActivityInstance = {}", historicActivityInstance);
}
logger.info("historicActivityInstances.size", historicActivityInstances.size());
}
private void submitFommTask() {
Task task = activitiRule.getTaskService().createTaskQuery().singleResult();
Map<String, String> properties = Maps.newHashMap();
properties.put("formKey1", "valuef1");
properties.put("formKey2", "valuef2");
activitiRule.getFormService().submitTaskFormData(task.getId(), properties);
}
private void changeVariable() {
List<Execution> executions = activitiRule.getRuntimeService().createExecutionQuery().listPage(0, 100);
for (Execution execution : executions) {
logger.info("execution = {}", execution);
}
logger.info("executions.size = {}", executions.size());
String id = executions.iterator().next().getId();
activitiRule.getRuntimeService().setVariable(id, "keyStart1", "value1_");
}
private void startProcess() {
Map<String, Object> params = Maps.newHashMap();
params.put("keyStart1", "value1");
params.put("keyStart2", "value2");
ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("my-process");
}
static String toString(HistoricDetail historicDetail) {
return ToStringBuilder.reflectionToString(historicDetail, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
- 配置:activiti_history.cfg.xml
```xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
<property name="commandInvoker" ref="commandInvoker"/>
<!-- <property name="history" value="none"></property>-->
<!-- <property name="history" value="activity"></property>-->
<!-- <property name="history" value="audit"></property>-->
<property name="history" value="full"></property>
</bean>
<bean id="commandInvoker" class="com.laogoubi.interceptor.MDCCommandInvoker"></bean>
</beans>