Activiti6.0流程引擎学习——(17)Activiti流程引擎的事件处理&eventLog事件日志

activiti 同时被 2 个专栏收录
29 篇文章 8 订阅

首先跟上篇博客一样先创建单元测试类  ConfigEventLogTest。

其中内容修改如下:

package com.jjf.activiti.config;

import org.activiti.engine.event.EventLogEntry;
import org.activiti.engine.logging.LogMDC;
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.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;


/**
 * EventLog 事件日志测试
 */
public class ConfigEventLogTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(ConfigEventLogTest.class);

    @Rule
    public ActivitiRule activitiRule = new ActivitiRule("activiti_eventlog.cfg.xml");    //我们配置使用activiti_eventlog.cfg.xml文件

    @Test
    @Deployment(resources = {"com/jjf/activiti/my-process.bpmn20.xml"})    //单元测试启动之前将这个定义文件部署到流程引擎中
    public void test() {
        ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("my-process");
        Task task = activitiRule.getTaskService().createTaskQuery().singleResult();
        activitiRule.getTaskService().complete(task.getId());   //执行Task

        List<EventLogEntry> eventLogEntries = activitiRule.getManagementService()
                .getEventLogEntriesByProcessInstanceId(processInstance.getProcessInstanceId());   //基于流程实例id去取

        for (EventLogEntry eventLogEntry:eventLogEntries){
            LOGGER.info("eventlog的类型 = [{}],eventLog的内容 = [{}]",eventLogEntry.getType(),new String(eventLogEntry.getData()));    //将事件日志全部输出看看
        }
        LOGGER.info("eventLog的计数条数: [{}]",eventLogEntries.size());
    }
}

这里要注意一下配置文件: activiti_eventlog.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="jdbcUrl" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000;MVCC=TRUE" />
    <property name="jdbcDriver" value="org.h2.Driver" />
    <property name="jdbcUsername" value="sa" />
    <property name="jdbcPassword" value="" />

    <property name="commandInvoker" ref="commandInvoker"/>    <!-- 使用我们自己定义的拦截器 -->
    <property name="enableDatabaseEventLogging" value="true"/>    <!-- 这里我们将事件日志开启 -->
  </bean>     <!-- 默认数据库的配置  h2数据库 -->

  <bean id="commandInvoker" class="com.jjf.activiti.interceptor.MDCCommandInvoker"/>

</beans>

然后我们执行看看此时的输出结果:

Loading XML bean definitions from class path resource [activiti_eventlog.cfg.xml]
Activiti 5 compatibility handler implementation not found or error during instantiation : org.activiti.compatibility.DefaultActiviti5CompatibilityHandler. Activiti 5 backwards compatibility disabled.
performing create on engine with resource org/activiti/db/create/activiti.h2.create.engine.sql
performing create on history with resource org/activiti/db/create/activiti.h2.create.history.sql
performing create on identity with resource org/activiti/db/create/activiti.h2.create.identity.sql
ProcessEngine default created
Execution tree while executing operation class org.activiti.engine.impl.agenda.ContinueProcessOperation :

4 (process instance)
└── 5 : start (StartEvent, parent id 4 (active)

Execution tree while executing operation class org.activiti.engine.impl.agenda.TakeOutgoingSequenceFlowsOperation :

4 (process instance)
└── 5 : start (StartEvent, parent id 4 (active)

Execution tree while executing operation class org.activiti.engine.impl.agenda.ContinueProcessOperation :

4 (process instance)
└── 5 : start -> someTask, parent id 4 (active)

Execution tree while executing operation class org.activiti.engine.impl.agenda.ContinueProcessOperation :

4 (process instance)
└── 5 : someTask (UserTask, parent id 4 (active)

Execution tree while executing operation class org.activiti.engine.impl.agenda.TriggerExecutionOperation :

4 (process instance)
└── 5 : someTask (UserTask, parent id 4 (active)

Execution tree while executing operation class org.activiti.engine.impl.agenda.TakeOutgoingSequenceFlowsOperation :

4 (process instance)
└── 5 : someTask (UserTask, parent id 4 (active)

Execution tree while executing operation class org.activiti.engine.impl.agenda.ContinueProcessOperation :

4 (process instance)
└── 5 : someTask -> end, parent id 4 (active)

Execution tree while executing operation class org.activiti.engine.impl.agenda.ContinueProcessOperation :

4 (process instance)
└── 5 : end (EndEvent, parent id 4 (active)

Execution tree while executing operation class org.activiti.engine.impl.agenda.TakeOutgoingSequenceFlowsOperation :

4 (process instance)
└── 5 : end (EndEvent, parent id 4 (active)

Execution tree while executing operation class org.activiti.engine.impl.agenda.EndExecutionOperation :

4 (process instance)
└── 5 : end (EndEvent, parent id 4 (active)

eventlog的类型 = [PROCESSINSTANCE_START],eventLog的内容 = [{"timeStamp":1545363519269,"processDefinitionId":"my-process:1:3","createTime":1545363519269,"id":"4"}]
eventlog的类型 = [ACTIVITY_STARTED],eventLog的内容 = [{"timeStamp":1545363519290,"activityId":"start","processDefinitionId":"my-process:1:3","processInstanceId":"4","executionId":"5","behaviorClass":"org.activiti.engine.impl.bpmn.behavior.NoneStartEventActivityBehavior","activityType":"startEvent"}]
eventlog的类型 = [ACTIVITY_COMPLETED],eventLog的内容 = [{"timeStamp":1545363519298,"activityId":"start","processDefinitionId":"my-process:1:3","processInstanceId":"4","executionId":"5","behaviorClass":"org.activiti.engine.impl.bpmn.behavior.NoneStartEventActivityBehavior","activityType":"startEvent"}]
eventlog的类型 = [SEQUENCEFLOW_TAKEN],eventLog的内容 = [{"targetActivityId":"someTask","timeStamp":1545363519303,"targetActivityBehaviorClass":"org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior","sourceActivityType":"org.activiti.bpmn.model.StartEvent","targetActivityName":"Activiti is awesome!","id":"flow1","sourceActivityBehaviorClass":"org.activiti.engine.impl.bpmn.behavior.NoneStartEventActivityBehavior","targetActivityType":"org.activiti.bpmn.model.UserTask","sourceActivityId":"start"}]
eventlog的类型 = [ACTIVITY_STARTED],eventLog的内容 = [{"timeStamp":1545363519304,"activityId":"someTask","processDefinitionId":"my-process:1:3","processInstanceId":"4","executionId":"5","behaviorClass":"org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior","activityName":"Activiti is awesome!","activityType":"userTask"}]
eventlog的类型 = [TASK_CREATED],eventLog的内容 = [{"timeStamp":1545363519351,"taskDefinitionKey":"someTask","processDefinitionId":"my-process:1:3","processInstanceId":"4","executionId":"5","createTime":1545363519304,"name":"Activiti is awesome!","id":"8","priority":50}]
eventlog的类型 = [TASK_COMPLETED],eventLog的内容 = [{"duration":609,"timeStamp":1545363519913,"taskDefinitionKey":"someTask","processDefinitionId":"my-process:1:3","processInstanceId":"4","executionId":"5","createTime":1545363519304,"name":"Activiti is awesome!","id":"8","priority":50}]
eventlog的类型 = [ACTIVITY_COMPLETED],eventLog的内容 = [{"timeStamp":1545363519962,"activityId":"someTask","processDefinitionId":"my-process:1:3","processInstanceId":"4","executionId":"5","behaviorClass":"org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior","activityName":"Activiti is awesome!","activityType":"userTask"}]
eventlog的类型 = [SEQUENCEFLOW_TAKEN],eventLog的内容 = [{"targetActivityId":"end","timeStamp":1545363519964,"sourceActivityName":"Activiti is awesome!","targetActivityBehaviorClass":"org.activiti.engine.impl.bpmn.behavior.NoneEndEventActivityBehavior","sourceActivityType":"org.activiti.bpmn.model.UserTask","id":"flow2","sourceActivityBehaviorClass":"org.activiti.engine.impl.bpmn.behavior.UserTaskActivityBehavior","targetActivityType":"org.activiti.bpmn.model.EndEvent","sourceActivityId":"someTask"}]
eventlog的类型 = [ACTIVITY_STARTED],eventLog的内容 = [{"timeStamp":1545363519965,"activityId":"end","processDefinitionId":"my-process:1:3","processInstanceId":"4","executionId":"5","behaviorClass":"org.activiti.engine.impl.bpmn.behavior.NoneEndEventActivityBehavior","activityType":"endEvent"}]
eventlog的类型 = [ACTIVITY_COMPLETED],eventLog的内容 = [{"timeStamp":1545363519966,"activityId":"end","processDefinitionId":"my-process:1:3","processInstanceId":"4","executionId":"5","behaviorClass":"org.activiti.engine.impl.bpmn.behavior.NoneEndEventActivityBehavior","activityType":"endEvent"}]
eventlog的类型 = [PROCESSINSTANCE_END],eventLog的内容 = [{"timeStamp":1545363519996,"processDefinitionId":"my-process:1:3","id":"4","endTime":1545363519996}]
eventLog的计数条数: [12]

Process finished with exit code 0

可以查询到12条记录,这些就是整个流程进行状态的流转。

eventlog的类型

eventlog的内容含义

PROCESSINSTANCE_START流程引擎启动
ACTIVITY_STARTE启动一个activiti的结点
ACTIVITY_COMPLETED activiti的一个结点完成
SEQUENCEFLOW_TAKEN   流(也就是没两个结点之间的连接线)
TASK_CREATED 创建一个Task事件
TASK_COMPLETED Task事件完成
PROCESSINSTANCE_END流程引擎执行完成

 

 

 

我们可以从这里  看出这12条记录就是my-process.bpmn20.xml中流程图的执行过程。

我们这里是把 enableDatabaseEventLogging 的值设为true的输出结果,那如果是false又会怎么样?为了了解activiti底层做了什么我们可以来查看一下源码。

首先我们直接按住control左键点击enableDatabaseEventLogging:

会打开 ProcessEngineConfigurationImpl 类到此处。

快速定位到以下代码:

    public void initDatabaseEventLogging() {
        if (this.enableDatabaseEventLogging) {
            this.getEventDispatcher().addEventListener(new EventLogger(this.clock, this.objectMapper));
        }

    }

当enableDatabaseEventLogging参数配置为true时,才会向下执行,创建一个事件监听器。当我们监听到某些事件的时候则执行eventLog操作。若是为false则不执行。

接下来我们来测试一下将value值改为false会输出什么:

果然输出的eventLog记录条数为0:

 

  • 0
    点赞
  • 0
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 1024 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值