开始定时事件(Start Event Timer)指在开始事件中指定定时器,在符合指定的时间时流程会自动启动。下面通过实例来学习该事件。
图标:
流程图:
流程将在设定的时间点启动流程,然后流程进入“任务01”(需要调用complete方法),最后流程结束。
流程定义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="timerStartEventTest01" name="定时开始事件测试01" isExecutable="true"> <startEvent id="start"> <timerEventDefinition> <timeDate>2015-03-08T15:30:30</timeDate> </timerEventDefinition> </startEvent> <userTask id="task01" name="任务01"></userTask> <endEvent id="end"></endEvent> <sequenceFlow id="sid-B96A1813-6837-4845-8167-3ACA1A3B3D20" sourceRef="start" targetRef="task01"></sequenceFlow> <sequenceFlow id="sid-35F14E43-5255-4FCC-8962-209578CDE088" sourceRef="task01" targetRef="end"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_timerStartEventTest01"> <bpmndi:BPMNPlane bpmnElement="timerStartEventTest01" id="BPMNPlane_timerStartEventTest01"> <bpmndi:BPMNShape bpmnElement="task01" id="BPMNShape_task01"> <omgdc:Bounds height="80.0" width="100.0" x="191.0" y="125.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end"> <omgdc:Bounds height="28.0" width="28.0" x="336.0" y="151.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start"> <omgdc:Bounds height="30.0" width="30.0" x="100.0" y="150.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="sid-35F14E43-5255-4FCC-8962-209578CDE088" id="BPMNEdge_sid-35F14E43-5255-4FCC-8962-209578CDE088"> <omgdi:waypoint x="291.0" y="165.0"></omgdi:waypoint> <omgdi:waypoint x="336.0" y="165.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="sid-B96A1813-6837-4845-8167-3ACA1A3B3D20" id="BPMNEdge_sid-B96A1813-6837-4845-8167-3ACA1A3B3D20"> <omgdi:waypoint x="130.0" y="165.0"></omgdi:waypoint> <omgdi:waypoint x="191.0" y="165.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>
Activiti配置文件:
<?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="true" /> <property name="history" value="full"/> </bean> </beans>
Java客户端代码:
package com.bug315.event; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.TaskService; import org.activiti.engine.impl.ProcessEngineImpl; import org.activiti.engine.task.TaskQuery; /** * 定时开始事件测试 * @author Administrator * */ public class TimerStartEventTest01 { public static void main(String[] args) throws InterruptedException { ProcessEngineImpl processEngine = (ProcessEngineImpl)ProcessEngines.getDefaultProcessEngine(); processEngine.getProcessEngineConfiguration().getJobExecutor().start(); RepositoryService repositoryService = processEngine.getRepositoryService(); repositoryService.createDeployment().addClasspathResource("bpmn/timerStartEventTest01.bpmn20.xml").deploy(); System.out.println("流程部署成功"); // 暂停60秒 Thread.sleep(1000 * 60); TaskService taskService = processEngine.getTaskService(); TaskQuery taskQuery = taskService.createTaskQuery(); System.out.println("task size = " + taskQuery.list().size()); processEngine.getProcessEngineConfiguration().getJobExecutor().shutdown(); } }
注意事项:
1、使用定时器开始事件需要启动JobExecutor