这不,这周公司经理叫我在前段时间的基础上,把项目中的一个客户回访计划的提醒功能也加到工作流当中,对于没有接触过定时任务的我来说,又有的一番研究了。
在 咖啡兔 http://weibo.com/kafeituzi 的提醒下,使用了边界定时事件 + Job的方式成功实现了,任务定时启动,任务超时过期处理的功能,必须得感谢兔子,感谢他对Activiti在国内的普及所作出的努力。也希望Activiti的路越走越远,现在5.11版本还是和以前老版本有了很多的变化了,好用,简单实用的API,有了native query,也方便自己扩展。
好了,废话不多说,开始分析实例:
1.开启JOB引擎,在activiti配置文件中:
- <property name="jobExecutorActivate" value="true" />
<property name="jobExecutorActivate" value="true" />
这个必须开启,如果没有开启的话,定时任务是启动不了的。呵呵,
其实这个功能开启过后,在后台的实现是这样饿,它会定时的查询act_ru_job这一张表,看见我们的某一条记录的时间要求达到了,就会执行这一个JOB。是不是就明白了?如果是web项目,你打开LOG功能,在控制台是可以看见日志输出的,其实就是这个JOB引擎在工作。
2.下面就是流程图设计。
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <process id="notify" name="任务计划"isExecutable="true">
- <userTask id="usertask1" name="任务等待"></userTask>
- <endEvent id="endevent1" name="End"></endEvent>
- <startEvent id="startevent1" name="Start"></startEvent>
- <sequenceFlow id="flow5" sourceRef="startevent1"targetRef="usertask1"></sequenceFlow>
- <boundaryEvent id="boundarytimer1" name="Timer" attachedToRef="usertask1" cancelActivity="true">
- <timerEventDefinition>
- <timeDate>${qishi}</timeDate>
- </timerEventDefinition>
- </boundaryEvent>
- <userTask id="usertask2" name="任务提醒" activiti:candidateUsers="123">
- <extensionElements>
- <activiti:taskListenerevent="create"delegateExpression="${notifyHandler}"></activiti:taskListener>
- </extensionElements>
- </userTask>
- <sequenceFlow id="flow6" sourceRef="boundarytimer1"targetRef="usertask2"></sequenceFlow>
- <serviceTask id="servicetask3" name="任务处理" activiti:expression="${myHandler}"></serviceTask>
- <sequenceFlow id="flow7" sourceRef="usertask2"targetRef="servicetask3"></sequenceFlow>
- <sequenceFlow id="flow8" sourceRef="servicetask3"targetRef="endevent1"></sequenceFlow>
- <boundaryEvent id="boundarytimer2" name="Timer" attachedToRef="usertask2" cancelActivity="true">
- <timerEventDefinition>
- <timeDate>${jieshu}</timeDate>
- </timerEventDefinition>
- </boundaryEvent>
- <serviceTask id="servicetask4" name="任务过期" activiti:delegateExpression="${myHandler2}"></serviceTask>
- <sequenceFlow id="flow9" sourceRef="boundarytimer2"targetRef="servicetask4"></sequenceFlow>
- <sequenceFlow id="flow10" sourceRef="servicetask4"targetRef="endevent1"></sequenceFlow>
- </process>
- <bpmndi:BPMNDiagram id="BPMNDiagram_notify">
- <bpmndi:BPMNPlane bpmnElement="notify" id="BPMNPlane_notify">
- <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
- <omgdc:Bounds height="55.0" width="105.0" x="180.0" y="110.0"></omgdc:Bounds>
- </bpmndi:BPMNShape>
- <bpmndi:BPMNShape bpmnElement="boundarytimer1" id="BPMNShape_boundarytimer1">
- <omgdc:Bounds height="30.0" width="30.0" x="271.0" y="124.0"></omgdc:Bounds>
- </bpmndi:BPMNShape>
- <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
- <omgdc:Bounds height="35.0" width="35.0" x="670.0" y="120.0"></omgdc:Bounds>
- </bpmndi:BPMNShape>
- <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
- <omgdc:Bounds height="35.0" width="35.0" x="70.0" y="120.0"></omgdc:Bounds>
- </bpmndi:BPMNShape>
- <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
- <omgdc:Bounds height="55.0" width="105.0" x="330.0" y="110.0"></omgdc:Bounds>
- </bpmndi:BPMNShape>
- <bpmndi:BPMNShape bpmnElement="servicetask3" id="BPMNShape_servicetask3">
- <omgdc:Bounds height="55.0" width="105.0" x="500.0" y="110.0"></omgdc:Bounds>
- </bpmndi:BPMNShape>
- <bpmndi:BPMNShape bpmnElement="boundarytimer2" id="BPMNShape_boundarytimer2">
- <omgdc:Bounds height="30.0" width="30.0" x="405.0" y="150.0"></omgdc:Bounds>
- </bpmndi:BPMNShape>
- <bpmndi:BPMNShape bpmnElement="servicetask4" id="BPMNShape_servicetask4">
- <omgdc:Bounds height="55.0" width="105.0" x="500.0" y="210.0"></omgdc:Bounds>
- </bpmndi:BPMNShape>
- <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
- <omgdi:waypoint x="105.0" y="137.0"></omgdi:waypoint>
- <omgdi:waypoint x="180.0" y="137.0"></omgdi:waypoint>
- </bpmndi:BPMNEdge>
- <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
- <omgdi:waypoint x="301.0" y="139.0"></omgdi:waypoint>
- <omgdi:waypoint x="330.0" y="137.0"></omgdi:waypoint>
- </bpmndi:BPMNEdge>
- <bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
- <omgdi:waypoint x="435.0" y="137.0"></omgdi:waypoint>
- <omgdi:waypoint x="500.0" y="137.0"></omgdi:waypoint>
- </bpmndi:BPMNEdge>
- <bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
- <omgdi:waypoint x="605.0" y="137.0"></omgdi:waypoint>
- <omgdi:waypoint x="670.0" y="137.0"></omgdi:waypoint>
- </bpmndi:BPMNEdge>
- <bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
- <omgdi:waypoint x="420.0" y="180.0"></omgdi:waypoint>
- <omgdi:waypoint x="419.0" y="237.0"></omgdi:waypoint>
- <omgdi:waypoint x="500.0" y="237.0"></omgdi:waypoint>
- </bpmndi:BPMNEdge>
- <bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
- <omgdi:waypoint x="605.0" y="237.0"></omgdi:waypoint>
- <omgdi:waypoint x="687.0" y="237.0"></omgdi:waypoint>
- <omgdi:waypoint x="687.0" y="155.0"></omgdi:waypoint>
- </bpmndi:BPMNEdge>
- </bpmndi:BPMNPlane>
- </bpmndi:BPMNDiagram>
- </definitions>
<?xml version="1.0" encoding="UTF-8"?>
<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">
<process id="notify" name="任务计划" isExecutable="true">
<userTask id="usertask1" name="任务等待"></userTask>
<endEvent id="endevent1" name="End"></endEvent>
<startEvent id="startevent1" name="Start"></startEvent>
<sequenceFlow id="flow5" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<boundaryEvent id="boundarytimer1" name="Timer" attachedToRef="usertask1" cancelActivity="true">
<timerEventDefinition>
<timeDate>${qishi}</timeDate>
</timerEventDefinition>
</boundaryEvent>
<userTask id="usertask2" name="任务提醒" activiti:candidateUsers="123">
<extensionElements>
<activiti:taskListener event="create" delegateExpression="${notifyHandler}"></activiti:taskListener>
</extensionElements>
</userTask>
<sequenceFlow id="flow6" sourceRef="boundarytimer1" targetRef="usertask2"></sequenceFlow>
<serviceTask id="servicetask3" name="任务处理" activiti:expression="${myHandler}"></serviceTask>
<sequenceFlow id="flow7" sourceRef="usertask2" targetRef="servicetask3"></sequenceFlow>
<sequenceFlow id="flow8" sourceRef="servicetask3" targetRef="endevent1"></sequenceFlow>
<boundaryEvent id="boundarytimer2" name="Timer" attachedToRef="usertask2" cancelActivity="true">
<timerEventDefinition>
<timeDate>${jieshu}</timeDate>
</timerEventDefinition>
</boundaryEvent>
<serviceTask id="servicetask4" name="任务过期" activiti:delegateExpression="${myHandler2}"></serviceTask>
<sequenceFlow id="flow9" sourceRef="boundarytimer2" targetRef="servicetask4"></sequenceFlow>
<sequenceFlow id="flow10" sourceRef="servicetask4" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_notify">
<bpmndi:BPMNPlane bpmnElement="notify" id="BPMNPlane_notify">
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="180.0" y="110.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="boundarytimer1" id="BPMNShape_boundarytimer1">
<omgdc:Bounds height="30.0" width="30.0" x="271.0" y="124.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="670.0" y="120.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="70.0" y="120.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="330.0" y="110.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="servicetask3" id="BPMNShape_servicetask3">
<omgdc:Bounds height="55.0" width="105.0" x="500.0" y="110.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="boundarytimer2" id="BPMNShape_boundarytimer2">
<omgdc:Bounds height="30.0" width="30.0" x="405.0" y="150.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="servicetask4" id="BPMNShape_servicetask4">
<omgdc:Bounds height="55.0" width="105.0" x="500.0" y="210.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
<omgdi:waypoint x="105.0" y="137.0"></omgdi:waypoint>
<omgdi:waypoint x="180.0" y="137.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
<omgdi:waypoint x="301.0" y="139.0"></omgdi:waypoint>
<omgdi:waypoint x="330.0" y="137.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
<omgdi:waypoint x="435.0" y="137.0"></omgdi:waypoint>
<omgdi:waypoint x="500.0" y="137.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
<omgdi:waypoint x="605.0" y="137.0"></omgdi:waypoint>
<omgdi:waypoint x="670.0" y="137.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
<omgdi:waypoint x="420.0" y="180.0"></omgdi:waypoint>
<omgdi:waypoint x="419.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="500.0" y="237.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
<omgdi:waypoint x="605.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="687.0" y="237.0"></omgdi:waypoint>
<omgdi:waypoint x="687.0" y="155.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>