StartEvent组件
StartEvent总述
StartEvent分为五种,第一种为普通StartEvent,第二种为带定时器的SatrtEvent,第三种为接收消息的StartEvent,第四种为带错误信息的StartEvent这种最为特殊,下文会详细来说。第五种为带信号的StartEvent从这篇文章主要讲工作流的StartEvent。下面开始逐一讲解。
StartEvent(普通)
StartEvent为一个流程的起始点,我们来看看官网是如何描述的:
StartEvent(普通)对应的XML内代码如下
<startEvent id="startevent1" name="Start"></startEvent>
activiti:initiator="initiator"
这个部分,activiti:initiator:在进程启动时将存储用户标识的变量名称。以initiator为UserId。一般设置这个是和form联用的【emmm我是不会去用的】。这里就是告诉大家可以用xml打开bpmn的代码有这个属性设置。通过eclipse插件可以直接如下图这样设置。
老规矩跑起来试试
可以看到已经进了历史数据表了【因为已经启动了start这个实例被执行了就进入了历史表】
starter:以这种方式设置就会是starter
participant:任务办理人,建议以流程变量的方式指定
candidate:候选人,组任务那块可以指定
TimerStartEvent(定时器启动组件)
打开允许异步操作
TimerStartEvent对应的XML内代码如下【未做任何设置的TimerStartEvent】
<startEvent id="timerstartevent1" name="Timer start">
<timerEventDefinition></timerEventDefinition>
</startEvent>
计时器启动事件用于在给定时间创建流程实例。它既可以用于应该只启动一次的进程,也可以用于应该以特定时间间隔启动的进程。
注意【摘自官网】:
- 子流程不能有TimerStartEvent。
- 一旦部署了进程,就会安排TimerStartEvent。不需要调用startProcessInstanceByXXX方法,虽然调用start process方法不受限制,并且会在startProcessInstanceByXXX Invocation时再启动一个进程。
- 当部署具有TimerStartEvent的新版本的进程时,将删除与先前计时器对应的作业。原因是通常不希望自动启动此旧版本流程的新流程实例。
使用的方式呢就是使用如下三个标签控制定时器开始事件。接下来是demo了。
TiemCycle【按时间循环启动】
流程图总览【UserTask以及后面的完全没有设置,for流程完整性和方便查看运行效果】
流程图XML
<?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:xsd="http://www.w3.org/2001/XMLSchema" 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="TiemCycle">
<process id="TiemCycle" name="TiemCycle" isExecutable="true">
<endEvent id="endevent1" name="End"></endEvent>
<userTask id="usertask1" name="User Task"></userTask>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
<startEvent id="timerstartevent1" name="Timer start">
<timerEventDefinition>
<timeCycle>0/5 * * * * ?</timeCycle>
</timerEventDefinition>
</startEvent>
<sequenceFlow id="flow3" sourceRef="timerstartevent1" targetRef="usertask1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_TiemCycle">
<bpmndi:BPMNPlane bpmnElement="TiemCycle" id="BPMNPlane_TiemCycle">
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="490.0" y="170.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="290.0" y="160.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="timerstartevent1" id="BPMNShape_timerstartevent1">
<omgdc:Bounds height="35.0" width="35.0" x="160.0" y="170.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="395.0" y="187.0"></omgdi:waypoint>
<omgdi:waypoint x="490.0" y="187.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="195.0" y="187.0"></omgdi:waypoint>
<omgdi:waypoint x="290.0" y="187.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
corn 表达式
这个代表每分钟的第0秒开始每五秒钟调用一次。【生成Corn表达式的网站,不必刻意去记忆 http://cron.qqe2.com/】
【ISO 8601方式】重复5次,每次间隔10小时
<timeCycle activiti:endDate="2019-09-12T21:42:00+00:00">R5/PT10H</timeCycle>
<timeCycle>R5/2019-10-07T12:00/PT10H</timeCycle>//这个是开始时间
测试代码
@Test
public void TiemCycleTest() throws InterruptedException{
Deployment deployment = repositoryService.createDeployment()/
.name("StartEventNormal")
.addClasspathResource("bpmn/TiemCycle.bpmn")
.addClasspathResource("bpmn/TiemCycle.png")
.deploy();
long dataCount = runtimeService.createProcessInstanceQuery().count();
System.out.println("sleep前流程实例数量:" + dataCount);
Thread.sleep(60000L);
dataCount = runtimeService.createProcessInstanceQuery().count();
System.out.println("sleep后流程实例数量:" + dataCount);
}
【上图Name见谅一下不想再去截图了。复制代码快了】
TimeDate【定时启动】
流程图总览
XML代码
<?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:xsd="http://www.w3.org/2001/XMLSchema" 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="myProcess" name="My process" isExecutable="true">
<startEvent id="timerstartevent1" name="Timer start">
<timerEventDefinition>
<timeDate>2018-09-12T17:20:00</timeDate>
</timerEventDefinition>
</startEvent>
<userTask id="usertask1" name="User Task"></userTask>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow1" sourceRef="timerstartevent1" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
<bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
<bpmndi:BPMNShape bpmnElement="timerstartevent1" id="BPMNShape_timerstartevent1">
<omgdc:Bounds height="35.0" width="35.0" x="210.0" y="240.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="360.0" y="230.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="590.0" y="240.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="245.0" y="257.0"></omgdi:waypoint>
<omgdi:waypoint x="360.0" y="257.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="465.0" y="257.0"></omgdi:waypoint>
<omgdi:waypoint x="590.0" y="257.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
时间的格式是有要求的
直接部署流程五点20时查看是否已经自动生成流程实例
@Test
public void TimeDate() throws InterruptedException{
Deployment deployment = repositoryService.createDeployment()
.name("TimeDate")
.addClasspathResource("bpmn/TimeDate.bpmn")
.addClasspathResource("bpmn/TimeDate.png")
.deploy();
long dataCount = runtimeService.createProcessInstanceQuery().count();
System.out.println("sleep前流程实例数量:" + dataCount);
Thread.sleep(60000L);//
dataCount = runtimeService.createProcessInstanceQuery().count();
System.out.println("sleep后流程实例数量:" + dataCount);
}
【单元测试线程代码运行完则退出,不会像主线程一样等待子线程退出而退出, 会直接退出。Junit单元测试不支持多线程。主线程退出,子线程也会退出。】
TimeDuration【延时启动】
微改一下,这里稍微偷懒一下啦。
@Test
public void TimeDate() throws InterruptedException{
Deployment deployment = repositoryService.createDeployment()
.name("TimeDate")
.addClasspathResource("bpmn/TimeDate.bpmn")
.addClasspathResource("bpmn/TimeDate.png")
.deploy();
long dataCount = runtimeService.createProcessInstanceQuery().count();
System.out.println("sleep前流程实例数量:" + dataCount);
Thread.sleep(5000L);
dataCount = runtimeService.createProcessInstanceQuery().count();
System.out.println("sleep后流程实例数量:" + dataCount);
}