Activiti定时中间事件入门实例

定时中间事件指在流程中将一个定时器作为独立的节点来运行。当流程到达定时中间事件节点的时候,流程会等待事件到达,只有当事件到达了才会触发定时事件,然后流程继续向下走。下面定义了一个简单的流程来测试如何使用定时中间事件。

定时中间事件指在流程中将一个定时器作为独立的节点来运行。当流程到达定时中间事件节点的时候,流程会等待事件到达,只有当事件到达了才会触发定时事件,然后流程继续向下走。下面定义了一个简单的流程来测试如何使用定时中间事件。

定时中间事件图标:

定时中间事件

实例流程图:

定时中间事件测试流程图

启动流程进入“接收订单”,然后进入定时器(等待1分钟)。如果时间到时触发定时器,然后进入“发货”任务,最后完成流程。

流程XML定义:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="https://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="https://www.w3.org/2001/XMLSchema" xmlns:activiti="https://activiti.org/bpmn" xmlns:bpmndi="https://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="https://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="https://www.omg.org/spec/DD/20100524/DI" typeLanguage="https://www.w3.org/2001/XMLSchema" expressionLanguage="https://www.w3.org/1999/XPath" targetNamespace="https://www.activiti.org/processdef">
  
  <process id="timerIntermediateEventTest01" name="定时器中间事件测试01" isExecutable="true">
    <startEvent id="start"></startEvent>
    <userTask id="task01" name="接收订单"></userTask>
    <!-- 定时器中间事件 -->
    <intermediateCatchEvent id="timer01">
      <timerEventDefinition>
        <timeDuration>PT1M</timeDuration>
      </timerEventDefinition>
    </intermediateCatchEvent>
    <userTask id="task02" name="发货"></userTask>
    <endEvent id="end"></endEvent>
    
    <sequenceFlow id="f2" sourceRef="task01" targetRef="timer01"></sequenceFlow>
    <sequenceFlow id="f3" sourceRef="timer01" targetRef="task02"></sequenceFlow>
    <sequenceFlow id="f4" sourceRef="task02" targetRef="end"></sequenceFlow>
    <sequenceFlow id="f1" sourceRef="start" targetRef="task01"></sequenceFlow>
  </process>
  
  <bpmndi:BPMNDiagram id="BPMNDiagram_timerIntermediateEventTest01">
    <bpmndi:BPMNPlane bpmnElement="timerIntermediateEventTest01" id="BPMNPlane_timerIntermediateEventTest01">
      <bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start">
        <omgdc:Bounds height="30.0" width="30.0" x="135.0" y="78.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="task01" id="BPMNShape_task01">
        <omgdc:Bounds height="80.0" width="100.0" x="215.25" y="53.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="timer01" id="BPMNShape_timer01">
        <omgdc:Bounds height="31.0" width="31.0" x="360.0" y="77.5"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="task02" id="BPMNShape_task02">
        <omgdc:Bounds height="80.0" width="100.0" x="435.0" y="53.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end">
        <omgdc:Bounds height="28.0" width="28.0" x="585.0" y="79.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="f1" id="BPMNEdge_f1">
        <omgdi:waypoint x="165.0" y="93.0"></omgdi:waypoint>
        <omgdi:waypoint x="215.25" y="93.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="f3" id="BPMNEdge_f3">
        <omgdi:waypoint x="391.9998316666579" y="93.42660627675845"></omgdi:waypoint>
        <omgdi:waypoint x="435.0" y="93.22935779816514"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="f2" id="BPMNEdge_f2">
        <omgdi:waypoint x="315.25" y="93.0"></omgdi:waypoint>
        <omgdi:waypoint x="360.0" y="93.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="f4" id="BPMNEdge_f4">
        <omgdi:waypoint x="535.0" y="93.0"></omgdi:waypoint>
        <omgdi:waypoint x="585.0" y="93.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

Java客户端代码:

package com.bug315.event;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;

/**
 * 定时器中间事件测试
 * @author Administrator
 *
 */
public class TimerIntermediateEventTest01 {
	public static void main(String[] args) throws Exception {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		engine.getRepositoryService().createDeployment()
			.addClasspathResource("bpmn/timerIntermediateEventTest01.bpmn20.xml").deploy();
		engine.getRuntimeService().startProcessInstanceByKey("timerIntermediateEventTest01");
		
		TaskService taskService = engine.getTaskService();
		TaskQuery taskQuery = taskService.createTaskQuery();
		Task task = taskQuery.singleResult();
		System.out.println("task name: " + task.getName());
		taskService.complete(task.getId());
		
		System.out.println("暂停70秒钟");
		Thread.sleep(1000 * 70);
		
		task = taskQuery.singleResult();
		System.out.println("task name: " + task.getName());
		taskService.complete(task.getId());
	}
}

注意事项:

1、使用定时器必须启动JobExecutor (设置activiti.cfg.xml配置中jobExecutorActivate=true)

2、该事件会一直等待被触发

一寸光阴一寸金,寸金难买寸光阴。——《增广贤文》
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站专注于 Java、数据库(MySQL、Oracle)、Linux、软件架构及大数据等多领域技术知识分享。涵盖丰富的原创与精选技术文章,助力技术传播与交流。无论是技术新手渴望入门,还是资深开发者寻求进阶,这里都能为您提供深度见解与实用经验,让复杂编码变得轻松易懂,携手共赴技术提升新高度。如有侵权,请来信告知:hxstrive@outlook.com
公众号