activiti 7 实现边界定时任务
1.首先先画流程图
- 上图的定时边界任务是非阻塞定时边界任务 也可以是阻塞的,具体看业务需求
bpmn文件
<?xml version="1.0" encoding="UTF-8"?>
<bpmn2:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn2="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:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:activiti="http://activiti.org/bpmn" id="sample-diagram" targetNamespace="http://activiti.org/bpmn" xsi:schemaLocation="http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd">
<bpmn2:process id="Process_12121" isExecutable="true">
<bpmn2:startEvent id="Event_1uomw0p" name="开始">
<bpmn2:outgoing>Flow_1wifyui</bpmn2:outgoing>
</bpmn2:startEvent>
<bpmn2:userTask id="Activity_1gzz3oz" name="总经理审批" activiti:assignee="zhangsan">
<bpmn2:incoming>Flow_1wifyui</bpmn2:incoming>
<bpmn2:outgoing>Flow_0tfspq7</bpmn2:outgoing>
</bpmn2:userTask>
<bpmn2:sequenceFlow id="Flow_1wifyui" sourceRef="Event_1uomw0p" targetRef="Activity_1gzz3oz" />
<bpmn2:userTask id="Activity_14jhj9o" name="财务审批" activiti:assignee="lisi">
<bpmn2:incoming>Flow_0tfspq7</bpmn2:incoming>
<bpmn2:outgoing>Flow_1ppl94m</bpmn2:outgoing>
</bpmn2:userTask>
<bpmn2:sequenceFlow id="Flow_0tfspq7" sourceRef="Activity_1gzz3oz" targetRef="Activity_14jhj9o" />
<bpmn2:endEvent id="Event_0i1u941" name="结束">
<bpmn2:incoming>Flow_1ppl94m</bpmn2:incoming>
</bpmn2:endEvent>
<bpmn2:sequenceFlow id="Flow_1ppl94m" sourceRef="Activity_14jhj9o" targetRef="Event_0i1u941" />
<bpmn2:boundaryEvent id="Event_0vmre63" name="超时1分钟" cancelActivity="false" attachedToRef="Activity_1gzz3oz">
<bpmn2:outgoing>Flow_0i3bao6</bpmn2:outgoing>
<bpmn2:timerEventDefinition id="TimerEventDefinition_1uz66l9">
<bpmn2:timeDuration xsi:type="bpmn2:tFormalExpression">${date}</bpmn2:timeDuration>
</bpmn2:timerEventDefinition>
</bpmn2:boundaryEvent>
<bpmn2:sequenceFlow id="Flow_0i3bao6" sourceRef="Event_0vmre63" targetRef="Activity_0omyl8v" />
<bpmn2:serviceTask id="Activity_0omyl8v" name="发送任务" activiti:class="com.example.demo.config.TimeoutTaskListener">
<bpmn2:incoming>Flow_0i3bao6</bpmn2:incoming>
<bpmn2:outgoing>Flow_19z7u10</bpmn2:outgoing>
</bpmn2:serviceTask>
<bpmn2:endEvent id="Event_1is7f4l" name="结束">
<bpmn2:incoming>Flow_19z7u10</bpmn2:incoming>
</bpmn2:endEvent>
<bpmn2:sequenceFlow id="Flow_19z7u10" sourceRef="Activity_0omyl8v" targetRef="Event_1is7f4l" />
</bpmn2:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">
<bpmndi:BPMNEdge id="Flow_1wifyui_di" bpmnElement="Flow_1wifyui">
<di:waypoint x="268" y="250" />
<di:waypoint x="320" y="250" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_0tfspq7_di" bpmnElement="Flow_0tfspq7">
<di:waypoint x="420" y="250" />
<di:waypoint x="480" y="250" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_1ppl94m_di" bpmnElement="Flow_1ppl94m">
<di:waypoint x="580" y="250" />
<di:waypoint x="642" y="250" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_0i3bao6_di" bpmnElement="Flow_0i3bao6">
<di:waypoint x="390" y="308" />
<di:waypoint x="390" y="370" />
<di:waypoint x="460" y="370" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_19z7u10_di" bpmnElement="Flow_19z7u10">
<di:waypoint x="560" y="370" />
<di:waypoint x="612" y="370" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="Event_1uomw0p_di" bpmnElement="Event_1uomw0p">
<dc:Bounds x="232" y="232" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="239" y="275" width="23" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_1gzz3oz_di" bpmnElement="Activity_1gzz3oz">
<dc:Bounds x="320" y="210" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_14jhj9o_di" bpmnElement="Activity_14jhj9o">
<dc:Bounds x="480" y="210" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_0i1u941_di" bpmnElement="Event_0i1u941">
<dc:Bounds x="642" y="232" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="649" y="275" width="23" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_1u8kcnv_di" bpmnElement="Activity_0omyl8v">
<dc:Bounds x="460" y="330" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_1is7f4l_di" bpmnElement="Event_1is7f4l">
<dc:Bounds x="612" y="352" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="619" y="395" width="23" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_132mlyq_di" bpmnElement="Event_0vmre63">
<dc:Bounds x="372" y="272" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="366" y="315" width="50" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn2:definitions>
2.编写监听器
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* @author DEMO
*/
@Component
public class TimeoutTaskListener implements JavaDelegate {
@Override
public void execute(DelegateExecution delegateExecution) {
System.out.println("============================发送消息了呀 大兄弟============================");
}
}
因为业务需要做到超时预警,所以具体监听器就只有发送消息的作用 ,当然也可以记录日志…等操作
3.也可以使用任务监听器实现
public class TimeOutTaskListener implements TaskRuntimeEventListener {
@Override
public void onEvent(RuntimeEvent runtimeEvent) {
System.out.println("============================发送消息了呀 大兄弟============================");
}
}
}