工作流activiti 6 学习笔记四 开始事件
【摘要】 activiti 开始事件
开始事件分以下5类:,他们都是捕获类事件。
第一类:StartEvent 通过RuntimeService 的startProcessInstanceById就可以创建流程。
第二类:TimerStartEvent 不需要启动流程,部署后,等待定时任务触发,定时任务触发条件定义在bpmn文件中,下面示例表示,每0分后5s启动一次流程
<startEvent id="timerstartevent1" name="Timer start">
<timerEventDefinition>
<timeCycle>0/5 * * * * ?</timeCycle>
</timerEventDefinition>
</startEvent>
关键代码
rs.createDeployment().addClasspathResource("timer.bpmn").deploy();
long count = runS.createProcessInstanceQuery().count();
System.out.println("count1"+count);
Thread.sleep(70*1000);
count = runS.createProcessInstanceQuery().count();
System.out.println("count2"+count);
第三类:messageStartEvent 是根据消息进行启动流程,在bpmn中定义消息,在start节点引用消息
<message id="messA" name="messName"></message>
<process id="myProcess" name="My process" isExecutable="true">
<startEvent id="messagestartevent1" name="Message start">
<messageEventDefinition messageRef="messA"></messageEventDefinition><!-- 引入消息值 -->
</startEvent>
<userTask id="usertask1" name="User Task"></userTask>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow1" sourceRef="messagestartevent1" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
</process>
// 部署流程
rs.createDeployment().addClasspathResource("message.bpmn").deploy();
ProcessInstance pi = runS.startProcessInstanceByMessage("messName");// bpmn 文件message节点的name值
System.out.println(pi.getId());
第四类:errorStartEvent,这个开始节点只能放在event sub process 节点中,
<error id="errorid" name="throwNname"></error>
<process id="myProcess" name="My process" isExecutable="true">
<subProcess id="eventsubprocess1" name="Event sub Process" triggeredByEvent="true">
<startEvent id="errorstartevent1" name="Error start">
<errorEventDefinition errorRef="errorid"></errorEventDefinition>
</startEvent>
<serviceTask id="servicetask1" name="report" activiti:class="org.scf.act.api.event.start.ReportDelegate"></serviceTask>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow3" sourceRef="errorstartevent1" targetRef="servicetask1"></sequenceFlow>
<sequenceFlow id="flow4" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow>
</subProcess>
<startEvent id="startevent1" name="Start"></startEvent>
<serviceTask id="servicetask2" name="count people" activiti:class="org.scf.act.api.event.start.CountDelegate"></serviceTask>
<endEvent id="endevent2" name="End"></endEvent>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask2"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="servicetask2" targetRef="endevent2"></sequenceFlow>
</process>
定义一个error节点,作为触发子流程的消息。在子流程serviceTask引用一个java类,在主流程中也引用一个java类,主流程抛出一个error,触发子流程。
package org.scf.act.api.event.start;
import org.activiti.engine.delegate.BpmnError;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
public class CountDelegate implements JavaDelegate{
@Override
public void execute(DelegateExecution execution) {
System.out.println("轻点人数开始");// 处理正常业务
//抛出error,则触发错误开始流程
throw new BpmnError("throwNname");
}
}
package org.scf.act.api.event.start;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;
public class ReportDelegate implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) {
System.out.println("上报流程开始了");
}
}
启动流程
// 部署流程
Deployment deploye = rs.createDeployment().addClasspathResource("errorStart.bpmn").deploy();
ProcessDefinition pdf = rs.createProcessDefinitionQuery().deploymentId(deploye.getId()).singleResult();
ProcessInstance pi = runS.startProcessInstanceById(pdf.getId());
System.out.println(pi.getId());
第五类:signalstartevent ,bmpn文件如下,先定义一个信号节点,然后在开始节点处引用这个节点
<signal id="signal" name="signalName"></signal>
<process id="signal1" name="my signal" isExecutable="true">
<startEvent id="signalstartevent1" name="Signal start">
<signalEventDefinition signalRef="signal"></signalEventDefinition>
</startEvent>
<userTask id="usertask1" name="User Task"></userTask>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow1" sourceRef="signalstartevent1" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
</process>
java代码
// 部署流程
Deployment deploye = rs.createDeployment().addClasspathResource("singleStart.bpmn").deploy();
ProcessDefinition pdf = rs.createProcessDefinitionQuery().deploymentId(deploye.getId()).singleResult();
runS.signalEventReceived("signalName");
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)