一 代码
/**
* Copyright (C), 2020-2020, 软件公司
* FileName: MessageEvent.java
* Author: cakin
* Date: 2020/4/23
* Description: messageEventReceived触发消息事件
*/
package com.syc.activiti.chapter9;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.Execution;
/**
* @className: MessageEvent
* @description: messageEventReceived触发消息事件
* @date: 2020/4/23
* @author: cakin
*/
public class MessageEvent {
public static void main( String[] args ) {
// 创建流程引擎
ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
// 得到流程存储服务实例
RepositoryService repositoryService = engine.getRepositoryService();
// 得到运行时服务组件
RuntimeService runtimeService = engine.getRuntimeService();
// 部署流程描述文件
repositoryService.createDeployment()
.addClasspathResource("bpmn/chapter9/MessageEvent.bpmn").deploy();
// 开始流流程
runtimeService.startProcessInstanceByKey("testProcess");
// 查询当前节点
Execution exe = runtimeService.createExecutionQuery()
.activityId("messageintermediatecatchevent1").singleResult();
/****************************************************************************
mysql> select ID_, REV_,PROC_INST_ID_ ,BUSINESS_KEY_ ,PARENT_ID_ ,PROC_DEF_ID_ ,SUPER_EXEC_,ROOT_PROC_INST_ID_ ,ACT_ID_ from act_ru_execution;
+--------+------+---------------+---------------+------------+----------------------+-------------+--------------------+--------------------------------+
| ID_ | REV_ | PROC_INST_ID_ | BUSINESS_KEY_ | PARENT_ID_ | PROC_DEF_ID_ | SUPER_EXEC_ | ROOT_PROC_INST_ID_ | ACT_ID_ |
+--------+------+---------------+---------------+------------+----------------------+-------------+--------------------+--------------------------------+
| 195005 | 1 | 195005 | NULL | NULL | testProcess:2:195004 | NULL | 195005 | NULL |
| 195006 | 1 | 195005 | NULL | 195005 | testProcess:2:195004 | NULL | 195005 | messageintermediatecatchevent1 |
+--------+------+---------------+---------------+------------+----------------------+-------------+--------------------+--------------------------------+
mysql> select * from act_ru_event_subscr;
+--------+------+-------------+-------------+---------------+---------------+--------------------------------+----------------+-------------------------+----------------------+------------+
| ID_ | REV_ | EVENT_TYPE_ | EVENT_NAME_ | EXECUTION_ID_ | PROC_INST_ID_ | ACTIVITY_ID_ | CONFIGURATION_ | CREATED_ | PROC_DEF_ID_ | TENANT_ID_ |
+--------+------+-------------+-------------+---------------+---------------+--------------------------------+----------------+-------------------------+----------------------+------------+
| 195009 | 1 | message | testMsg | 195006 | 195005 | messageintermediatecatchevent1 | NULL | 2020-04-23 20:51:18.247 | testProcess:2:195004 | |
+--------+------+-------------+-------------+---------------+---------------+--------------------------------+----------------+-------------------------+----------------------+------------+
1 row in set (0.00 sec)
*****************************************************************************/
System.out.println("当前流程节点:" + exe.getActivityId());
// 触发消息事件
runtimeService.messageEventReceived("testMsg", exe.getId());
// 查询当前事件
exe = runtimeService.createExecutionQuery().activityId("usertask1")
.singleResult();
/****************************************************************************
mysql> select * from act_ru_event_subscr;
+--------+------+-------------+-------------+---------------+---------------+--------------------------------+----------------+-------------------------+----------------------+------------+
| ID_ | REV_ | EVENT_TYPE_ | EVENT_NAME_ | EXECUTION_ID_ | PROC_INST_ID_ | ACTIVITY_ID_ | CONFIGURATION_ | CREATED_ | PROC_DEF_ID_ | TENANT_ID_ |
+--------+------+-------------+-------------+---------------+---------------+--------------------------------+----------------+-------------------------+----------------------+------------+
| 195009 | 1 | message | testMsg | 195006 | 195005 | messageintermediatecatchevent1 | NULL | 2020-04-23 20:51:18.247 | testProcess:2:195004 | |
+--------+------+-------------+-------------+---------------+---------------+--------------------------------+----------------+-------------------------+----------------------+------------+
1 row in set (0.00 sec)
mysql> select ID_, REV_,PROC_INST_ID_ ,BUSINESS_KEY_ ,PARENT_ID_ ,PROC_DEF_ID_ ,SUPER_EXEC_,ROOT_PROC_INST_ID_ ,ACT_ID_ from act_ru_execution;
+--------+------+---------------+---------------+------------+----------------------+-------------+--------------------+-----------+
| ID_ | REV_ | PROC_INST_ID_ | BUSINESS_KEY_ | PARENT_ID_ | PROC_DEF_ID_ | SUPER_EXEC_ | ROOT_PROC_INST_ID_ | ACT_ID_ |
+--------+------+---------------+---------------+------------+----------------------+-------------+--------------------+-----------+
| 195005 | 1 | 195005 | NULL | NULL | testProcess:2:195004 | NULL | 195005 | NULL |
| 195006 | 2 | 195005 | NULL | 195005 | testProcess:2:195004 | NULL | 195005 | usertask1 |
+--------+------+---------------+---------------+------------+----------------------+-------------+--------------------+-----------+
2 rows in set (0.00 sec)
mysql> select * from act_ru_event_subscr;
Empty set (0.00 sec)
mysql> select ID_ ,REV_, EXECUTION_ID_, PROC_INST_ID_ , PROC_DEF_ID_, NAME_ , PARENT_TASK_ID_ , DESCRIPTION_ , TASK_DEF_KEY_ , OWNER_, ASSIGNEE_ from act_ru_task;
+--------+------+---------------+---------------+----------------------+----------+-----------------+--------------+---------------+--------+-----------+
| ID_ | REV_ | EXECUTION_ID_ | PROC_INST_ID_ | PROC_DEF_ID_ | NAME_ | PARENT_TASK_ID_ | DESCRIPTION_ | TASK_DEF_KEY_ | OWNER_ | ASSIGNEE_ |
+--------+------+---------------+---------------+----------------------+----------+-----------------+--------------+---------------+--------+-----------+
| 195011 | 1 | 195006 | 195005 | testProcess:2:195004 | End Task | NULL | NULL | usertask1 | NULL | NULL |
+--------+------+---------------+---------------+----------------------+----------+-----------------+--------------+---------------+--------+-----------+
1 row in set (0.00 sec)
*****************************************************************************/
System.out.println("当前流程节点:" + exe.getActivityId());
}
}
二 流程
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn"
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"
xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema"
expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<message id="testMsg" name="testMsg"></message>
<process id="testProcess" name="testProcess">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="End Task"></userTask>
<intermediateCatchEvent id="messageintermediatecatchevent1"
name="MessageCatchEvent">
<messageEventDefinition messageRef="testMsg"></messageEventDefinition>
</intermediateCatchEvent>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow1" name="" sourceRef="startevent1"
targetRef="messageintermediatecatchevent1"></sequenceFlow>
<sequenceFlow id="flow2" name=""
sourceRef="messageintermediatecatchevent1" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow3" name="" sourceRef="usertask1"
targetRef="endevent1"></sequenceFlow>
</process>
</definitions>
三 结果
当前流程节点:messageintermediatecatchevent1
当前流程节点:usertask1
来源:oschina
链接:https://my.oschina.net/u/4323266/blog/3652285