天天看点

【activiti 入门】activiti6.0事件之定时事件

环境:

IDEA+actbpm插件

第一步绘制流程图:

【activiti 入门】activiti6.0事件之定时事件

具体xml代码如下:

<process id="tcProcess" name="tcProcess">
  <startEvent id="startevent1" name="Start"></startEvent>
  <userTask id="usertask1" name="发货"></userTask>
  <userTask id="usertask2" name="接收订单"></userTask>
  <intermediateCatchEvent id="timerintermediatecatchevent1"
                          name="TimerCatchEvent">
    <timerEventDefinition>
      <timeDuration>PT1M</timeDuration>
    </timerEventDefinition>
  </intermediateCatchEvent>
  <endEvent id="endevent1" name="End"></endEvent>
  <sequenceFlow id="flow1" name="" sourceRef="startevent1"
                targetRef="usertask2"></sequenceFlow>
  <sequenceFlow id="flow2" name="" sourceRef="usertask2"
                targetRef="timerintermediatecatchevent1"></sequenceFlow>
  <sequenceFlow id="flow3" name=""
                sourceRef="timerintermediatecatchevent1" targetRef="usertask1"></sequenceFlow>
  <sequenceFlow id="flow4" name="" sourceRef="usertask1"
                targetRef="endevent1"></sequenceFlow>
</process>      

 讲解:

事件框架总的定义型的事件都是intermediateCatchEvent标签体,而定时事件其实就是该标签体指定了类型,也就是event Type属性值为 timer event。

如果不太明白可以参考我之前的文章:https://blog.csdn.net/qq_33333654/article/details/101363454

第二步设定定时事件的运行规则:

【activiti 入门】activiti6.0事件之定时事件

仔细看下定时事件的type属性,有三个值:

Main config中的数据格式均为:ISO_8601格式:(P ,Y,M,W,D,T,.H,M,S)或 cron时间格式:

Iso_8601格式:

-----Time duration:延时多长时间后触发。例:P10D 表示10天以后触发

-----time date:什么时间触发。例:2011-03-12T12:12:23 表示在2011年03 月12日 12:12:23触发

---time cycle:循环规则:

R【循环次数】【/开始时间】/时间间隔【/结束时间】。

例:R3/PT10 表示重复3次,每次间隔10小时

  从2004年5月6日北京时间下午1点起时间间隔半年零5天3小 时循环,且循环3次,可以表示为  

R3/2004-05-06T130000+08/P0Y6M5DT3H0M0S。

以1年2个月为循环间隔,无限次循环,最后循环终止于2025年1    月1日,可表示为R/P1Y2M/2025-01-01

Cron 时间格式:seconds minutes hours day-of -mouth mouth,day-of-week year

其中*表示这个域上所有的合法值

例:0 * 17 * * ? 表示每天下午5点到5:59每分钟触发一次

?表示不指定该域上的值(只能用在日域或周域上,且不能同时在两 个域上使用,即假如在在其中的一个域上指定了值,必要再另一 个上放?)

,表示在某个域上指定一个值列表

例:0 10,44 14 ? 3 WEB  表示三月中每个星期三的下午2:10 和下午的2:44触发

- 表示在某个域上指定一个值范围

例:0 45 3-8 ? * * 表示上午3点到上午8点的45分时触发

m/n 表示某个域的值从m开始,按n递增

例:0/15 0/30 * * * ? 表示整点和半点时每15秒触发

L 表示某个域上允许的最后一个值(只能用在日域和周域上,用在 日域上时,表示当月的最后一天触发;用在周域上,表示周的最 后一天,即周六触发)

例:0 0 12 ? * 2L 表示每个月中的最后一个星期一的12:00触发

W 表示里指定日期最近的工作日(只用在日域上,其只能指定单天, 不能指定范围或列表)

例:在日域上指定15W,那么如果15号是工作日,就在15号 触发

如果15号是周六,就在14号触发

如果15号是周日,就在16号触发

# 表示月份中的第几周的哪一天(只用在周域上)

例:6#3 表示某个月的第三个星期五

具体的iso和cron格式规范可自行百度。

另外一种用法就是定义的规则放到activiti的全局变量中通过${value}的方式进行获取调用。

第三步java代码:(仅供参考)

// 创建流程引擎

        ProcessEngineImpl engine = (ProcessEngineImpl)ProcessEngines

                .getDefaultProcessEngine();

        // 启动JobExecutor

        engine.getProcessEngineConfiguration().getJobExecutor().start();

        // 得到流程存储服务组件

        RepositoryService repositoryService = engine.getRepositoryService();

        // 得到运行时服务组件

        RuntimeService runtimeService = engine.getRuntimeService();

        TaskService taskService = engine.getTaskService();

        // 部署流程文件

        repositoryService.createDeployment()

                .addClasspathResource("bpmn/TimerCatchingEvent.bpmn").deploy();

        // 启动流程

        runtimeService.startProcessInstanceByKey("tcProcess");

        // 查询当前任务

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

        taskService.complete(currentTask.getId());

        Thread.sleep(1000 * 70);

        // 重新查询当前任务

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

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

        //关闭JobExecutor

        engine.getProcessEngineConfiguration().getJobExecutor().shutdown();

以上部分就是定时事件的相关操作,可做参考。