[15]深入浅出工作开源框架Camunda:定时任务

[15]深入浅出工作开源框架Camunda:定时任务,第1张

1.引言

BPMN中,可以通过改变“Timer Start Event” 启动节点的启动类型来自动启动流程实例。
其提供了下面三种定时自动触发流程实例的模式:

指定固定的时间点启动一个新的流程实例指定相对延迟时间启动一个新的流程实例周期性的启动一个新的流程实例

下面分别就上面的三种方式进行设计和实验~
三种使用方式,如下:
(1)Date:特定时间(例:2022-05-25T16:00:00)
其支持的是ISO 8601标准,需要注意是,如果最后带了“Z”,说明用的是带时区的时间。
比如下面的例子,
2019-10-01T12:00:00Z - UTC time
2019-10-02T08:09:40+02:00 - UTC plus two hours zone offset 相对UTC时区加了2个时区
2019-10-02T08:09:40+02:00[Europe/Berlin] - UTC plus two hours zone offset at Berlin

如果不加Z表示就是当地的时间。 比如这个2022-05-25T16:00:00设置,其意思就是流程部署后,会在应用部署的时区内2022-05-25 16:00的时候启动一次而且仅仅一次~

(2)Duration: 延迟时间(例:PT10M)
Duration的格式符合ISO_8601标准,其格式如下:

P(n)Y(n)M(n)DT(n)H(n)M(n)S 其中n代表的具体的数值PnW

其具体说明如下:

P is the duration designator (for period) placed at the start of the duration representation. 主要指周期Y is the year designator that follows the value for the number of years. 代表年M is the month designator that follows the value for the number of months. 代表月W is the week designator that follows the value for the number of weeks. 代表周的缩写D is the day designator that follows the value for the number of days. 代表天的缩写T is the time designator that precedes the time components of the representation. 代表前置时间H is the hour designator that follows the value for the number of hours. 代表小时M is the minute designator that follows the value for the number of minutes. 代表分钟S is the second designator that follows the value for the number of seconds. 代表秒

其中P, Y, M, W, D, T, H, M, and S 能被忽略但是不能被替换

比如:PT10M标识其在部署后,10分钟后启动流程~

再来看一些例子:
PT15S - 15 seconds 15秒后创建一个流程实例~
PT1H30M - 1 hour and 30 minutes 1个小时30分钟后创建一个流程实例~
P14D - 14 days 14天后创建流程~
P14DT1H30M - 14 days, 1 hour and 30 minutes 14天1小时30分钟后创建一个流程实例~
P3Y6M4DT12H30M5S - 3 years, 6 months, 4 days, 12 hours, 30 minutes and 5 seconds 3年6个月4天12个小时30分5秒后创建一个流程实例~

(3)Cycle:循环时间(例:RS3/2022-05-25T16:00:00/PT30S)
Cycle支持的标准是ISO_8601的重复间隔标准,
下面是具体的例子:
R5/PT10S: Every 10 seconds, up to five times 每10秒触发一次,最多5次
R/P1D: Every day, infinitely ; 每天触发一次,无限循环
当然其也支持Cron的正则表达式,
比如下面就代表其当前的流程实例每隔5分钟就会启动一次:

0 0/5 * * * ?

下面举一个例子,比如下面的2022-05-25T16:00:00,每间隔30s就会触发一次,总共触发3次;

格式解析
RS3/2022-05-25T16:00:00/PT30S
重复次数/开始时间/运行间隔
重复次数:R - 将永远重复;R3- 将重复3次。
开始时间 :2021年11月19日16点00分00秒开始
运行间隔 :30秒

例如: P1Y2M3DT2H10M30S 表示间隔1年2个月3天2小时10分钟30秒
PT2M 表示间隔2分钟(Per Time 2 minutes)
注意:T是时间和日期分的割标记,如果没有年月日"T"也不能省略

计时器启动事件可用特定时间或者循环时间中间计时器事件可用特定时间或延迟时间。周期时间用于计时器启动事件或附加的非中断计时器边界事件 2.Camunda的实现原理

通过观察Camunda的数据库的变化,我们可以推测出其实现的原理。下面一个一个的来看:
(1)Date:特定时间(例:2022-05-25T10:40:00)


<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_1mnxduk" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.11.1" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0">
  <bpmn:process id="Process_14pjj1v" name="请假流程" isExecutable="true">
    <bpmn:startEvent id="StartEvent_1" name="特定时间启动">
      <bpmn:outgoing>Flow_1ihjej0bpmn:outgoing>
      <bpmn:timerEventDefinition id="TimerEventDefinition_0bshgz6">
        <bpmn:timeDate xsi:type="bpmn:tFormalExpression">2022-05-24T10:53:00bpmn:timeDate>
      bpmn:timerEventDefinition>
    bpmn:startEvent>
    <bpmn:sequenceFlow id="Flow_1ihjej0" sourceRef="StartEvent_1" targetRef="Activity_1tiqpqb" />
    <bpmn:sequenceFlow id="Flow_0748tlu" sourceRef="Activity_1tiqpqb" targetRef="Activity_0jzwi5i" />
    <bpmn:endEvent id="Event_1yb23ri" name="结束">
      <bpmn:incoming>Flow_0a6rwtsbpmn:incoming>
    bpmn:endEvent>
    <bpmn:sequenceFlow id="Flow_0a6rwts" sourceRef="Activity_0jzwi5i" targetRef="Event_1yb23ri" />
    <bpmn:userTask id="Activity_1tiqpqb" name="请假" camunda:assignee="demo">
      <bpmn:incoming>Flow_1ihjej0bpmn:incoming>
      <bpmn:outgoing>Flow_0748tlubpmn:outgoing>
    bpmn:userTask>
    <bpmn:userTask id="Activity_0jzwi5i" name="审批" camunda:assignee="demo">
      <bpmn:incoming>Flow_0748tlubpmn:incoming>
      <bpmn:outgoing>Flow_0a6rwtsbpmn:outgoing>
    bpmn:userTask>
  bpmn:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_14pjj1v">
      <bpmndi:BPMNEdge id="Flow_1ihjej0_di" bpmnElement="Flow_1ihjej0">
        <di:waypoint x="215" y="117" />
        <di:waypoint x="270" y="117" />
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_0748tlu_di" bpmnElement="Flow_0748tlu">
        <di:waypoint x="370" y="117" />
        <di:waypoint x="430" y="117" />
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_0a6rwts_di" bpmnElement="Flow_0a6rwts">
        <di:waypoint x="530" y="117" />
        <di:waypoint x="592" y="117" />
      bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="Event_1xfvi9y_di" bpmnElement="StartEvent_1">
        <dc:Bounds x="179" y="99" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="165" y="142" width="65" height="14" />
        bpmndi:BPMNLabel>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Event_1yb23ri_di" bpmnElement="Event_1yb23ri">
        <dc:Bounds x="592" y="99" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="599" y="142" width="22" height="14" />
        bpmndi:BPMNLabel>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_0sq8nj2_di" bpmnElement="Activity_1tiqpqb">
        <dc:Bounds x="270" y="77" width="100" height="80" />
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_0pt371x_di" bpmnElement="Activity_0jzwi5i">
        <dc:Bounds x="430" y="77" width="100" height="80" />
      bpmndi:BPMNShape>
    bpmndi:BPMNPlane>
  bpmndi:BPMNDiagram>
bpmn:definitions>


部署之后,查询 select * from ACT_RU_JOB,其里面添加了一条新的数据;

 select * from ACT_RU_JOB
{
"select * from ACT_RU_JOB": [
	{
		"ID_" : "225e7949-db0c-11ec-8676-005056c00008",
		"REV_" : 1,
		"TYPE_" : "timer",
		"LOCK_EXP_TIME_" : null,
		"LOCK_OWNER_" : null,
		"EXCLUSIVE_" : 1,
		"EXECUTION_ID_" : null,
		"PROCESS_INSTANCE_ID_" : null,
		"PROCESS_DEF_ID_" : "Process_14pjj1v:1:225d19b7-db0c-11ec-8676-005056c00008",
		"PROCESS_DEF_KEY_" : "Process_14pjj1v",
		"RETRIES_" : 3,
		"EXCEPTION_STACK_ID_" : null,
		"EXCEPTION_MSG_" : null,
		"FAILED_ACT_ID_" : null,
		"DUEDATE_" : "2022-05-24 10:53:00",
		"REPEAT_" : null,
		"REPEAT_OFFSET_" : 0,
		"HANDLER_TYPE_" : "timer-start-event",
		"HANDLER_CFG_" : "Process_14pjj1v",
		"DEPLOYMENT_ID_" : "22577465-db0c-11ec-8676-005056c00008",
		"SUSPENSION_STATE_" : 1,
		"JOB_DEF_ID_" : "225d19b8-db0c-11ec-8676-005056c00008",
		"PRIORITY_" : 0,
		"SEQUENCE_COUNTER_" : 1,
		"TENANT_ID_" : null,
		"CREATE_TIME_" : "2022-05-24 10:49:30"
	}
]}


Job的定义如下:

{
"select * from ACT_RU_JOBDEF": [
	{
		"ID_" : "225d19b8-db0c-11ec-8676-005056c00008",
		"REV_" : 1,
		"PROC_DEF_ID_" : "Process_14pjj1v:1:225d19b7-db0c-11ec-8676-005056c00008",
		"PROC_DEF_KEY_" : "Process_14pjj1v",
		"ACT_ID_" : "StartEvent_1",
		"JOB_TYPE_" : "timer-start-event",
		"JOB_CONFIGURATION_" : "DATE: 2022-05-24T10:53:00",
		"SUSPENSION_STATE_" : 1,
		"JOB_PRIORITY_" : null,
		"TENANT_ID_" : null,
		"DEPLOYMENT_ID_" : null
	}
]}


同时执行查询 select * from ACT_RU_TASK, 其表是空的

这个时候,我们看到用户任务里面没有待审批的工作流,表ACT_RU_TASK和ACT_RU_EXECUTION
也查询不到数据。

等到2022-05-24T10:53的时候,我们可以看到任务出现在Demo用户下:

同时,表ACT_RU_JOB里面的响应记录被删除;表ACT_RU_TASK和ACT_RU_EXECUTION有执行记录。

{
"select * from ACT_RU_TASK": [
	{
		"ID_" : "ad612c9e-db0c-11ec-8676-005056c00008",
		"REV_" : 1,
		"EXECUTION_ID_" : "ad60de7b-db0c-11ec-8676-005056c00008",
		"PROC_INST_ID_" : "ad60de7b-db0c-11ec-8676-005056c00008",
		"PROC_DEF_ID_" : "Process_14pjj1v:1:225d19b7-db0c-11ec-8676-005056c00008",
		"CASE_EXECUTION_ID_" : null,
		"CASE_INST_ID_" : null,
		"CASE_DEF_ID_" : null,
		"NAME_" : "请假",
		"PARENT_TASK_ID_" : null,
		"DESCRIPTION_" : null,
		"TASK_DEF_KEY_" : "Activity_1tiqpqb",
		"OWNER_" : null,
		"ASSIGNEE_" : "demo",
		"DELEGATION_" : null,
		"PRIORITY_" : 50,
		"CREATE_TIME_" : "2022-05-24 10:53:24",
		"DUE_DATE_" : null,
		"FOLLOW_UP_DATE_" : null,
		"SUSPENSION_STATE_" : 1,
		"TENANT_ID_" : null
	}
]}

{
"select * from ACT_RU_EXECUTION": [
	{
		"ID_" : "ad60de7b-db0c-11ec-8676-005056c00008",
		"REV_" : 1,
		"ROOT_PROC_INST_ID_" : "ad60de7b-db0c-11ec-8676-005056c00008",
		"PROC_INST_ID_" : "ad60de7b-db0c-11ec-8676-005056c00008",
		"BUSINESS_KEY_" : null,
		"PARENT_ID_" : null,
		"PROC_DEF_ID_" : "Process_14pjj1v:1:225d19b7-db0c-11ec-8676-005056c00008",
		"SUPER_EXEC_" : null,
		"SUPER_CASE_EXEC_" : null,
		"CASE_INST_ID_" : null,
		"ACT_ID_" : "Activity_1tiqpqb",
		"ACT_INST_ID_" : "Activity_1tiqpqb:ad61058d-db0c-11ec-8676-005056c00008",
		"IS_ACTIVE_" : 1,
		"IS_CONCURRENT_" : 0,
		"IS_SCOPE_" : 1,
		"IS_EVENT_SCOPE_" : 0,
		"SUSPENSION_STATE_" : 1,
		"CACHED_ENT_STATE_" : 2,
		"SEQUENCE_COUNTER_" : 3,
		"TENANT_ID_" : null
	}
]}

(2)Duration: 延迟时间(例:PT3M,PT60S)
下面让当前的任务在部署后延迟3分钟后启动,流程如下:


<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_07guhgc" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.11.1" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0">
  <bpmn:process id="Process_1l8shll" name="定时启动流程" isExecutable="true">
    <bpmn:startEvent id="StartEvent_1" name="定时启动">
      <bpmn:outgoing>Flow_1vlwakcbpmn:outgoing>
      <bpmn:timerEventDefinition id="TimerEventDefinition_0qlgfxu">
        <bpmn:timeDuration xsi:type="bpmn:tFormalExpression">PT3Mbpmn:timeDuration>
      bpmn:timerEventDefinition>
    bpmn:startEvent>
    <bpmn:sequenceFlow id="Flow_1vlwakc" sourceRef="StartEvent_1" targetRef="Activity_1v7sbrd" />
    <bpmn:endEvent id="Event_0pi412e" name="结束">
      <bpmn:incoming>Flow_07cx1sgbpmn:incoming>
    bpmn:endEvent>
    <bpmn:sequenceFlow id="Flow_07cx1sg" sourceRef="Activity_1v7sbrd" targetRef="Event_0pi412e" />
    <bpmn:userTask id="Activity_1v7sbrd" name="定时启动触发的任务" camunda:assignee="demo">
      <bpmn:incoming>Flow_1vlwakcbpmn:incoming>
      <bpmn:outgoing>Flow_07cx1sgbpmn:outgoing>
    bpmn:userTask>
  bpmn:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1l8shll">
      <bpmndi:BPMNEdge id="Flow_1vlwakc_di" bpmnElement="Flow_1vlwakc">
        <di:waypoint x="215" y="117" />
        <di:waypoint x="270" y="117" />
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_07cx1sg_di" bpmnElement="Flow_07cx1sg">
        <di:waypoint x="370" y="117" />
        <di:waypoint x="432" y="117" />
      bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="Event_0iv489d_di" bpmnElement="StartEvent_1">
        <dc:Bounds x="179" y="99" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="176" y="142" width="43" height="14" />
        bpmndi:BPMNLabel>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Event_0pi412e_di" bpmnElement="Event_0pi412e">
        <dc:Bounds x="432" y="99" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="439" y="142" width="22" height="14" />
        bpmndi:BPMNLabel>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_1gvk3cr_di" bpmnElement="Activity_1v7sbrd">
        <dc:Bounds x="270" y="77" width="100" height="80" />
      bpmndi:BPMNShape>
    bpmndi:BPMNPlane>
  bpmndi:BPMNDiagram>
bpmn:definitions>

部署之后,咱们查看数据库:表ACT_RU_JOB和ACT_RU_JOBDEF有数据;
ACT_RU_TASK和ACT_RU_EXECUTION没有数据。
当前用户[demo]的用户任务里面没有任务

{
"select * from ACT_RU_JOB": [
	{
		"ID_" : "5da0d864-db0e-11ec-8676-005056c00008",
		"REV_" : 1,
		"TYPE_" : "timer",
		"LOCK_EXP_TIME_" : null,
		"LOCK_OWNER_" : null,
		"EXCLUSIVE_" : 1,
		"EXECUTION_ID_" : null,
		"PROCESS_INSTANCE_ID_" : null,
		"PROCESS_DEF_ID_" : "Process_1l8shll:1:5d9fc6f2-db0e-11ec-8676-005056c00008",
		"PROCESS_DEF_KEY_" : "Process_1l8shll",
		"RETRIES_" : 3,
		"EXCEPTION_STACK_ID_" : null,
		"EXCEPTION_MSG_" : null,
		"FAILED_ACT_ID_" : null,
		"DUEDATE_" : "2022-05-24 11:08:29",
		"REPEAT_" : null,
		"REPEAT_OFFSET_" : 0,
		"HANDLER_TYPE_" : "timer-start-event",
		"HANDLER_CFG_" : "Process_1l8shll",
		"DEPLOYMENT_ID_" : "5d984ce0-db0e-11ec-8676-005056c00008",
		"SUSPENSION_STATE_" : 1,
		"JOB_DEF_ID_" : "5d9fc6f3-db0e-11ec-8676-005056c00008",
		"PRIORITY_" : 0,
		"SEQUENCE_COUNTER_" : 1,
		"TENANT_ID_" : null,
		"CREATE_TIME_" : "2022-05-24 11:05:29"
	}
]}
{
"select * from ACT_RU_JOBDEF": [
	{
		"ID_" : "5d9fc6f3-db0e-11ec-8676-005056c00008",
		"REV_" : 1,
		"PROC_DEF_ID_" : "Process_1l8shll:1:5d9fc6f2-db0e-11ec-8676-005056c00008",
		"PROC_DEF_KEY_" : "Process_1l8shll",
		"ACT_ID_" : "StartEvent_1",
		"JOB_TYPE_" : "timer-start-event",
		"JOB_CONFIGURATION_" : "DURATION: PT3M",
		"SUSPENSION_STATE_" : 1,
		"JOB_PRIORITY_" : null,
		"TENANT_ID_" : null,
		"DEPLOYMENT_ID_" : null
	}
]}

从上面的ACT_RU_JOB表里面,我们可以看到有一个DUEDATE_的字段,其估计在2022-05-24 11:08:29的时候启动流程;但是我们发现流程的启动不是那么精确,下面将会具体确认。

经过3分钟后,我们查询数据库。表ACT_RU_JOB已经没有数据表ACT_RU_JOB和ACT_RU_EXECUTION里面的数据如下:
{
"select * from ACT_RU_TASK": [
	{
		"ID_" : "e1835b89-db0e-11ec-8676-005056c00008",
		"REV_" : 1,
		"EXECUTION_ID_" : "e182e656-db0e-11ec-8676-005056c00008",
		"PROC_INST_ID_" : "e182e656-db0e-11ec-8676-005056c00008",
		"PROC_DEF_ID_" : "Process_1l8shll:1:5d9fc6f2-db0e-11ec-8676-005056c00008",
		"CASE_EXECUTION_ID_" : null,
		"CASE_INST_ID_" : null,
		"CASE_DEF_ID_" : null,
		"NAME_" : "定时启动触发的任务",
		"PARENT_TASK_ID_" : null,
		"DESCRIPTION_" : null,
		"TASK_DEF_KEY_" : "Activity_1v7sbrd",
		"OWNER_" : null,
		"ASSIGNEE_" : "demo",
		"DELEGATION_" : null,
		"PRIORITY_" : 50,
		"CREATE_TIME_" : "2022-05-24 11:09:10",
		"DUE_DATE_" : null,
		"FOLLOW_UP_DATE_" : null,
		"SUSPENSION_STATE_" : 1,
		"TENANT_ID_" : null
	}
]}
{
"select * from ACT_RU_EXECUTION": [
	{
		"ID_" : "e182e656-db0e-11ec-8676-005056c00008",
		"REV_" : 1,
		"ROOT_PROC_INST_ID_" : "e182e656-db0e-11ec-8676-005056c00008",
		"PROC_INST_ID_" : "e182e656-db0e-11ec-8676-005056c00008",
		"BUSINESS_KEY_" : null,
		"PARENT_ID_" : null,
		"PROC_DEF_ID_" : "Process_1l8shll:1:5d9fc6f2-db0e-11ec-8676-005056c00008",
		"SUPER_EXEC_" : null,
		"SUPER_CASE_EXEC_" : null,
		"CASE_INST_ID_" : null,
		"ACT_ID_" : "Activity_1v7sbrd",
		"ACT_INST_ID_" : "Activity_1v7sbrd:e1833478-db0e-11ec-8676-005056c00008",
		"IS_ACTIVE_" : 1,
		"IS_CONCURRENT_" : 0,
		"IS_SCOPE_" : 1,
		"IS_EVENT_SCOPE_" : 0,
		"SUSPENSION_STATE_" : 1,
		"CACHED_ENT_STATE_" : 2,
		"SEQUENCE_COUNTER_" : 3,
		"TENANT_ID_" : null
	}
]}

从上面的表ACT_RU_TASK,里面的用户任务创建的时间来看,其是在2022-05-24 11:09:10 被创建的;说明和预估的时间有几十秒的差异;大家在使用定时任务的时候可能要特别注意。

(3)Cycle:循环时间(例:R3/2022-05-24T11:40:00/PT30S)
当前例子触发的条件,R3/2022-05-24T13:45:00/PT30S

其流程的文件如下:


<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_0q7aoqg" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.11.1" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.15.0">
  <bpmn:process id="Process_1xbub4w" name="循环执行流程" isExecutable="true">
    <bpmn:startEvent id="StartEvent_1" name="启动循环执行">
      <bpmn:outgoing>Flow_0iu9vjxbpmn:outgoing>
      <bpmn:timerEventDefinition id="TimerEventDefinition_0twu21y">
        <bpmn:timeCycle xsi:type="bpmn:tFormalExpression">R3/2022-05-24T13:45:00/PT30Sbpmn:timeCycle>
      bpmn:timerEventDefinition>
    bpmn:startEvent>
    <bpmn:sequenceFlow id="Flow_0iu9vjx" sourceRef="StartEvent_1" targetRef="Activity_1cozyk4" />
    <bpmn:userTask id="Activity_1cozyk4" name="循环执行用户任务" camunda:assignee="demo">
      <bpmn:incoming>Flow_0iu9vjxbpmn:incoming>
      <bpmn:outgoing>Flow_0e5x62xbpmn:outgoing>
    bpmn:userTask>
    <bpmn:endEvent id="Event_1yjhcvb" name="结束">
      <bpmn:incoming>Flow_0e5x62xbpmn:incoming>
    bpmn:endEvent>
    <bpmn:sequenceFlow id="Flow_0e5x62x" sourceRef="Activity_1cozyk4" targetRef="Event_1yjhcvb" />
  bpmn:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1xbub4w">
      <bpmndi:BPMNEdge id="Flow_0iu9vjx_di" bpmnElement="Flow_0iu9vjx">
        <di:waypoint x="215" y="117" />
        <di:waypoint x="310" y="117" />
      bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="Flow_0e5x62x_di" bpmnElement="Flow_0e5x62x">
        <di:waypoint x="410" y="117" />
        <di:waypoint x="502" y="117" />
      bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="Event_1w01fbs_di" bpmnElement="StartEvent_1">
        <dc:Bounds x="179" y="99" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="165" y="142" width="66" height="14" />
        bpmndi:BPMNLabel>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Event_1yjhcvb_di" bpmnElement="Event_1yjhcvb">
        <dc:Bounds x="502" y="99" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="509" y="142" width="22" height="14" />
        bpmndi:BPMNLabel>
      bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Activity_1kfy0rr_di" bpmnElement="Activity_1cozyk4">
        <dc:Bounds x="310" y="77" width="100" height="80" />
      bpmndi:BPMNShape>
    bpmndi:BPMNPlane>
  bpmndi:BPMNDiagram>
bpmn:definitions>

部署之后,其将会自动触发~
查询ACT_RU_JOB和ACT_RU_JOBDEF,其结果如下:

{
"select * from ACT_RU_JOB": [
	{
		"ID_" : "46f5ebf6-db24-11ec-8676-005056c00008",
		"REV_" : 1,
		"TYPE_" : "timer",
		"LOCK_EXP_TIME_" : null,
		"LOCK_OWNER_" : null,
		"EXCLUSIVE_" : 1,
		"EXECUTION_ID_" : null,
		"PROCESS_INSTANCE_ID_" : null,
		"PROCESS_DEF_ID_" : "Process_1xbub4w:1:46f4da84-db24-11ec-8676-005056c00008",
		"PROCESS_DEF_KEY_" : "Process_1xbub4w",
		"RETRIES_" : 3,
		"EXCEPTION_STACK_ID_" : null,
		"EXCEPTION_MSG_" : null,
		"FAILED_ACT_ID_" : null,
		"DUEDATE_" : "2022-05-24 13:45:00",
		"REPEAT_" : "R3\/2022-05-24T13:45:00\/PT30S",
		"REPEAT_OFFSET_" : 0,
		"HANDLER_TYPE_" : "timer-start-event",
		"HANDLER_CFG_" : "Process_1xbub4w",
		"DEPLOYMENT_ID_" : "46ef3532-db24-11ec-8676-005056c00008",
		"SUSPENSION_STATE_" : 1,
		"JOB_DEF_ID_" : "46f4da85-db24-11ec-8676-005056c00008",
		"PRIORITY_" : 0,
		"SEQUENCE_COUNTER_" : 1,
		"TENANT_ID_" : null,
		"CREATE_TIME_" : "2022-05-24 13:42:20"
	}
]}
{
"select * from ACT_RU_JOBDEF": [
	{
		"ID_" : "46f4da85-db24-11ec-8676-005056c00008",
		"REV_" : 1,
		"PROC_DEF_ID_" : "Process_1xbub4w:1:46f4da84-db24-11ec-8676-005056c00008",
		"PROC_DEF_KEY_" : "Process_1xbub4w",
		"ACT_ID_" : "StartEvent_1",
		"JOB_TYPE_" : "timer-start-event",
		"JOB_CONFIGURATION_" : "CYCLE: R3\/2022-05-24T13:45:00\/PT30S",
		"SUSPENSION_STATE_" : 1,
		"JOB_PRIORITY_" : null,
		"TENANT_ID_" : null,
		"DEPLOYMENT_ID_" : null
	}
]}

表ACT_RU_TASK和ACT_RU_EXECUTION还是空的。用户Demo里面的任务也是空的。

等时间流转到2022-05-24T13:45:00。我们再来看表里面的数据。

表ACT_RU_JOB和ACT_RU_JOBDEF定义的数据如下:

{
"select * from ACT_RU_JOB": [
	{
		"ID_" : "d5d3ddc6-db24-11ec-8676-005056c00008",
		"REV_" : 1,
		"TYPE_" : "timer",
		"LOCK_EXP_TIME_" : null,
		"LOCK_OWNER_" : null,
		"EXCLUSIVE_" : 1,
		"EXECUTION_ID_" : null,
		"PROCESS_INSTANCE_ID_" : null,
		"PROCESS_DEF_ID_" : "Process_1xbub4w:1:46f4da84-db24-11ec-8676-005056c00008",
		"PROCESS_DEF_KEY_" : "Process_1xbub4w",
		"RETRIES_" : 3,
		"EXCEPTION_STACK_ID_" : null,
		"EXCEPTION_MSG_" : null,
		"FAILED_ACT_ID_" : null,
		"DUEDATE_" : "2022-05-24 13:46:30",
		"REPEAT_" : "R3\/2022-05-24T13:45:00\/PT30S",
		"REPEAT_OFFSET_" : 0,
		"HANDLER_TYPE_" : "timer-start-event",
		"HANDLER_CFG_" : "Process_1xbub4w",
		"DEPLOYMENT_ID_" : "46ef3532-db24-11ec-8676-005056c00008",
		"SUSPENSION_STATE_" : 1,
		"JOB_DEF_ID_" : "46f4da85-db24-11ec-8676-005056c00008",
		"PRIORITY_" : 0,
		"SEQUENCE_COUNTER_" : 1,
		"TENANT_ID_" : null,
		"CREATE_TIME_" : "2022-05-24 13:46:19"
	}
]}
{
"select * from ACT_RU_JOBDEF": [
	{
		"ID_" : "46f4da85-db24-11ec-8676-005056c00008",
		"REV_" : 1,
		"PROC_DEF_ID_" : "Process_1xbub4w:1:46f4da84-db24-11ec-8676-005056c00008",
		"PROC_DEF_KEY_" : "Process_1xbub4w",
		"ACT_ID_" : "StartEvent_1",
		"JOB_TYPE_" : "timer-start-event",
		"JOB_CONFIGURATION_" : "CYCLE: R3\/2022-05-24T13:45:00\/PT30S",
		"SUSPENSION_STATE_" : 1,
		"JOB_PRIORITY_" : null,
		"TENANT_ID_" : null,
		"DEPLOYMENT_ID_" : null
	}
]}

任务栏中出现4次任务

查看表ACT_RU_TASK和表ACT_RU_EXECUTION

{
"select * from ACT_RU_TASK": [
	{
		"ID_" : "abb0d14d-db24-11ec-8676-005056c00008",
		"REV_" : 1,
		"EXECUTION_ID_" : "abb0aa3a-db24-11ec-8676-005056c00008",
		"PROC_INST_ID_" : "abb0aa3a-db24-11ec-8676-005056c00008",
		"PROC_DEF_ID_" : "Process_1xbub4w:1:46f4da84-db24-11ec-8676-005056c00008",
		"CASE_EXECUTION_ID_" : null,
		"CASE_INST_ID_" : null,
		"CASE_DEF_ID_" : null,
		"NAME_" : "循环执行用户任务",
		"PARENT_TASK_ID_" : null,
		"DESCRIPTION_" : null,
		"TASK_DEF_KEY_" : "Activity_1cozyk4",
		"OWNER_" : null,
		"ASSIGNEE_" : "demo",
		"DELEGATION_" : null,
		"PRIORITY_" : 50,
		"CREATE_TIME_" : "2022-05-24 13:45:09",
		"DUE_DATE_" : null,
		"FOLLOW_UP_DATE_" : null,
		"SUSPENSION_STATE_" : 1,
		"TENANT_ID_" : null
	},
	{
		"ID_" : "c0b33c56-db24-11ec-8676-005056c00008",
		"REV_" : 1,
		"EXECUTION_ID_" : "c0b2ee33-db24-11ec-8676-005056c00008",
		"PROC_INST_ID_" : "c0b2ee33-db24-11ec-8676-005056c00008",
		"PROC_DEF_ID_" : "Process_1xbub4w:1:46f4da84-db24-11ec-8676-005056c00008",
		"CASE_EXECUTION_ID_" : null,
		"CASE_INST_ID_" : null,
		"CASE_DEF_ID_" : null,
		"NAME_" : "循环执行用户任务",
		"PARENT_TASK_ID_" : null,
		"DESCRIPTION_" : null,
		"TASK_DEF_KEY_" : "Activity_1cozyk4",
		"OWNER_" : null,
		"ASSIGNEE_" : "demo",
		"DELEGATION_" : null,
		"PRIORITY_" : 50,
		"CREATE_TIME_" : "2022-05-24 13:45:44",
		"DUE_DATE_" : null,
		"FOLLOW_UP_DATE_" : null,
		"SUSPENSION_STATE_" : 1,
		"TENANT_ID_" : null
	},
	{
		"ID_" : "d5d53d5b-db24-11ec-8676-005056c00008",
		"REV_" : 1,
		"EXECUTION_ID_" : "d5d4ef38-db24-11ec-8676-005056c00008",
		"PROC_INST_ID_" : "d5d4ef38-db24-11ec-8676-005056c00008",
		"PROC_DEF_ID_" : "Process_1xbub4w:1:46f4da84-db24-11ec-8676-005056c00008",
		"CASE_EXECUTION_ID_" : null,
		"CASE_INST_ID_" : null,
		"CASE_DEF_ID_" : null,
		"NAME_" : "循环执行用户任务",
		"PARENT_TASK_ID_" : null,
		"DESCRIPTION_" : null,
		"TASK_DEF_KEY_" : "Activity_1cozyk4",
		"OWNER_" : null,
		"ASSIGNEE_" : "demo",
		"DELEGATION_" : null,
		"PRIORITY_" : 50,
		"CREATE_TIME_" : "2022-05-24 13:46:19",
		"DUE_DATE_" : null,
		"FOLLOW_UP_DATE_" : null,
		"SUSPENSION_STATE_" : 1,
		"TENANT_ID_" : null
	},
	{
		"ID_" : "def5aa62-db24-11ec-8676-005056c00008",
		"REV_" : 1,
		"EXECUTION_ID_" : "def5834f-db24-11ec-8676-005056c00008",
		"PROC_INST_ID_" : "def5834f-db24-11ec-8676-005056c00008",
		"PROC_DEF_ID_" : "Process_1xbub4w:1:46f4da84-db24-11ec-8676-005056c00008",
		"CASE_EXECUTION_ID_" : null,
		"CASE_INST_ID_" : null,
		"CASE_DEF_ID_" : null,
		"NAME_" : "循环执行用户任务",
		"PARENT_TASK_ID_" : null,
		"DESCRIPTION_" : null,
		"TASK_DEF_KEY_" : "Activity_1cozyk4",
		"OWNER_" : null,
		"ASSIGNEE_" : "demo",
		"DELEGATION_" : null,
		"PRIORITY_" : 50,
		"CREATE_TIME_" : "2022-05-24 13:46:35",
		"DUE_DATE_" : null,
		"FOLLOW_UP_DATE_" : null,
		"SUSPENSION_STATE_" : 1,
		"TENANT_ID_" : null
	}
]}

{
"select * from ACT_RU_EXECUTION": [
	{
		"ID_" : "abb0aa3a-db24-11ec-8676-005056c00008",
		"REV_" : 1,
		"ROOT_PROC_INST_ID_" : "abb0aa3a-db24-11ec-8676-005056c00008",
		"PROC_INST_ID_" : "abb0aa3a-db24-11ec-8676-005056c00008",
		"BUSINESS_KEY_" : null,
		"PARENT_ID_" : null,
		"PROC_DEF_ID_" : "Process_1xbub4w:1:46f4da84-db24-11ec-8676-005056c00008",
		"SUPER_EXEC_" : null,
		"SUPER_CASE_EXEC_" : null,
		"CASE_INST_ID_" : null,
		"ACT_ID_" : "Activity_1cozyk4",
		"ACT_INST_ID_" : "Activity_1cozyk4:abb0d14c-db24-11ec-8676-005056c00008",
		"IS_ACTIVE_" : 1,
		"IS_CONCURRENT_" : 0,
		"IS_SCOPE_" : 1,
		"IS_EVENT_SCOPE_" : 0,
		"SUSPENSION_STATE_" : 1,
		"CACHED_ENT_STATE_" : 2,
		"SEQUENCE_COUNTER_" : 3,
		"TENANT_ID_" : null
	},
	{
		"ID_" : "c0b2ee33-db24-11ec-8676-005056c00008",
		"REV_" : 1,
		"ROOT_PROC_INST_ID_" : "c0b2ee33-db24-11ec-8676-005056c00008",
		"PROC_INST_ID_" : "c0b2ee33-db24-11ec-8676-005056c00008",
		"BUSINESS_KEY_" : null,
		"PARENT_ID_" : null,
		"PROC_DEF_ID_" : "Process_1xbub4w:1:46f4da84-db24-11ec-8676-005056c00008",
		"SUPER_EXEC_" : null,
		"SUPER_CASE_EXEC_" : null,
		"CASE_INST_ID_" : null,
		"ACT_ID_" : "Activity_1cozyk4",
		"ACT_INST_ID_" : "Activity_1cozyk4:c0b31545-db24-11ec-8676-005056c00008",
		"IS_ACTIVE_" : 1,
		"IS_CONCURRENT_" : 0,
		"IS_SCOPE_" : 1,
		"IS_EVENT_SCOPE_" : 0,
		"SUSPENSION_STATE_" : 1,
		"CACHED_ENT_STATE_" : 2,
		"SEQUENCE_COUNTER_" : 3,
		"TENANT_ID_" : null
	},
	{
		"ID_" : "d5d4ef38-db24-11ec-8676-005056c00008",
		"REV_" : 1,
		"ROOT_PROC_INST_ID_" : "d5d4ef38-db24-11ec-8676-005056c00008",
		"PROC_INST_ID_" : "d5d4ef38-db24-11ec-8676-005056c00008",
		"BUSINESS_KEY_" : null,
		"PARENT_ID_" : null,
		"PROC_DEF_ID_" : "Process_1xbub4w:1:46f4da84-db24-11ec-8676-005056c00008",
		"SUPER_EXEC_" : null,
		"SUPER_CASE_EXEC_" : null,
		"CASE_INST_ID_" : null,
		"ACT_ID_" : "Activity_1cozyk4",
		"ACT_INST_ID_" : "Activity_1cozyk4:d5d53d5a-db24-11ec-8676-005056c00008",
		"IS_ACTIVE_" : 1,
		"IS_CONCURRENT_" : 0,
		"IS_SCOPE_" : 1,
		"IS_EVENT_SCOPE_" : 0,
		"SUSPENSION_STATE_" : 1,
		"CACHED_ENT_STATE_" : 2,
		"SEQUENCE_COUNTER_" : 3,
		"TENANT_ID_" : null
	},
	{
		"ID_" : "def5834f-db24-11ec-8676-005056c00008",
		"REV_" : 1,
		"ROOT_PROC_INST_ID_" : "def5834f-db24-11ec-8676-005056c00008",
		"PROC_INST_ID_" : "def5834f-db24-11ec-8676-005056c00008",
		"BUSINESS_KEY_" : null,
		"PARENT_ID_" : null,
		"PROC_DEF_ID_" : "Process_1xbub4w:1:46f4da84-db24-11ec-8676-005056c00008",
		"SUPER_EXEC_" : null,
		"SUPER_CASE_EXEC_" : null,
		"CASE_INST_ID_" : null,
		"ACT_ID_" : "Activity_1cozyk4",
		"ACT_INST_ID_" : "Activity_1cozyk4:def5aa61-db24-11ec-8676-005056c00008",
		"IS_ACTIVE_" : 1,
		"IS_CONCURRENT_" : 0,
		"IS_SCOPE_" : 1,
		"IS_EVENT_SCOPE_" : 0,
		"SUSPENSION_STATE_" : 1,
		"CACHED_ENT_STATE_" : 2,
		"SEQUENCE_COUNTER_" : 3,
		"TENANT_ID_" : null
	}
]}

很奇怪,明明我设置了3次循环,为什么会出现四次任务。我估计可能是Camunda的Bug。
而且我加了下面查询条件的SQL之后,还是查出4条数据。

select * from ACT_RU_TASK where PROC_DEF_ID_='Process_1xbub4w:1:46f4da84-db24-11ec-8676-005056c00008';
select * from ACT_RU_EXECUTION where PROC_DEF_ID_='Process_1xbub4w:1:46f4da84-db24-11ec-8676-005056c00008';


难道是偶然发生的? 笔者于是删除了当前的部署,再重新部署, 结果有的时候触发了3次,有的时候触发了4次,不是很准确~

另外需要注意的是,R3/2022-05-24T13:45:00/PT30S 里面的时间不能早于当时部署的时间,否则,用Camunda Modeler部署的时候,其会报出下面的错误。
duedate is null [ deploy-error ]

3.总结

经过上面的实验和研究后,我们发现了Camnda定时启动的3种方式以及触发后数据库里面表的数据的变化。

参考文献

https://docs.camunda.org/manual/7.16/reference/bpmn20/events/timer-events/
https://en.wikipedia.org/wiki/ISO_8601#Durations
https://docs.camunda.io/docs/components/modeler/bpmn/timer-events/

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1320218.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-11
下一篇 2022-06-11

发表评论

登录后才能评论

评论列表(0条)

保存