SpringBoot整合Activiti7——定时器事件(九)

文章目录

  • 定时器事件
    • 时间定义
      • 时间固定
      • 时间段
      • 时间周期
    • 1.开始事件
    • 2.中间事件
    • 3.边界事件
    • 代码实现
      • xml文件
      • 自定义服务任务监听器
      • 自定义用户任务监听器
      • 测试流程
      • 流程执行步骤


定时器事件

可以用在开始事件、中间事件、边界事件上,边界事件可以是中断和非中断边界事件

需要开启异步任务配置:全局配置类中 config.setAsyncExecutorActivate(true);

触发时间后会在 act_ru_timer_job 表中记录,事件结束后自动删除。

时间定义

时间固定

遵循ISO 8601标准

<timerEventDefinition>
  <timeDate>2023-08-19T09:24:20timeDate>
timerEventDefinition>

时间段

P表示日期的开始(年月日),T表示时间的开始(时分秒)

  • P10DT5M:表示每10天5分钟为一个周期
  • PM10M:每10分钟为一个周期
  • PT1M:一分钟后执行一次
  • PT20S:20秒后执行一次
<timerEventDefinition>
  <timeDuration>P10DT5MtimeDuration>
timerEventDefinition>

时间周期

  • R3/PT10H表示:重复3次,每次间隔10小时
  • R3/PT3S:循环3次,间隔3秒
  • R/PT1H:每隔1小时执行一次
<timerEventDefinition>
  <timeCycle>R3/PT10HtimeCycle>
timerEventDefinition>

1.开始事件

使用场景:流程只需要启动一次、流程需要在特定的时间间隔重复启动。

  • 子流程不能有定时开始事件
  • 流程部署后马上开始定时事件,不需要手动启动流程,如果手动启动后会额外启动一个流程
  • 当部署带有定时器启动事件的流程的更新版本时,上一版本的定时器作业会被移除

<startEvent id="sid-eac7f836-a8c2-4120-bfc7-4b06c0705e8b" name="定时开始事件">
  <timerEventDefinition>
    
    <timeDate>2023-08-19T09:24:20timeDate>
    

  timerEventDefinition>
startEvent>

2.中间事件

使用场景:定时器中间事件是一个Catching事件,当执行到达捕获事件节点, 就会启动一个定时器,并一直等待触发,只有到达指定时间定时器才被触发。


<intermediateCatchEvent id="sid-018de371-80da-4706-a948-ac10f838f9d0" name="定时器中间事件">
  <timerEventDefinition>
    
    <timeDuration>PT20StimeDuration>
  timerEventDefinition>
intermediateCatchEvent>

3.边界事件

使用场景:当某个用户任务或者子流程在规定的时间后还没有执行。那么我们就可以通过定时器边界事件来触发执行特定的处理流程。

其中cancelActivity属性,用于说明该事件是否为中断事件。cancelActivity属性值默认为true,表示它是边界中断事件,当该边界事件触发时,它所依附的活动实例被终止,原有的执行流会被中断,流程将沿边界事件的外出顺序流继续流转。如果将其设置为false,表示它是边界非中断事件,当边界事件触发时,则原来的执行流仍然存在,所依附的活动实例继续执行,同时也执行边界事件的外出顺序流。


<boundaryEvent id="sid-0f29bc51-9400-4b01-a13c-e9c37b2bec0a" attachedToRef="sid-8c894b30-98d9-455d-9766-2a8e22f4e7ab" cancelActivity="true" name="定时器边界事件">
  <timerEventDefinition>
    
    <timeDuration>PT1MtimeDuration>
  timerEventDefinition>
boundaryEvent>

代码实现

定时器开始事件 - 自动任务 - 定时器中间事件 - 用户任务(定时器边界事件非中断)- 用户任务(定时器边界事件中断)- 结束
SpringBoot整合Activiti7——定时器事件(九)_第1张图片

xml文件


<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="timer" name="定时器事件" isExecutable="true">
    <documentation>模拟定时器事件documentation>
    
    <startEvent id="sid-eac7f836-a8c2-4120-bfc7-4b06c0705e8b" name="定时开始事件">
      <timerEventDefinition>
        
        <timeDate>2023-08-19T11:35:00timeDate>
        

      timerEventDefinition>
    startEvent>
    <sequenceFlow id="sid-aa41edf6-f2e4-414b-8758-3d49d5d09dd5" sourceRef="sid-eac7f836-a8c2-4120-bfc7-4b06c0705e8b" targetRef="sid-dbdad760-92b3-4561-a1e7-9401197b7aa7"/>
    
    <serviceTask id="sid-dbdad760-92b3-4561-a1e7-9401197b7aa7" activiti:exclusive="true" name="服务任务" activiti:delegateExpression="${myServiceTaskListener}"/>
    <sequenceFlow id="sid-56d3c5eb-4e3f-4904-bf20-250241539247" sourceRef="sid-dbdad760-92b3-4561-a1e7-9401197b7aa7" targetRef="sid-018de371-80da-4706-a948-ac10f838f9d0"/>
    
    <intermediateCatchEvent id="sid-018de371-80da-4706-a948-ac10f838f9d0" name="定时器中间事件1">
      <timerEventDefinition>
        
        <timeDuration>PT20StimeDuration>
      timerEventDefinition>
    intermediateCatchEvent>
    <sequenceFlow id="sid-6ce61e04-1b4b-43b4-8b42-b392bcf44b17" sourceRef="sid-018de371-80da-4706-a948-ac10f838f9d0" targetRef="sid-8c894b30-98d9-455d-9766-2a8e22f4e7ab"/>
    
    <userTask id="sid-8c894b30-98d9-455d-9766-2a8e22f4e7ab" name="美团外卖骑手">
      <extensionElements>
        <activiti:taskListener event="create" delegateExpression="${myUserTaskListener}"/>
      extensionElements>
    userTask>
    
    <boundaryEvent id="sid-0f29bc51-9400-4b01-a13c-e9c37b2bec0a" attachedToRef="sid-8c894b30-98d9-455d-9766-2a8e22f4e7ab" cancelActivity="false" name="定时器边界事件1">
      <timerEventDefinition>
        
        <timeDuration>PT30StimeDuration>
      timerEventDefinition>
    boundaryEvent>
    
    <endEvent id="sid-7dc6287d-719f-44ae-9f29-158c7320f5f3" name="结束事件1"/>
    
    <endEvent id="sid-e86db864-c95e-4ccb-a670-03fc0689b874" name="结束事件2"/>
    <sequenceFlow id="sid-24f01276-0ed8-4792-abc4-3abc39156b30" sourceRef="sid-8c894b30-98d9-455d-9766-2a8e22f4e7ab" targetRef="sid-fbb3c837-1dca-4bff-85a6-5301f954b6bb"/>
    
    <serviceTask id="sid-f38227c9-d0a8-44f1-8ef0-66c1aa5b2ec0" activiti:exclusive="true" name="通知服务" activiti:delegateExpression="${myServiceTaskListener}"/>
    <sequenceFlow id="sid-99ea1e4d-c0b5-4133-b997-5c77a6486129" sourceRef="sid-0f29bc51-9400-4b01-a13c-e9c37b2bec0a" targetRef="sid-f38227c9-d0a8-44f1-8ef0-66c1aa5b2ec0"/>
    <sequenceFlow id="sid-79f59c59-d979-4928-99e1-5b628fe7652c" sourceRef="sid-f38227c9-d0a8-44f1-8ef0-66c1aa5b2ec0" targetRef="sid-7dc6287d-719f-44ae-9f29-158c7320f5f3"/>
    
    
    <userTask id="sid-fbb3c837-1dca-4bff-85a6-5301f954b6bb" name="饿了么外卖骑手">
      <extensionElements>
        <activiti:taskListener event="create" delegateExpression="${myUserTaskListener}"/>
      extensionElements>
    userTask>
    <sequenceFlow id="sid-9f81e343-9cba-4e5a-b0fe-9c02f655f910" sourceRef="sid-fbb3c837-1dca-4bff-85a6-5301f954b6bb" targetRef="sid-e86db864-c95e-4ccb-a670-03fc0689b874"/>
    
    <boundaryEvent id="sid-72c1a016-1aca-4993-af35-a55d8440da00" attachedToRef="sid-fbb3c837-1dca-4bff-85a6-5301f954b6bb" cancelActivity="true" name="定时器边界事件2">
      <timerEventDefinition>
        
        <timeDuration>PT1MtimeDuration>
      timerEventDefinition>
    boundaryEvent>
    
    <userTask id="sid-20cc70be-f536-42a0-99de-9102a3cadfcb" name="美团优选骑手">
      <extensionElements>
        <activiti:taskListener event="create" delegateExpression="${myUserTaskListener}"/>
      extensionElements>
    userTask>
    <sequenceFlow id="sid-4129ae15-d826-49af-8f31-777315488965" sourceRef="sid-72c1a016-1aca-4993-af35-a55d8440da00" targetRef="sid-20cc70be-f536-42a0-99de-9102a3cadfcb"/>
    <sequenceFlow id="sid-e95dd49a-916b-43fd-8e05-cca9c7111175" sourceRef="sid-20cc70be-f536-42a0-99de-9102a3cadfcb" targetRef="sid-e86db864-c95e-4ccb-a670-03fc0689b874"/>
  process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_timer">
    <bpmndi:BPMNPlane bpmnElement="timer" id="BPMNPlane_timer">
      <bpmndi:BPMNShape id="shape-658aeea0-f6ff-4bc2-b5da-55c5f702fe50" bpmnElement="sid-8c894b30-98d9-455d-9766-2a8e22f4e7ab">
        <omgdc:Bounds x="-174.94717" y="-48.5" width="100.0" height="80.0"/>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape-8566e87c-20a9-4ffb-ac20-ae4451a51fb2" bpmnElement="sid-e86db864-c95e-4ccb-a670-03fc0689b874">
        <omgdc:Bounds x="303.36176" y="-23.500008" width="30.0" height="30.0"/>
      bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-0c609232-fc10-45aa-9616-fdc80154f29a" bpmnElement="sid-24f01276-0ed8-4792-abc4-3abc39156b30">
        <omgdi:waypoint x="-74.947174" y="-8.5"/>
        <omgdi:waypoint x="77.419815" y="-8.500008"/>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-ee857fe8-5832-4ebc-8ed1-a2a7a5e91e71" bpmnElement="sid-0f29bc51-9400-4b01-a13c-e9c37b2bec0a">
        <omgdc:Bounds x="-139.7814" y="0.06927776" width="30.0" height="30.0"/>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape-308c774f-cd05-44f6-aaeb-0f1a7688363f" bpmnElement="sid-eac7f836-a8c2-4120-bfc7-4b06c0705e8b">
        <omgdc:Bounds x="-434.54727" y="-23.5" width="30.0" height="30.0"/>
      bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-1e2b1a10-eb7b-4565-9d4f-cce4a448edc6" bpmnElement="sid-aa41edf6-f2e4-414b-8758-3d49d5d09dd5">
        <omgdi:waypoint x="-404.54727" y="-8.5"/>
        <omgdi:waypoint x="-374.95337" y="-8.500008"/>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-4f4f0129-1c50-4fa1-a1dd-106b0b6631f7" bpmnElement="sid-dbdad760-92b3-4561-a1e7-9401197b7aa7">
        <omgdc:Bounds x="-374.95337" y="-48.5" width="100.0" height="80.0"/>
      bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-44c41995-1aba-4891-bd2e-3db8dc6c07fb" bpmnElement="sid-56d3c5eb-4e3f-4904-bf20-250241539247">
        <omgdi:waypoint x="-274.95337" y="-8.5"/>
        <omgdi:waypoint x="-241.08704" y="-8.5"/>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-a6a5c18e-6800-410b-93eb-b71425264291" bpmnElement="sid-018de371-80da-4706-a948-ac10f838f9d0">
        <omgdc:Bounds x="-241.08705" y="-23.5" width="30.0" height="30.0"/>
      bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-bcd06a8a-aa48-441a-9a81-e18426f5616e" bpmnElement="sid-6ce61e04-1b4b-43b4-8b42-b392bcf44b17">
        <omgdi:waypoint x="-211.08705" y="-8.5"/>
        <omgdi:waypoint x="-174.94717" y="-8.5"/>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-5132461d-88d6-4d15-a46f-ae8950679c6a" bpmnElement="sid-7dc6287d-719f-44ae-9f29-158c7320f5f3">
        <omgdc:Bounds x="29.145199" y="103.683395" width="30.0" height="30.0"/>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape-181bee77-cdac-4c01-bb9c-7cbc3f6ae95a" bpmnElement="sid-f38227c9-d0a8-44f1-8ef0-66c1aa5b2ec0">
        <omgdc:Bounds x="-99.58714" y="78.6834" width="100.0" height="80.0"/>
      bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-c79f62c9-ee85-45e0-a2f6-a4fe6197b4f8" bpmnElement="sid-79f59c59-d979-4928-99e1-5b628fe7652c">
        <omgdi:waypoint x="0.41285706" y="118.6834"/>
        <omgdi:waypoint x="29.145199" y="118.683395"/>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge-311f6c99-60f3-4bd4-a086-be28045fdf12" bpmnElement="sid-99ea1e4d-c0b5-4133-b997-5c77a6486129">
        <omgdi:waypoint x="-124.7814" y="30.069279"/>
        <omgdi:waypoint x="-124.94719" y="118.683395"/>
        <omgdi:waypoint x="-99.58715" y="118.6834"/>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-11c81c88-f78c-4962-abef-f8f55bb68dc5" bpmnElement="sid-fbb3c837-1dca-4bff-85a6-5301f954b6bb">
        <omgdc:Bounds x="77.41982" y="-48.500008" width="100.0" height="80.0"/>
      bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-a7ba3c08-497c-4383-a171-c57c07e777a5" bpmnElement="sid-9f81e343-9cba-4e5a-b0fe-9c02f655f910">
        <omgdi:waypoint x="177.41983" y="-8.500008"/>
        <omgdi:waypoint x="303.36176" y="-8.500008"/>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="shape-0aa2e7ef-ca53-41d1-9cc8-d3d710620a0a" bpmnElement="sid-72c1a016-1aca-4993-af35-a55d8440da00">
        <omgdc:Bounds x="121.671555" y="-1.2286987" width="30.0" height="30.0"/>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="shape-894c438f-317d-463a-8350-2d63c0f33d27" bpmnElement="sid-20cc70be-f536-42a0-99de-9102a3cadfcb">
        <omgdc:Bounds x="170.75076" y="78.683395" width="100.0" height="80.0"/>
      bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="edge-bdc901eb-a8eb-4b4a-85a9-6032f63dee37" bpmnElement="sid-4129ae15-d826-49af-8f31-777315488965">
        <omgdi:waypoint x="136.67155" y="28.771301"/>
        <omgdi:waypoint x="136.67155" y="118.68339"/>
        <omgdi:waypoint x="170.75075" y="118.68339"/>
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="edge-ad52e61b-1a75-4470-a786-e063cf41a7a3" bpmnElement="sid-e95dd49a-916b-43fd-8e05-cca9c7111175">
        <omgdi:waypoint x="270.7508" y="118.6834"/>
        <omgdi:waypoint x="319.42508" y="118.6834"/>
        <omgdi:waypoint x="318.36176" y="6.4999924"/>
      bpmndi:BPMNEdge>
    bpmndi:BPMNPlane>
  bpmndi:BPMNDiagram>
definitions>

自定义服务任务监听器

@Component
public class MyServiceTaskListener implements JavaDelegate, Serializable {

    private static final long serialVersionUID = 1L;

    @Override
    public void execute(DelegateExecution execution) {
        System.out.println("《================服务任务监听器================》");
        System.out.println("当前时间:" + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        System.out.println("delegateTask.getProcessInstanceId() = " + execution.getProcessInstanceId());
        System.out.println("delegateTask.getProcessInstanceBusinessKey() = " + execution.getProcessInstanceBusinessKey());
        System.out.println("delegateTask.getEventName() = " + execution.getEventName());
        System.out.println("execution.getCurrentFlowElement().getName() = " + execution.getCurrentFlowElement().getName());
    }
}

自定义用户任务监听器

@Component
public class MyUserTaskListener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        System.out.println("《================用户任务监听器================》");
        System.out.println("当前时间:" + DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        System.out.println("delegateTask.getProcessInstanceId() = " + delegateTask.getProcessInstanceId());
        System.out.println("delegateTask.getTaskDefinitionKey() = " + delegateTask.getTaskDefinitionKey());
        System.out.println("delegateTask.getEventName() = " + delegateTask.getEventName());
        System.out.println("delegateTask.getName() = " + delegateTask.getName());
    }
}

测试流程

@Test
public void deployProcess() {
    Deployment deploy = repositoryService.createDeployment()
            .addClasspathResource("processes/timer.bpmn20.xml")
            .deploy();
    System.out.println("deploy = " + deploy);
}

@Test
public void completeTask() {
    // 查询任务
    Task task = taskService.createTaskQuery().processInstanceId("620d7748-3e41-11ee-9ba8-18c04dcd4aee").singleResult();
    System.out.println("task = " + task);
    // 完成任务
    taskService.complete(task.getId());
}

流程执行步骤

// 1.部署流程触发定时开始事件(固定日期时间):2023-08-19 11:35:00.000、timer-start-event
// 2.服务任务触发:当前时间:2023-08-19 11:35:03
// 3.触发定时中间事件20秒:2023-08-19 11:35:23.392、trigger-timer
// 4.20后美团外卖骑手任务触发:当前时间:2023-08-19 11:35:23
// 5.触发定时边界事件30秒(非中断):2023-08-19 11:35:53.406,trigger-timer
// 6.30秒后通知服务触发:当前时间:2023-08-19 11:35:53
// 7.完成美团外卖骑手任务,当前任务变更为饿了么外卖骑手:当前时间:2023-08-19 11:36:14
// 8.触发定时边界事件1分钟(中断):2023-08-19 11:37:14.586、trigger-timer
// 9.1分钟后任务变更为美团优选:当前时间:2023-08-19 11:37:23
// 10.完成美团优选任务结束流程

你可能感兴趣的:(Activiti,spring,boot,java,流程图)