activity事件(1)定时器事件

2025-11-02 09:52:44

1、本文用的项目是springboot2+activity6,下面的链接是我写的整合文章,有需要的可以点击观看。

2、定义定时器的标签是<timerEventDefinition>...</timerEventDefinition>,那怎么用这个标签呢?首先timerEventDefinition里面有个属性为businessCalendarName,这个属性是指向流程引擎配置中的业务日历的位置。如果想使用默认的业务日历,官方文档中是这样定义的:<timerEventDefinition activiti:businessCalendarName="custom">...</timerEventDefinition>

3、定时器必须具有以下一个元素:

timeDate,timeDuration,timeCycle。

timeDate表示何时触发触发器,例如:

<timerEventDefinition>

    <timeDate>2019-07-13T17:45:38</timeDate>

</timerEventDefinition>

需要注意这里的时间格式是ISO 8601固定格式

4、timeDuration表示定时器经过多少时间后触发触发器,例:

<timerEventDefinition>

    <timeDuration>P3D</timeDuration>

</timerEventDefinition>

时间格式依然为ISO 8601格式,一年两个月三天四小时五分六秒内,可以写成P1Y2M3DT4H5M6S,P是开始标记,T是时间和日期分割标记没有日期只有时间T是不能省去的,比如1小时执行一次应该写成PT1H。

5、timeCycle表示重复触发触发器的间隔时间,该标签里面包含了endDate指的是某个时间让重复触发的失效(该属性为可选非必须的)。例如:

<timerEventDefinition>

    <timeCycle activiti:endDate="2019-07-13T16:42:11+00:00">R3/PT4H</timeCycle>

</timerEventDefinition>

或者

<timerEventDefinition>

    <timeCycle>R3/PT10H/${EndDate}</timeCycle>

</timerEventDefinition>

${EndDate}为变量。

时间格式依然固定的ISO 8601格式,但是这个时间还支撑另外一种cron表达式的时间。R 表示永远重复;R1 - 重复一次;R231 - 重复231次。R3/PT10H表示重复3次每次间隔4小时

 

1、首先创建一个bpmn流程文件,内容如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.activiti.org/testm1552011432748" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1552011432748" name="" targetNamespace="http://www.activiti.org/testm1552011432748" typeLanguage="http://www.w3.org/2001/XMLSchema">

  <process id="cy_timerProcess_1" isClosed="false" isExecutable="true" name="testProcess02" processType="None">

    <startEvent id="_2" name="start"/>

    <endEvent id="_7" name="end"/>

    <intermediateCatchEvent id="_5" name="timer01">

      <timerEventDefinition id="_5_ED_1">

        <timeDuration><![CDATA[PT10S]]></timeDuration>

      </timerEventDefinition>

    </intermediateCatchEvent>

    <sequenceFlow id="_9" sourceRef="_2" targetRef="_5"/>

    <userTask activiti:exclusive="true" id="_6" name="user task"/>

    <sequenceFlow id="_8" sourceRef="_6" targetRef="_7"/>

    <sequenceFlow id="_4" sourceRef="_5" targetRef="_6"/>

  </process>

  <bpmndi:BPMNDiagram documentation="background=#3C3F41;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram">

    <bpmndi:BPMNPlane bpmnElement="cy_timerProcess_1">

      <bpmndi:BPMNShape bpmnElement="_2" id="Shape-_2">

        <dc:Bounds height="32.0

        <bpmndi:BPMNLabel>

          <dc:Bounds height="32.0

        </bpmndi:BPMNLabel>

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape bpmnElement="_7" id="Shape-_7">

        <dc:Bounds height="32.0

        <bpmndi:BPMNLabel>

          <dc:Bounds height="32.0

        </bpmndi:BPMNLabel>

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape bpmnElement="_5" id="Shape-_5">

        <dc:Bounds height="32.0

        <bpmndi:BPMNLabel>

          <dc:Bounds height="32.0

        </bpmndi:BPMNLabel>

      </bpmndi:BPMNShape>

      <bpmndi:BPMNShape bpmnElement="_6" id="Shape-_6">

        <dc:Bounds height="55.0

        <bpmndi:BPMNLabel>

          <dc:Bounds height="55.0

        </bpmndi:BPMNLabel>

      </bpmndi:BPMNShape>

      <bpmndi:BPMNEdge bpmnElement="_4" id="BPMNEdge__4" sourceElement="_5" targetElement="_6">

        <di:waypoint x="201.0" y="196.0"/>

        <di:waypoint x="201.0" y="230.0"/>

        <bpmndi:BPMNLabel>

          <dc:Bounds height="-1.0

        </bpmndi:BPMNLabel>

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNEdge bpmnElement="_8" id="BPMNEdge__8" sourceElement="_6" targetElement="_7">

        <di:waypoint x="206.0" y="285.0"/>

        <di:waypoint x="206.0" y="340.0"/>

        <bpmndi:BPMNLabel>

          <dc:Bounds height="0.0

        </bpmndi:BPMNLabel>

      </bpmndi:BPMNEdge>

      <bpmndi:BPMNEdge bpmnElement="_9" id="BPMNEdge__9" sourceElement="_2" targetElement="_5">

        <di:waypoint x="201.0" y="112.0"/>

        <di:waypoint x="201.0" y="164.0"/>

        <bpmndi:BPMNLabel>

          <dc:Bounds height="0.0

        </bpmndi:BPMNLabel>

      </bpmndi:BPMNEdge>

    </bpmndi:BPMNPlane>

  </bpmndi:BPMNDiagram>

</definitions>

activity事件(1)定时器事件

2、测试代码为:

package cn.cy.activity;

 

import org.activiti.engine.*;

 

import org.activiti.engine.impl.ProcessEngineImpl;

 

import org.activiti.engine.runtime.ProcessInstance;

 

import org.activiti.engine.task.Task;

 

import org.junit.Test;

 

import org.junit.runner.RunWith;

 

import org.springframework.beans.factory.annotation.Autowired;

 

import org.springframework.boot.test.context.SpringBootTest;

 

import org.springframework.jdbc.core.JdbcTemplate;

 

import org.springframework.jdbc.datasource.DataSourceTransactionManager;

 

import org.springframework.test.context.junit4.SpringRunner;

 

import javax.annotation.Resource;

 

import javax.sql.DataSource;

 

import java.util.List;

 

/**

 

* Created by 30721 on 2019/7/7.

 

*/

 

@RunWith(SpringRunner.class)

 

@SpringBootTest

 

public class ActivityTest {

 

   @Autowired

 

   private ProcessEngine engine;

 

   @Autowired

 

   private RuntimeService runtimeService;

 

   @Autowired

 

   private RepositoryService repositoryService;

 

   @Autowired

 

   private TaskService taskService;

 

   /**

 

    * 测试定时器事件

 

    */

 

   @Test

 

   public void timerTest() {

 

       // 启动AsyncExecutor

 

       engine.getProcessEngineConfiguration().getAsyncExecutor().start();//注意这句话开启定时器,一定要加

 

       // 部署流程文件

 

       repositoryService.createDeployment()

 

               .addClasspathResource("processes/timer.bpmn").deploy();

 

       // 启动流程

 

       runtimeService.startProcessInstanceByKey("cy_timerProcess_1");

 

       // 查询当前任务

 

       Task task = taskService.createTaskQuery().singleResult();

 

       System.out.println("定时器为触发时:" + task);

 

//        taskService.complete(task.getId());

 

       try {

 

           Thread.sleep(1000 * 20);

 

       } catch (InterruptedException e) {

 

           e.printStackTrace();

 

       }

 

       // 重新查询当前任务

 

       task = taskService.createTaskQuery().singleResult();

 

       System.out.println("定时器中间事件的触发后任务:" + task.getName());

 

       //执行

 

       taskService.complete(task.getId());

 

       //关闭AsyncExecutor

 

       engine.getProcessEngineConfiguration().getAsyncExecutor().shutdown();

 

   }

 

}

下图的结果与预测的是一样的,定时未触发时没有当前任务,定时触发后有当前任务。

activity事件(1)定时器事件

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢