Activiti定时任务示例

时间:2021-09-12 07:51:52
工作流其实做的事情还是很多的,
这不,这周公司经理叫我在前段时间的基础上,把项目中的一个客户回访计划的提醒功能也加到工作流当中,对于没有接触过定时任务的我来说,又有的一番研究了。
在 咖啡兔 http://weibo.com/kafeituzi 的提醒下,使用了边界定时事件 + Job的方式成功实现了,任务定时启动,任务超时过期处理的功能,必须得感谢兔子,感谢他对Activiti在国内的普及所作出的努力。也希望Activiti的路越走越远,现在5.11版本还是和以前老版本有了很多的变化了,好用,简单实用的API,有了native query,也方便自己扩展。
好了,废话不多说,开始分析实例:
1.开启JOB引擎,在activiti配置文件中:
Java代码 Activiti定时任务示例 Activiti定时任务示例Activiti定时任务示例
  1. <property name="jobExecutorActivate" value="true" />

这个必须开启,如果没有开启的话,定时任务是启动不了的。呵呵,
其实这个功能开启过后,在后台的实现是这样饿,它会定时的查询act_ru_job这一张表,看见我们的某一条记录的时间要求达到了,就会执行这一个JOB。是不是就明白了?如果是web项目,你打开LOG功能,在控制台是可以看见日志输出的,其实就是这个JOB引擎在工作。
2.下面就是流程图设计。
Activiti定时任务示例
Xml代码 Activiti定时任务示例 Activiti定时任务示例Activiti定时任务示例
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"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/test">
  3. <process id="notify" name="任务计划"isExecutable="true">
  4. <userTask id="usertask1" name="任务等待"></userTask>
  5. <endEvent id="endevent1" name="End"></endEvent>
  6. <startEvent id="startevent1" name="Start"></startEvent>
  7. <sequenceFlow id="flow5" sourceRef="startevent1"targetRef="usertask1"></sequenceFlow>
  8. <boundaryEvent id="boundarytimer1" name="Timer" attachedToRef="usertask1" cancelActivity="true">
  9. <timerEventDefinition>
  10. <timeDate>${qishi}</timeDate>
  11. </timerEventDefinition>
  12. </boundaryEvent>
  13. <userTask id="usertask2" name="任务提醒" activiti:candidateUsers="123">
  14. <extensionElements>
  15. <activiti:taskListenerevent="create"delegateExpression="${notifyHandler}"></activiti:taskListener>
  16. </extensionElements>
  17. </userTask>
  18. <sequenceFlow id="flow6" sourceRef="boundarytimer1"targetRef="usertask2"></sequenceFlow>
  19. <serviceTask id="servicetask3" name="任务处理" activiti:expression="${myHandler}"></serviceTask>
  20. <sequenceFlow id="flow7" sourceRef="usertask2"targetRef="servicetask3"></sequenceFlow>
  21. <sequenceFlow id="flow8" sourceRef="servicetask3"targetRef="endevent1"></sequenceFlow>
  22. <boundaryEvent id="boundarytimer2" name="Timer" attachedToRef="usertask2" cancelActivity="true">
  23. <timerEventDefinition>
  24. <timeDate>${jieshu}</timeDate>
  25. </timerEventDefinition>
  26. </boundaryEvent>
  27. <serviceTask id="servicetask4" name="任务过期" activiti:delegateExpression="${myHandler2}"></serviceTask>
  28. <sequenceFlow id="flow9" sourceRef="boundarytimer2"targetRef="servicetask4"></sequenceFlow>
  29. <sequenceFlow id="flow10" sourceRef="servicetask4"targetRef="endevent1"></sequenceFlow>
  30. </process>
  31. <bpmndi:BPMNDiagram id="BPMNDiagram_notify">
  32. <bpmndi:BPMNPlane bpmnElement="notify" id="BPMNPlane_notify">
  33. <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
  34. <omgdc:Bounds height="55.0" width="105.0" x="180.0" y="110.0"></omgdc:Bounds>
  35. </bpmndi:BPMNShape>
  36. <bpmndi:BPMNShape bpmnElement="boundarytimer1" id="BPMNShape_boundarytimer1">
  37. <omgdc:Bounds height="30.0" width="30.0" x="271.0" y="124.0"></omgdc:Bounds>
  38. </bpmndi:BPMNShape>
  39. <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
  40. <omgdc:Bounds height="35.0" width="35.0" x="670.0" y="120.0"></omgdc:Bounds>
  41. </bpmndi:BPMNShape>
  42. <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
  43. <omgdc:Bounds height="35.0" width="35.0" x="70.0" y="120.0"></omgdc:Bounds>
  44. </bpmndi:BPMNShape>
  45. <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
  46. <omgdc:Bounds height="55.0" width="105.0" x="330.0" y="110.0"></omgdc:Bounds>
  47. </bpmndi:BPMNShape>
  48. <bpmndi:BPMNShape bpmnElement="servicetask3" id="BPMNShape_servicetask3">
  49. <omgdc:Bounds height="55.0" width="105.0" x="500.0" y="110.0"></omgdc:Bounds>
  50. </bpmndi:BPMNShape>
  51. <bpmndi:BPMNShape bpmnElement="boundarytimer2" id="BPMNShape_boundarytimer2">
  52. <omgdc:Bounds height="30.0" width="30.0" x="405.0" y="150.0"></omgdc:Bounds>
  53. </bpmndi:BPMNShape>
  54. <bpmndi:BPMNShape bpmnElement="servicetask4" id="BPMNShape_servicetask4">
  55. <omgdc:Bounds height="55.0" width="105.0" x="500.0" y="210.0"></omgdc:Bounds>
  56. </bpmndi:BPMNShape>
  57. <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
  58. <omgdi:waypoint x="105.0" y="137.0"></omgdi:waypoint>
  59. <omgdi:waypoint x="180.0" y="137.0"></omgdi:waypoint>
  60. </bpmndi:BPMNEdge>
  61. <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
  62. <omgdi:waypoint x="301.0" y="139.0"></omgdi:waypoint>
  63. <omgdi:waypoint x="330.0" y="137.0"></omgdi:waypoint>
  64. </bpmndi:BPMNEdge>
  65. <bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
  66. <omgdi:waypoint x="435.0" y="137.0"></omgdi:waypoint>
  67. <omgdi:waypoint x="500.0" y="137.0"></omgdi:waypoint>
  68. </bpmndi:BPMNEdge>
  69. <bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
  70. <omgdi:waypoint x="605.0" y="137.0"></omgdi:waypoint>
  71. <omgdi:waypoint x="670.0" y="137.0"></omgdi:waypoint>
  72. </bpmndi:BPMNEdge>
  73. <bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
  74. <omgdi:waypoint x="420.0" y="180.0"></omgdi:waypoint>
  75. <omgdi:waypoint x="419.0" y="237.0"></omgdi:waypoint>
  76. <omgdi:waypoint x="500.0" y="237.0"></omgdi:waypoint>
  77. </bpmndi:BPMNEdge>
  78. <bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
  79. <omgdi:waypoint x="605.0" y="237.0"></omgdi:waypoint>
  80. <omgdi:waypoint x="687.0" y="237.0"></omgdi:waypoint>
  81. <omgdi:waypoint x="687.0" y="155.0"></omgdi:waypoint>
  82. </bpmndi:BPMNEdge>
  83. </bpmndi:BPMNPlane>
  84. </bpmndi:BPMNDiagram>
  85. </definitions>