定时开始事件用于在指定时间启动一个流程,或者在指定周期内循环启动多次流程,如在2023年8月25号10点发起年度目标审核流程,或每月1号启动财务结算处理流程。当满足设定的时间条件时,定时启动时间就会被触发,从而启动流程。
注意:
1、子流程中不能嵌入定时开始事件
2、 定时开始事件会自动触发流程启动,无需调用API启动流程,如果调用会启动两个流程实例
3、 如果定时开始事件的流程重新部署,新的流程定义会冲刷掉老的流程定义,定时作业也会刷新,再起自动启动流程会启动新的流程定义
4、 如果想使用定时启动事件需要开启作业任务 配置如下:configuration.setAsyncExecutorActivate(true);
注意
定时开始事件的XML内容是普通开始事件的定义中嵌入一个定时事件。定时开始事件的定义格式如下三种形式:
如:在2023-08-11T12:13:14执行启动该流程
<startEvent id="start">
<timerEventDefinition>
<timeDate>2023-08-11T12:13:14timeDate>
timerEventDefinition>
startEvent>
示例:等待10天执行
<startEvent id="start">
<timerEventDefinition>
<timeDuration>P10DtimeDuration>
timerEventDefinition>
startEvent>
示例:重复3次,每次间隔10小时
<startEvent id="start">
<timerEventDefinition>
<timeCycle>R3/PT10HtimeCycle>
timerEventDefinition>
startEvent>
示例:如每月月初启动一个任务
<startEvent id="start" >
<timerEventDefinition>
<timeCycle>0 0 2 1 * ?timeCycle>
timerEventDefinition>
startEvent>
<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/processdef">
<process id="TimerStartEventProcess" name="定时开始事件示例流程" isExecutable="true">
<startEvent id="start">
<timerEventDefinition>
<timeDuration>PT1MtimeDuration>
timerEventDefinition>
startEvent>
<userTask id="task1" name="数据上报">userTask>
<endEvent id="end">endEvent>
<sequenceFlow id="sequenceFlow1" sourceRef="start" targetRef="task1">sequenceFlow>
<sequenceFlow id="sequenceFlow2" sourceRef="task1" targetRef="end">sequenceFlow>
process>
<bpmndi:BPMNDiagram id="BPMNDiagram_TimerStartEventProcess">
<bpmndi:BPMNPlane bpmnElement="TimerStartEventProcess" id="BPMNPlane_TimerStartEventProcess">
<bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start">
<omgdc:Bounds height="31.0" width="31.0" x="100.0" y="150.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="task1" id="BPMNShape_task1">
<omgdc:Bounds height="80.0" width="100.0" x="255.0" y="125.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end">
<omgdc:Bounds height="28.0" width="28.0" x="480.0" y="151.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sequenceFlow1" id="BPMNEdge_sequenceFlow1">
<omgdi:waypoint x="130.99994604632707" y="165.45910304473264">omgdi:waypoint>
<omgdi:waypoint x="255.0" y="165.13192612137203">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sequenceFlow2" id="BPMNEdge_sequenceFlow2">
<omgdi:waypoint x="355.0" y="165.0">omgdi:waypoint>
<omgdi:waypoint x="480.0" y="165.0">omgdi:waypoint>
bpmndi:BPMNEdge>
bpmndi:BPMNPlane>
bpmndi:BPMNDiagram>
definitions>
@Slf4j
@SpringBootTest(classes = ProdFlowBpmnUiAdminApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class TimeStartEventTest extends AbstractFlowableEngine {
@Test
public void testTimeDuration() throws Exception{
ProcessDefinition processDefinition = deployByClasspathResource("startEvent/TimerStartEventProcess.bpmn20.xml");
Thread.sleep(1000*90);
TaskQuery taskQuery = taskService.createTaskQuery().processDefinitionId(processDefinition.getId());
List<Task> list = taskQuery.list();
if (CollectionUtils.isNotEmpty(list)) {
list.forEach(task -> {
log.info("任务名称为:{}", task.getName());
});
}
}
}
<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/processdef">
<process id="TimeCycleStartEventProcess" name="定时开始事件示例流程" isExecutable="true">
<startEvent id="start">
<timerEventDefinition>
<timeCycle>0 40 10 ? * *timeCycle>
timerEventDefinition>
startEvent>
<userTask id="task1" name="数据上报">userTask>
<endEvent id="end">endEvent>
<sequenceFlow id="sequenceFlow1" sourceRef="start" targetRef="task1">sequenceFlow>
<sequenceFlow id="sequenceFlow2" sourceRef="task1" targetRef="end">sequenceFlow>
process>
<bpmndi:BPMNDiagram id="BPMNDiagram_TimerStartEventProcess">
<bpmndi:BPMNPlane bpmnElement="TimerStartEventProcess" id="BPMNPlane_TimerStartEventProcess">
<bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start">
<omgdc:Bounds height="31.0" width="31.0" x="100.0" y="150.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="task1" id="BPMNShape_task1">
<omgdc:Bounds height="80.0" width="100.0" x="255.0" y="125.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end">
<omgdc:Bounds height="28.0" width="28.0" x="480.0" y="151.0">omgdc:Bounds>
bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sequenceFlow1" id="BPMNEdge_sequenceFlow1">
<omgdi:waypoint x="130.99994604632707" y="165.45910304473264">omgdi:waypoint>
<omgdi:waypoint x="255.0" y="165.13192612137203">omgdi:waypoint>
bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sequenceFlow2" id="BPMNEdge_sequenceFlow2">
<omgdi:waypoint x="355.0" y="165.0">omgdi:waypoint>
<omgdi:waypoint x="480.0" y="165.0">omgdi:waypoint>
bpmndi:BPMNEdge>
bpmndi:BPMNPlane>
bpmndi:BPMNDiagram>
definitions>
@Test
public void testTimeCycle() throws Exception {
ProcessDefinition processDefinition = this.deployByClasspathResource("startEvent/TimeCycleStartEventProcess.bpmn20.xml");
Thread.sleep(1000 * 70);
List<Task> list = taskService.createTaskQuery().processDefinitionId(processDefinition.getId()).list();
if (CollectionUtils.isNotEmpty(list)) {
list.forEach(task -> log.info("任务名称为:{}", task.getName()));
}
}
定时启动事件,对企业规范化操作是非常有帮助的,通过梳理业务流程,我们能清晰的知道哪些审批流程在什么时间点可以自动触发,大大节约了成本,起到降本增效之目的。
注意:
timeDate 和timeDuration 只能执行一次,如果需要定时重复执行的话,需要使用周期定时启动 timeCycle