此方式是以ISO 8601格式在指定的固定日期触发。在这里我们新建模块flowable-bpmn,接下来我们的案例都将在本模块中演示。
关于ISO 8601的一些知识可以参看文档:国际标准时间格式ISO8601 - Yungyu - 博客园。
1 开始事件TimerStartEvent
项目的pom.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>flowable-study</artifactId>
<groupId>com.dream21th</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>flowable-bpmn</artifactId>
<dependencies>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-engine</artifactId>
<version>6.6.0</version>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring</artifactId>
<version>6.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.28</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
在src/main/resources下面新建文件SpringTransactionIntegration-context.xml,文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/flowable_spring?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="processEngineConfiguration" class="org.flowable.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseSchemaUpdate" value="true" />
<property name="asyncExecutorActivate" value="true" />
<!-- 部署是否生成图片,默认(true)生成图片,对应数据库表:ACT_GE_BYTEARRAY-->
<property name="createDiagramOnDeploy" value="false" />
<!-- 部署设置中文宋体-->
<property name="activityFontName" value="宋体" />
<property name="labelFontName" value="宋体" />
<property name="annotationFontName" value="宋体" />
</bean>
<bean id="processEngine" class="org.flowable.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />
</beans>
新建timeDate.bpmn20.xml流程定义文件,内容如下:
<?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:xsd="http://www.w3.org/2001/XMLSchema" 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">
<process id="timeDate" name="固定时间触发" isExecutable="true">
<startEvent id="timerstartevent1" name="Timer start">
<timerEventDefinition>
<timeDate>2021-10-11T17:59:14</timeDate>
</timerEventDefinition>
</startEvent>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow1" sourceRef="timerstartevent1" targetRef="servicetask1"></sequenceFlow>
<serviceTask id="servicetask1" name="自动审批" activiti:class="com.dream21th.flowable.bpmn.time.TimeDateDelegate"></serviceTask>
<sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_timeDate">
<bpmndi:BPMNPlane bpmnElement="timeDate" id="BPMNPlane_timeDate">
<bpmndi:BPMNShape bpmnElement="timerstartevent1" id="BPMNShape_timerstartevent1">
<omgdc:Bounds height="35.0" width="35.0" x="210.0" y="220.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="620.0" y="220.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
<omgdc:Bounds height="55.0" width="105.0" x="370.0" y="210.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="245.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="370.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="475.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="620.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
这个流程定义的含义是,流程会在2021-10-11T17:59:14自动启动。
我们在测试代码里面写如下代码:
package com.dream21th.flowable.bpmn.time;
import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.DeploymentBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:SpringTransactionIntegration-context.xml")
public class Demo {
private ProcessEngine processEngine;
private TaskService taskService;
private RuntimeService runtimeService;
private RepositoryService repositoryService;
private HistoryService historyService;
private DynamicBpmnService dynamicBpmnService;
private FormService formService;
private IdentityService identityService;
private ManagementService managementService;
private ProcessEngineConfiguration processEngineConfiguration;
@Before
public void testProcessEngine() {
processEngine = ProcessEngines.getDefaultProcessEngine();
System.out.println("流程引擎类:" + processEngine);
taskService = processEngine.getTaskService();
runtimeService = processEngine.getRuntimeService();
repositoryService = processEngine.getRepositoryService();
historyService = processEngine.getHistoryService();
dynamicBpmnService = processEngine.getDynamicBpmnService();
formService = processEngine.getFormService();
identityService = processEngine.getIdentityService();
managementService = processEngine.getManagementService();
processEngineConfiguration = processEngine.getProcessEngineConfiguration();
String name = processEngine.getName();
System.out.println("流程引擎的名称: " + name);
System.out.println(processEngineConfiguration);
}
/**
* 关闭流程引擎
*/
@After
public void close() {
processEngine.close();
}
/**
* classpath方式部署 涉及三张表:ACT_RE_PROCDEF,ACT_RE_DEPLOYMENT,ACT_GE_BYTEARRAY
*/
@Test
public void deploy() throws InterruptedException {
DeploymentBuilder deploymentBuilder = repositoryService
.createDeployment()
.category("timeDate")
.name("timeDate")
.addClasspathResource("timeDate.bpmn20.xml");
Deployment deploy = deploymentBuilder.deploy();
System.out.println("classpath方式部署,流程ID: " + deploy.getId());
TimeUnit.MINUTES.sleep(2);
}
/**
* 启动流程实例
*
*/
@Test
public void startProcessInstanceByKey() throws Exception{
String processDefinitionKey = "timeDate";
Map<String, Object> variables = new HashMap();
runtimeService.startProcessInstanceByKey(processDefinitionKey,variables);
TimeUnit.MINUTES.sleep(2);
}
}
流程在2021-10-11T17:59:14之前部署完成,不需要手动启动流程,流程会如设置时间自动开启。
注意:自动开启的条件是流程部署要在设定时间前;配置文件中的asyncExecutorActivate要设置成true。
2 中间事件TimerCatchingEvent
编写流程定义timeDateImterdiate.bpmn20.xml,内容如下:
<?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:xsd="http://www.w3.org/2001/XMLSchema" 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">
<process id="timeDateImterdiate" name="固定时间触发" isExecutable="true">
<endEvent id="endevent1" name="End"></endEvent>
<serviceTask id="servicetask1" name="自动任务一" activiti:class="com.dream21th.flowable.bpmn.time.TimeDateDelegate"></serviceTask>
<startEvent id="startevent1" name="Start"></startEvent>
<sequenceFlow id="flow3" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
<serviceTask id="servicetask2" name="自动任务二" activiti:class="com.dream21th.flowable.bpmn.time.TimeDateDelegate"></serviceTask>
<sequenceFlow id="flow5" sourceRef="servicetask2" targetRef="endevent1"></sequenceFlow>
<intermediateCatchEvent id="timerintermediatecatchevent1" name="TimerCatchEvent">
<timerEventDefinition>
<timeDate>2021-10-11T21:44:14</timeDate>
</timerEventDefinition>
</intermediateCatchEvent>
<sequenceFlow id="flow6" sourceRef="servicetask1" targetRef="timerintermediatecatchevent1"></sequenceFlow>
<sequenceFlow id="flow7" sourceRef="timerintermediatecatchevent1" targetRef="servicetask2"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_timeDate">
<bpmndi:BPMNPlane bpmnElement="timeDateImterdiate" id="BPMNPlane_timeDate">
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="640.0" y="220.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
<omgdc:Bounds height="55.0" width="105.0" x="250.0" y="210.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="120.0" y="220.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="servicetask2" id="BPMNShape_servicetask2">
<omgdc:Bounds height="55.0" width="105.0" x="500.0" y="210.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="timerintermediatecatchevent1" id="BPMNShape_timerintermediatecatchevent1">
<omgdc:Bounds height="35.0" width="35.0" x="410.0" y="220.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="155.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="250.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
<omgdi:waypoint x="605.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="640.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
<omgdi:waypoint x="355.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="410.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
<omgdi:waypoint x="445.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="500.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
核心代码,表示第一个任务完成后,第二个任务要到2021-10-11T21:44:14后才会执行,下面是执行结果
<intermediateCatchEvent id="timerintermediatecatchevent1" name="TimerCatchEvent">
<timerEventDefinition>
<timeDate>2021-10-11T21:44:14</timeDate>
</timerEventDefinition>
</intermediateCatchEvent>
3 边界事件TimerBoundaryEvent
编写流程定义timeDateBoundary.bpmn20.xml,流程定义内容如下:
<?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:xsd="http://www.w3.org/2001/XMLSchema" 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">
<process id="timeDateBoundary" name="固定时间触发" isExecutable="true">
<endEvent id="endevent1" name="End"></endEvent>
<startEvent id="startevent1" name="Start"></startEvent>
<serviceTask id="servicetask2" name="自动任务一" activiti:class="com.dream21th.flowable.bpmn.time.JavaOneDelegate"></serviceTask>
<sequenceFlow id="flow5" sourceRef="servicetask2" targetRef="endevent1"></sequenceFlow>
<userTask id="usertask1" name="人工任务"></userTask>
<boundaryEvent id="boundarytimer1" name="Timer" attachedToRef="usertask1" cancelActivity="true">
<timerEventDefinition>
<timeDate>2021-10-11T22:05:14</timeDate>
</timerEventDefinition>
</boundaryEvent>
<serviceTask id="servicetask3" name="自动任务二" activiti:class="com.dream21th.flowable.bpmn.time.JavaTwoDelegate"></serviceTask>
<sequenceFlow id="flow6" sourceRef="usertask1" targetRef="servicetask2"></sequenceFlow>
<sequenceFlow id="flow7" sourceRef="boundarytimer1" targetRef="servicetask3"></sequenceFlow>
<endEvent id="endevent2" name="End"></endEvent>
<sequenceFlow id="flow8" sourceRef="servicetask3" targetRef="endevent2"></sequenceFlow>
<sequenceFlow id="flow9" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_timeDateImterdiate">
<bpmndi:BPMNPlane bpmnElement="timeDateImterdiate" id="BPMNPlane_timeDateImterdiate">
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="640.0" y="220.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="120.0" y="220.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="servicetask2" id="BPMNShape_servicetask2">
<omgdc:Bounds height="55.0" width="105.0" x="500.0" y="210.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="250.0" y="210.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="boundarytimer1" id="BPMNShape_boundarytimer1">
<omgdc:Bounds height="30.0" width="30.0" x="320.0" y="260.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="servicetask3" id="BPMNShape_servicetask3">
<omgdc:Bounds height="55.0" width="105.0" x="500.0" y="340.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent2" id="BPMNShape_endevent2">
<omgdc:Bounds height="35.0" width="35.0" x="640.0" y="350.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
<omgdi:waypoint x="605.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="640.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
<omgdi:waypoint x="355.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="500.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
<omgdi:waypoint x="335.0" y="290.0"></omgdi:waypoint>
<omgdi:waypoint x="334.0" y="367.0"></omgdi:waypoint>
<omgdi:waypoint x="500.0" y="367.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
<omgdi:waypoint x="605.0" y="367.0"></omgdi:waypoint>
<omgdi:waypoint x="640.0" y="367.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
<omgdi:waypoint x="155.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="250.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
实际流程图如下:
核心代码解析,如果人工任务超过2021-10-11T22:05:14还没有处理,就会触发。
<boundaryEvent id="boundarytimer1" name="Timer" attachedToRef="usertask1" cancelActivity="true">
<timerEventDefinition>
<timeDate>2021-10-11T22:05:14</timeDate>
</timerEventDefinition>
</boundaryEvent>