这篇博客对Activiti流程引擎的HistoryLevel(历史记录级别)进行介绍。
在test目录中创建ConfigHistoryLevelTest测试类:
在config的resources中创建activiti_history.cfg.xml文件:
配置文件 activiti_history.cfg.xml 中内容如下:
在ActivitiRule创建时路径修改为我们创建的 activiti_history.cfg.xml 文件
我们测试类的执行步骤如下:
1、启动流程
2、修改变量
3、提交表单 task
4、输出历史内容
5、输出历史用户任务
6、输出历史表单
7、输出历史详情
ConfigHistoryLevelTest测试类如下:
package com.jjf.activiti.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;
/**
* HistoryLevel测试
*/
public class ConfigHistoryLevelTest {
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigHistoryLevelTest.class);
@Rule
public ActivitiRule activitiRule = new ActivitiRule("activiti_history.cfg.xml"); //我们配置使用activiti_history.cfg.xml文件
@Test
@Deployment(resources = {"com/jjf/activiti/my-process.bpmn20.xml"}) //单元测试启动之前将这个定义文件部署到流程引擎中
public void test() {
//启动流程
startProcessInstance();
//修改变量
changeVariable();
//提交表单 task
submitTaskFormData();
//输出历史内容
//输出历史活动
showHistoryActivity();
//输出历史的变量
showHistoryVariable();
//输出历史用户任务
showHistoryTask();
//输出历史表单
showHistoryForm();
//输出历史详情
showHistoryDetails();
}
/**
* 输出历史的详情
*/
private void showHistoryDetails() {
List historicDetails = activitiRule.getHistoryService()
.createHistoricDetailQuery()
.listPage(0,100);
for (HistoricDetail historicDetail:historicDetails){
LOGGER.info("historicDetail: [{}]",toString(historicDetail)); //对输出进行格式化,让输出看起来更简洁一些
}
LOGGER.info("historicDetail的长度为: [{}]",historicDetails.size());
}
/**
* 输出历史表单
*/
private void showHistoryForm() {
List historicDetailsForm = activitiRule.getHistoryService()
.createHistoricDetailQuery()
.formProperties().listPage(0,100); //提交表单的详情
for (HistoricDetail historicDetail:historicDetailsForm){
LOGGER.info("historicDetail: [{}]",toString(historicDetail));
}
LOGGER.info("historicDetailsForm的长度为: [{}]",historicDetailsForm.size());
}
/**
* 输出历史用户任务
*/
private void showHistoryTask() {
List historicTaskInstances = activitiRule.getHistoryService()
.createHistoricTaskInstanceQuery().listPage(0,100);
for (HistoricTaskInstance historicTaskInstance:historicTaskInstances){
LOGGER.info("historicTaskInstance: [{}]",historicTaskInstance);
}
LOGGER.info("historicTaskInstances的长度为: [{}]",historicTaskInstances.size());
}
/**
* 输出历史的变量
*/
private void showHistoryVariable() {
List historicVariableInstances = activitiRule.getHistoryService()
.createHistoricVariableInstanceQuery().listPage(0,100);
for (HistoricVariableInstance historicVariableInstance:historicVariableInstances){
LOGGER.info("historicVariableInstance: [{}]",historicVariableInstance);
}
LOGGER.info("historicVariableInstances的长度为: [{}]",historicVariableInstances.size());
}
/**
*输出历史活动内容
*/
private void showHistoryActivity() {
List historicActivityInstances = activitiRule.getHistoryService()
.createHistoricActivityInstanceQuery()
.listPage(0,100);
for (HistoricActivityInstance historicActivityInstance:historicActivityInstances){
LOGGER.info("historicActivityInstance: [{}]",historicActivityInstance);
}
LOGGER.info("historicActivityInstances的长度为: [{}]",historicActivityInstances.size());
}
/**
* 提交表单
*/
private void submitTaskFormData() {
Task task = activitiRule.getTaskService().createTaskQuery().singleResult(); //任务结点
Map properties = Maps.newHashMap(); //作为我们form表单的内容
properties.put("formkey1t1","formvalue1"); //向form表单中添加两条属性
properties.put("formkey1t2","formvalue2");
activitiRule.getFormService().submitTaskFormData(task.getId(),properties); //将form表单提交,注意这里参数要String,String类型
}
/**
* 修改变量
*/
private void changeVariable() {
List executions = activitiRule.getRuntimeService()
.createExecutionQuery().listPage(0,100); //查出执行情况,设置分页查询的条件。从0条数据开始,只查询100条
for(Execution execution:executions){
LOGGER.info("execution: [{}]",execution);
}
LOGGER.info("execution的长度为: [{}]",executions.size()); //输出execution的记录数
String id = executions.iterator().next().getId(); //取出第一条记录
activitiRule.getRuntimeService().setVariable(id,"keyStart1","value1_change"); //我们将keyStart1这个键的值修改为value1_change
}
/**
* 启动流程
*/
private void startProcessInstance() {
Map params = Maps.newHashMap(); //作为我们启动传入的参数
params.put("keyStart1","value1"); //定义了一个键值对参数
params.put("keyStart2","value2");
ProcessInstance processInstance = activitiRule.getRuntimeService()
.startProcessInstanceByKey("my-process",params); //作为启动的参数传入
}
static String toString(HistoricDetail historicDetail){
return ToStringBuilder.reflectionToString(historicDetail, ToStringStyle.SHORT_PREFIX_STYLE); //返回SHORT_PREFIX_STYLE格式
}
}
输出结果如下:
historicActivityInstance: [HistoricActivityInstanceEntity[id=15, activityId=end, activityName=null]]
historicActivityInstance: [HistoricActivityInstanceEntity[id=8, activityId=start, activityName=null]]
historicActivityInstance: [HistoricActivityInstanceEntity[id=9, activityId=someTask, activityName=Activiti is awesome!]]
historicActivityInstances的长度为: [3]
historicVariableInstance: [HistoricVariableInstanceEntity[id=13, name=formkey1t1, revision=0, type=string, textValue=formvalue1]]
historicVariableInstance: [HistoricVariableInstanceEntity[id=14, name=formkey1t2, revision=0, type=string, textValue=formvalue2]]
historicVariableInstance: [HistoricVariableInstanceEntity[id=5, name=keyStart2, revision=0, type=string, textValue=value2]]
historicVariableInstance: [HistoricVariableInstanceEntity[id=6, name=keyStart1, revision=1, type=string, textValue=value1_change]]
historicVariableInstances的长度为: [4]
historicTaskInstance: [org.activiti.engine.impl.persistence.entity.HistoricTaskInstanceEntityImpl@3113a37]
historicTaskInstances的长度为: [1]
historicDetail: [HistoricFormPropertyEntityImpl[propertyId=formkey1t1,propertyValue=formvalue1,processInstanceId=4,activityInstanceId=9,taskId=10,executionId=7,time=Thu Dec 20 15:41:18 CST 2018,detailType=FormProperty,id=11,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetail: [HistoricFormPropertyEntityImpl[propertyId=formkey1t2,propertyValue=formvalue2,processInstanceId=4,activityInstanceId=9,taskId=10,executionId=7,time=Thu Dec 20 15:41:19 CST 2018,detailType=FormProperty,id=12,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetailsForm的长度为: [2]
historicDetail: [HistoricFormPropertyEntityImpl[propertyId=formkey1t1,propertyValue=formvalue1,processInstanceId=4,activityInstanceId=9,taskId=10,executionId=7,time=Thu Dec 20 15:41:18 CST 2018,detailType=FormProperty,id=11,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetail: [HistoricFormPropertyEntityImpl[propertyId=formkey1t2,propertyValue=formvalue2,processInstanceId=4,activityInstanceId=9,taskId=10,executionId=7,time=Thu Dec 20 15:41:19 CST 2018,detailType=FormProperty,id=12,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetail的长度为: [2]
Process finished with exit code 0
historicActivityInstance:可以看到有三个历史活动,长度为3:分别对应end,start,someTask三个结点。
默认情况下可以获取普通任务、历史任务与表单。
修改 activiti_history.cfg.xml 文件:
添加
ConfigHistoryLevelTest测试类输出结果:
Execution tree while executing operation class org.activiti.engine.impl.agenda.EndExecutionOperation :
4 (process instance)
└── 7 : end (EndEvent, parent id 4 (active)
historicActivityInstances的长度为: [0]
historicVariableInstances的长度为: [0]
historicTaskInstances的长度为: [0]
historicDetailsForm的长度为: [0]
historicDetail的长度为: [0]
Process finished with exit code 0
可以看到所有长度都为0。为none,比较容易理解。
将history的value值改为activity即可:
执行结果如下:
historicActivityInstance: [HistoricActivityInstanceEntity[id=13, activityId=end, activityName=null]]
historicActivityInstance: [HistoricActivityInstanceEntity[id=8, activityId=start, activityName=null]]
historicActivityInstance: [HistoricActivityInstanceEntity[id=9, activityId=someTask, activityName=Activiti is awesome!]]
historicActivityInstances的长度为: [3]
historicVariableInstance: [HistoricVariableInstanceEntity[id=11, name=formkey1t1, revision=0, type=string, textValue=formvalue1]]
historicVariableInstance: [HistoricVariableInstanceEntity[id=12, name=formkey1t2, revision=0, type=string, textValue=formvalue2]]
historicVariableInstance: [HistoricVariableInstanceEntity[id=5, name=keyStart2, revision=0, type=string, textValue=value2]]
historicVariableInstance: [HistoricVariableInstanceEntity[id=6, name=keyStart1, revision=1, type=string, textValue=value1_change]]
historicVariableInstances的长度为: [4]
historicTaskInstances的长度为: [0]
historicDetailsForm的长度为: [0]
historicDetail的长度为: [0]
Process finished with exit code 0
可以看出有流程变量为4:
[HistoricVariableInstanceEntity[id=6, name=keyStart1, revision=1, type=string, textValue=value1_change]] 只能看到修改后的值,并看不到修改之前的值。
将history的value值改为audit即可:
输出结果如下:
historicActivityInstance: [HistoricActivityInstanceEntity[id=15, activityId=end, activityName=null]]
historicActivityInstance: [HistoricActivityInstanceEntity[id=8, activityId=start, activityName=null]]
historicActivityInstance: [HistoricActivityInstanceEntity[id=9, activityId=someTask, activityName=Activiti is awesome!]]
historicActivityInstances的长度为: [3]
historicVariableInstance: [HistoricVariableInstanceEntity[id=13, name=formkey1t1, revision=0, type=string, textValue=formvalue1]]
historicVariableInstance: [HistoricVariableInstanceEntity[id=14, name=formkey1t2, revision=0, type=string, textValue=formvalue2]]
historicVariableInstance: [HistoricVariableInstanceEntity[id=5, name=keyStart2, revision=0, type=string, textValue=value2]]
historicVariableInstance: [HistoricVariableInstanceEntity[id=6, name=keyStart1, revision=1, type=string, textValue=value1_change]]
historicVariableInstances的长度为: [4]
historicTaskInstance: [org.activiti.engine.impl.persistence.entity.HistoricTaskInstanceEntityImpl@3113a37]
historicTaskInstances的长度为: [1]
historicDetail: [HistoricFormPropertyEntityImpl[propertyId=formkey1t1,propertyValue=formvalue1,processInstanceId=4,activityInstanceId=9,taskId=10,executionId=7,time=Thu Dec 20 16:23:50 CST 2018,detailType=FormProperty,id=11,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetail: [HistoricFormPropertyEntityImpl[propertyId=formkey1t2,propertyValue=formvalue2,processInstanceId=4,activityInstanceId=9,taskId=10,executionId=7,time=Thu Dec 20 16:23:50 CST 2018,detailType=FormProperty,id=12,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetailsForm的长度为: [2]
historicDetail: [HistoricFormPropertyEntityImpl[propertyId=formkey1t1,propertyValue=formvalue1,processInstanceId=4,activityInstanceId=9,taskId=10,executionId=7,time=Thu Dec 20 16:23:50 CST 2018,detailType=FormProperty,id=11,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetail: [HistoricFormPropertyEntityImpl[propertyId=formkey1t2,propertyValue=formvalue2,processInstanceId=4,activityInstanceId=9,taskId=10,executionId=7,time=Thu Dec 20 16:23:50 CST 2018,detailType=FormProperty,id=12,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetail的长度为: [2]
Process finished with exit code 0
将history的value值改为full即可:
输出结果如下:
historicActivityInstance: [HistoricActivityInstanceEntity[id=10, activityId=start, activityName=null]]
historicActivityInstance: [HistoricActivityInstanceEntity[id=11, activityId=someTask, activityName=Activiti is awesome!]]
historicActivityInstance: [HistoricActivityInstanceEntity[id=20, activityId=end, activityName=null]]
historicActivityInstances的长度为: [3]
historicVariableInstance: [HistoricVariableInstanceEntity[id=16, name=formkey1t1, revision=0, type=string, textValue=formvalue1]]
historicVariableInstance: [HistoricVariableInstanceEntity[id=18, name=formkey1t2, revision=0, type=string, textValue=formvalue2]]
historicVariableInstance: [HistoricVariableInstanceEntity[id=5, name=keyStart2, revision=0, type=string, textValue=value2]]
historicVariableInstance: [HistoricVariableInstanceEntity[id=7, name=keyStart1, revision=1, type=string, textValue=value1_change]]
historicVariableInstances的长度为: [4]
historicTaskInstance: [org.activiti.engine.impl.persistence.entity.HistoricTaskInstanceEntityImpl@70eecdc2]
historicTaskInstances的长度为: [1]
historicDetail: [HistoricFormPropertyEntityImpl[propertyId=formkey1t1,propertyValue=formvalue1,processInstanceId=4,activityInstanceId=11,taskId=12,executionId=9,time=Thu Dec 20 16:26:27 CST 2018,detailType=FormProperty,id=14,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetail: [HistoricFormPropertyEntityImpl[propertyId=formkey1t2,propertyValue=formvalue2,processInstanceId=4,activityInstanceId=11,taskId=12,executionId=9,time=Thu Dec 20 16:26:27 CST 2018,detailType=FormProperty,id=15,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetailsForm的长度为: [2]
historicDetail: [HistoricDetailVariableInstanceUpdateEntityImpl[revision=1,name=keyStart1,variableType=org.activiti.engine.impl.variable.StringType@2575f671,longValue=,doubleValue=,textValue=value1_change,textValue2=,byteArrayRef=ByteArrayRef[id=null, name=null, entity=null],cachedValue=,processInstanceId=4,activityInstanceId=,taskId=,executionId=4,time=Thu Dec 20 16:26:26 CST 2018,detailType=VariableUpdate,id=13,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetail: [HistoricFormPropertyEntityImpl[propertyId=formkey1t1,propertyValue=formvalue1,processInstanceId=4,activityInstanceId=11,taskId=12,executionId=9,time=Thu Dec 20 16:26:27 CST 2018,detailType=FormProperty,id=14,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetail: [HistoricFormPropertyEntityImpl[propertyId=formkey1t2,propertyValue=formvalue2,processInstanceId=4,activityInstanceId=11,taskId=12,executionId=9,time=Thu Dec 20 16:26:27 CST 2018,detailType=FormProperty,id=15,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetail: [HistoricDetailVariableInstanceUpdateEntityImpl[revision=0,name=formkey1t1,variableType=org.activiti.engine.impl.variable.StringType@2575f671,longValue=,doubleValue=,textValue=formvalue1,textValue2=,byteArrayRef=ByteArrayRef[id=null, name=null, entity=null],cachedValue=,processInstanceId=4,activityInstanceId=11,taskId=,executionId=4,time=Thu Dec 20 16:26:27 CST 2018,detailType=VariableUpdate,id=17,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetail: [HistoricDetailVariableInstanceUpdateEntityImpl[revision=0,name=formkey1t2,variableType=org.activiti.engine.impl.variable.StringType@2575f671,longValue=,doubleValue=,textValue=formvalue2,textValue2=,byteArrayRef=ByteArrayRef[id=null, name=null, entity=null],cachedValue=,processInstanceId=4,activityInstanceId=11,taskId=,executionId=4,time=Thu Dec 20 16:26:27 CST 2018,detailType=VariableUpdate,id=19,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetail: [HistoricDetailVariableInstanceUpdateEntityImpl[revision=0,name=keyStart2,variableType=org.activiti.engine.impl.variable.StringType@2575f671,longValue=,doubleValue=,textValue=value2,textValue2=,byteArrayRef=ByteArrayRef[id=null, name=null, entity=null],cachedValue=,processInstanceId=4,activityInstanceId=,taskId=,executionId=4,time=Thu Dec 20 16:26:26 CST 2018,detailType=VariableUpdate,id=6,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetail: [HistoricDetailVariableInstanceUpdateEntityImpl[revision=0,name=keyStart1,variableType=org.activiti.engine.impl.variable.StringType@2575f671,longValue=,doubleValue=,textValue=value1,textValue2=,byteArrayRef=ByteArrayRef[id=null, name=null, entity=null],cachedValue=,processInstanceId=4,activityInstanceId=,taskId=,executionId=4,time=Thu Dec 20 16:26:26 CST 2018,detailType=VariableUpdate,id=8,isInserted=false,isUpdated=false,isDeleted=false]]
historicDetail的长度为: [7]
Process finished with exit code 0
这里跟audit最大的区别就是 historicDetail 这部分。最重要的看 historicDetail ,除了两条和audit一样输出的HistoricFormPropertyEntityImpl,其他的五条都是HistoricDetailVariableInstanceUpdateEntityImpl,可以看到属性与变量的修改。