介绍: 在activiti中,事件按位置分可以有:开始事件、中间事件、边界时间、结束事件;按事件的特性区分有:捕获事件和抛出事件。除了事件,还有各种事件定义,常用的有:定时器事件定义、错误事件定义、信号事件定义、消息事件定义 、取消事件定义、补偿事件定义等。事件和事件定义可以进行组合,成为特定的事件。
此刻: 本文主要讲四种开始事件
<process id="myProcess_1" isClosed="false" isExecutable="true" processType="None">
<startEvent id="_2" name="StartEvent">
<timerEventDefinition>
<!--从每分钟的0s开始,每隔5秒开始一次流程(生成一个流程实例)-->
<timeCycle>0/5 * * * * ?</timeCycle>
</timerEventDefinition>
</startEvent>
<userTask activiti:exclusive="true" id="UserTask" name="UserTask"/>
<endEvent id="_4" name="EndEvent"/>
<sequenceFlow id="_5" sourceRef="_2" targetRef="UserTask"/>
<sequenceFlow id="_6" sourceRef="UserTask" targetRef="_4"/>
</process>
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
RuntimeService runtimeService = processEngine.getRuntimeService();
//部署流程后,不用我们自己启动流程实例,定时器的时间到了后会自动启动流程实例
Deployment deployment = repositoryService.createDeployment().addClasspathResource("timer-start-event.bpmn").deploy();
long count = runtimeService.createProcessInstanceQuery().count();
System.out.println("sleep前的流程实例个数:" + count);
//
//
//睡眠20s,5s运行一次,则会多4个流程实例
Thread.sleep(20 * 1000);
count = runtimeService.createProcessInstanceQuery().count();
System.out.println("sleep后的流程实例个数:" + count);
processEngine.close();
System.exit(0);
<message id="msg" name="msgName"></message>
<process id="myProcess_1" isClosed="false" isExecutable="true" processType="None">
<startEvent id="_2" name="StartEvent">
<!--开始事件引用消息-->
<messageEventDefinition messageRef="msg"></messageEventDefinition>
</startEvent>
<userTask activiti:exclusive="true" id="UserTask" name="UserTask"/>
<endEvent id="_4" name="EndEvent"/>
<sequenceFlow id="_5" sourceRef="_2" targetRef="UserTask"/>
<sequenceFlow id="_6" sourceRef="UserTask" targetRef="_4"/>
</process>
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
RuntimeService runtimeService = processEngine.getRuntimeService();
Deployment deployment = repositoryService.createDeployment().addClasspathResource("message-start-event.bpmn").deploy();
//通过消息启动流程实例,在一个项目中的消息事件会存储在表act_ru_event_subscr中,
// 并且因为流程实例依靠具体的消息来启动,因此在该项目中的消息需要唯一。例:如果一个流程的消息name定义为"msgName",第一次部署时act_ru_event_subscr
// 表会存储消息与部署的流程定义的关联,之后再部署相同的消息时,不会再增加记录,不会与消息绑定;即只有第一绑定消息的流程定义是有效的。
ProcessInstance processInstance = runtimeService.startProcessInstanceByMessage("msgName");
System.out.println("流程实例ID:" + processInstance.getId());
processEngine.close();
System.exit(0);
4.结果查看:
运行结果:流程实例ID:345012
表act_ru_event_subscr:
介绍: 此处模拟流程为,一个班级,班长清点人数,发现人数少了后,马上去报告班主任。
<error id="errorId" errorCode="abc"></error>
<process id="myProcess_1" isClosed="false" isExecutable="true" processType="None">
<startEvent id="_2" name="StartEvent"/>
<serviceTask activiti:exclusive="true" id="CountPeople" name="CountPeople"
activiti:class="com.xjf.test.delegate.CountPeopleDelegate"/>
<endEvent id="_4" name="EndEvent"/>
<sequenceFlow id="_5" sourceRef="_2" targetRef="CountPeople"/>
<sequenceFlow id="_6" sourceRef="CountPeople" targetRef="_4"/>
<!--triggeredByEvent配置必须为true,默认是false-->
<subProcess activiti:exclusive="true" id="_7" name="SubProcess" triggeredByEvent="true">
<startEvent id="_8" name="StartEvent">
<errorEventDefinition errorRef="errorId"></errorEventDefinition>
</startEvent>
<serviceTask activiti:exclusive="true" id="Report" name="Report"
activiti:class="com.xjf.test.delegate.ReportDelegate"/>
<endEvent id="_10" name="EndEvent"/>
<sequenceFlow id="_3" sourceRef="_8" targetRef="Report"/>
<sequenceFlow id="_11" sourceRef="Report" targetRef="_10"/>
</subProcess>
</process>
public class CountPeopleDelegate implements JavaDelegate {
@Override
public void execute(DelegateExecution delegateExecution) {
System.out.println("清点人数,要抛出错误");
//抛出错误,子流程的错误开始事件会捕获
throw new BpmnError("abc");
}
}
public class ReportDelegate implements JavaDelegate {
@Override
public void execute(DelegateExecution delegateExecution) {
System.out.println("少人了,准备上报");
}
}
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
RuntimeService runtimeService = processEngine.getRuntimeService();
Deployment deployment = repositoryService.createDeployment().addClasspathResource("error-start-event.bpmn").deploy();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
//流程实例启动后,会自动执行:主流程抛出错误,子流程捕获到错误然后一直执行完
ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
System.out.println("流程实例id:" + processInstance.getId());
processEngine.close();
System.exit(0);