Activiti中定时器事件实例

本实例演示怎样使用activiti中的定时器事件和java服务任务。流程启动后(同时设置两个定时器的触发时间),等待第一个定时器事件触发,然后进入下一个任务,然后结束该任务(通过线程来模拟)且修改第二个定时器事件的时间,等待第二个定时器被触发,执行下一个java服务任务,最后结束。

本实例演示怎样使用activiti中的定时器事件和java服务任务。流程启动后(同时设置两个定时器的触发时间),等待第一个定时器事件触发,然后进入下一个任务,然后结束该任务(通过线程来模拟)且修改第二个定时器事件的时间,等待第二个定时器被触发,执行下一个java服务任务,最后结束。

本实例的目的是为了证明定时器事件的时间是可以动态进行设置的,这在工作中非常重要。流程图如下:

Activiti中定时器事件实例

activiti配置文件内容如下(activiti.cfg.xml):

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
	xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://www.springframework.org/schema/beans
		https://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
	
    <bean id="processEngineConfiguration"  
        class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti" />
        <property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
        <property name="jdbcUsername" value="root" />
        <property name="jdbcPassword" value="aaaaaa" />
        <property name="jobExecutorActivate" value="true" />
        <property name="databaseType" value="mysql"/>
        <property name="databaseSchemaUpdate" value="false" />
        <property name="history" value="full"/>
    </bean>
    
</beans>

流程定义XML文件内容如下(startTimerEvent01.bpmn20.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: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="startTimerEvent01" name="定时器开始事件" isExecutable="true">
    <startEvent id="start" name="开始"></startEvent>
    <intermediateCatchEvent id="waitTimer1">
      <timerEventDefinition>
        <timeDate>${myDate1}</timeDate>
      </timerEventDefinition>
    </intermediateCatchEvent>
    <serviceTask id="checkLog" name="检查日志" activiti:expression="${myBean.execute()}"></serviceTask>
    <endEvent id="sid-FB551A38-B458-488B-8887-DA7A73A1D937" name="结束"></endEvent>
    <userTask id="userTask" name="用户任务"></userTask>
    <intermediateCatchEvent id="waitTimer2">
      <timerEventDefinition>
        <timeDate>${myDate2}</timeDate>
      </timerEventDefinition>
    </intermediateCatchEvent>
    <sequenceFlow id="flow01" sourceRef="start" targetRef="waitTimer1"></sequenceFlow>
    <sequenceFlow id="flow02" sourceRef="waitTimer1" targetRef="userTask"></sequenceFlow>
    <sequenceFlow id="flow03" sourceRef="userTask" targetRef="waitTimer2"></sequenceFlow>
    <sequenceFlow id="flow4" sourceRef="waitTimer2" targetRef="checkLog"></sequenceFlow>
    <sequenceFlow id="flow05" sourceRef="checkLog" targetRef="sid-FB551A38-B458-488B-8887-DA7A73A1D937"></sequenceFlow>
  </process>
  
  <bpmndi:BPMNDiagram id="BPMNDiagram_startTimerEvent01">
    <bpmndi:BPMNPlane bpmnElement="startTimerEvent01" id="BPMNPlane_startTimerEvent01">
      <bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start">
        <omgdc:Bounds height="30.0" width="30.0" x="160.0" y="130.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="waitTimer1" id="BPMNShape_waitTimer1">
        <omgdc:Bounds height="30.0" width="30.0" x="235.0" y="130.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="checkLog" id="BPMNShape_checkLog">
        <omgdc:Bounds height="80.0" width="100.0" x="545.0" y="105.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-FB551A38-B458-488B-8887-DA7A73A1D937" id="BPMNShape_sid-FB551A38-B458-488B-8887-DA7A73A1D937">
        <omgdc:Bounds height="28.0" width="28.0" x="690.0" y="131.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="userTask" id="BPMNShape_userTask">
        <omgdc:Bounds height="80.0" width="100.0" x="315.0" y="105.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="waitTimer2" id="BPMNShape_waitTimer2">
        <omgdc:Bounds height="30.0" width="30.0" x="475.0" y="130.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="505.0" y="145.0"></omgdi:waypoint>
        <omgdi:waypoint x="545.0" y="145.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow01" id="BPMNEdge_flow01">
        <omgdi:waypoint x="190.0" y="145.0"></omgdi:waypoint>
        <omgdi:waypoint x="235.0" y="145.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow02" id="BPMNEdge_flow02">
        <omgdi:waypoint x="265.0" y="145.0"></omgdi:waypoint>
        <omgdi:waypoint x="315.0" y="145.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow03" id="BPMNEdge_flow03">
        <omgdi:waypoint x="415.0" y="145.0"></omgdi:waypoint>
        <omgdi:waypoint x="475.0" y="145.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow05" id="BPMNEdge_flow05">
        <omgdi:waypoint x="645.0" y="145.0"></omgdi:waypoint>
        <omgdi:waypoint x="690.0" y="145.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

activiti的java服务任务调用的普通javabean代码如下(MyBeanStartTimerEvent.java):

package com.bug315.bean;

import java.io.Serializable;

public class MyBeanStartTimerEvent implements Serializable {

	private static final long serialVersionUID = 1L;

	public void execute(){
		System.out.println("execute()");
	}
	
}

客户端java代码如下(StartTimerEvent01.java):

package com.bug315;

import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.ProcessEngineImpl;
import org.activiti.engine.task.Task;

import com.bug315.bean.MyBeanStartTimerEvent;

public class StartTimerEvent01 {

	public static void main(String[] args) throws Exception {
		ProcessEngineImpl engine = (ProcessEngineImpl)ProcessEngines.getDefaultProcessEngine();
		
		// 启动JobExecutor
		System.out.println("启动JobExecutor...");
		engine.getProcessEngineConfiguration().getJobExecutor().start();
		
		RepositoryService repositoryService = engine.getRepositoryService();
		RuntimeService runtimeService = engine.getRuntimeService();
		TaskService taskService = engine.getTaskService();
		
		// 部署流程
		System.out.println("部署流程...");
		repositoryService.createDeployment().addClasspathResource("bpmn/startTimerEvent01.bpmn20.xml").deploy();
		
		// 启动流程
		System.out.println("开始启动流程...");
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("myDate1", "2015-02-25T20:34:00");
		variables.put("myDate2", "2015-02-25T20:35:00");
		variables.put("myBean", new MyBeanStartTimerEvent());
		runtimeService.startProcessInstanceByKey("startTimerEvent01", variables);
		
		// 查询第一个任务(需要等到第一个定时器触发后才能得到任务)
		Thread.sleep(1000*60);
		
		Task task = taskService.createTaskQuery().singleResult();
		System.out.println("task = " + task);
		System.out.println("任务ID:" + task.getId() + "  任务名称:" + task.getName());
		taskService.complete(task.getId());
		
		// 重置第二个定时器的时间
		Thread.sleep(1000 * 5);
		Map<String, Object> variables2 = new HashMap<String, Object>();
		variables2.put("myDate2", "2015-02-25T20:37:00");
		runtimeService.setVariables(task.getExecutionId(), variables2);
		
		// 流程结束
		System.out.println("流程结束...");
	}
	
}

运行结果如下:

    启动JobExecutor...

    部署流程...

    4750 [main] INFO org.activiti.engine.impl.bpmn.deployer.BpmnDeployer - Processing resource bpmn/startTimerEvent01.bpmn20.xml

    开始启动流程...

    task = Task[id=1407, name=用户任务]

    任务ID:1407  任务名称:用户任务

    流程结束...

    execute() // 注意:这个是activiti java服务任务调用后的输出的

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