首先,考虑下面这样一个流程图
主要的流程定义如下:
<process id="demo" name="demo" isExecutable="true">
<startEvent id="sid-aee4f5b6-6b26-423d-85c3-499659fb523b"/>
<manualTask id="sid-f10234c0-9056-4b68-8422-f967d08c1cac" activiti:exclusive="true" name="人工任务1"/>
<manualTask id="sid-fad46cbb-9529-4685-83d1-95cf96dba9dc" activiti:exclusive="true" name="人工任务2">
<extensionElements>
<activiti:executionListener event="end" class="com.example.demo222.MyExecutionListener"/>
</extensionElements>
</manualTask>
<userTask id="sid-7049a00c-8eb8-4018-9210-dba9ece4dcf7" name="用户任务" activiti:assignee="zhangsan">
<extensionElements>
<activiti:taskListener event="complete" class="com.example.demo222.MyTaskCompleteListener"/>
</extensionElements>
</userTask>
<endEvent id="sid-5be26610-d40f-4745-87a9-f20462595a45"/>
<sequenceFlow id="sid-3b11c1ae-cdc2-4899-9014-0aa6ecc948a3" sourceRef="sid-aee4f5b6-6b26-423d-85c3-499659fb523b" targetRef="sid-f10234c0-9056-4b68-8422-f967d08c1cac"/>
<sequenceFlow id="sid-04d1ef58-433d-4bd5-ac73-dec836ee2856" sourceRef="sid-f10234c0-9056-4b68-8422-f967d08c1cac" targetRef="sid-fad46cbb-9529-4685-83d1-95cf96dba9dc"/>
<sequenceFlow id="sid-428c8fef-fe25-4c09-8d27-78f109f8312f" sourceRef="sid-fad46cbb-9529-4685-83d1-95cf96dba9dc" targetRef="sid-7049a00c-8eb8-4018-9210-dba9ece4dcf7"/>
<sequenceFlow id="sid-fb1b8968-35e5-4fde-85c9-8c8dbafcf75b" sourceRef="sid-7049a00c-8eb8-4018-9210-dba9ece4dcf7" targetRef="sid-5be26610-d40f-4745-87a9-f20462595a45"/>
</process>
两个人工任务,一个用户任务
依赖
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter</artifactId>
<version>7.1.0.M6</version>
</dependency>
流程执行监听器
package com.example.demo222;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
public class MyExecutionListener implements ExecutionListener {
@Override
public void notify(DelegateExecution execution) {
throw new RuntimeException("ExecutionListener报错了");
}
}
任务监听器
package com.example.demo222;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
public class MyTaskCompleteListener implements TaskListener {
@Override
public void notify(DelegateTask delegateTask) {
throw new RuntimeException("TaskListener报错了");
}
}
启动流程
package com.example.demo222;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class DemoTest {
@Autowired
private RepositoryService repositoryService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Test
void deploy() {
repositoryService.createDeployment()
.addClasspathResource("processes/demo.bpmn20.xml")
.name("demo")
.key("demo")
.tenantId("10086")
.deploy();
}
@Test
public void start() {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKeyAndTenantId("demo", "10086");
System.out.println(processInstance);
}
}
如此简单的一个流程,如此简单的代码,我以为流程会顺利启动成功,结果启动失败了
java.lang.RuntimeException: ExecutionListener报错了
at com.example.demo222.MyExecutionListener.notify(MyExecutionListener.java:9) ~[classes/:na]
at org.activiti.engine.impl.delegate.invocation.ExecutionListenerInvocation.invoke(ExecutionListenerInvocation.java:34) ~[activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.delegate.invocation.DelegateInvocation.proceed(DelegateInvocation.java:35) ~[activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.delegate.invocation.DefaultDelegateInterceptor.handleInvocation(DefaultDelegateInterceptor.java:25) ~[activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.bpmn.helper.ClassDelegate.notify(ClassDelegate.java:109) ~[activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.bpmn.listener.ListenerNotificationHelper.executeExecutionListeners(ListenerNotificationHelper.java:77) ~[activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.agenda.AbstractOperation.executeExecutionListeners(AbstractOperation.java:81) ~[activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.agenda.AbstractOperation.executeExecutionListeners(AbstractOperation.java:71) ~[activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.agenda.TakeOutgoingSequenceFlowsOperation.handleActivityEnd(TakeOutgoingSequenceFlowsOperation.java:99) ~[activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.agenda.TakeOutgoingSequenceFlowsOperation.handleFlowNode(TakeOutgoingSequenceFlowsOperation.java:84) ~[activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.agenda.TakeOutgoingSequenceFlowsOperation.run(TakeOutgoingSequenceFlowsOperation.java:77) ~[activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.interceptor.CommandInvoker.executeOperation(CommandInvoker.java:73) ~[activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.interceptor.CommandInvoker.executeOperations(CommandInvoker.java:57) ~[activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:42) ~[activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.interceptor.TransactionContextInterceptor.execute(TransactionContextInterceptor.java:48) ~[activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:59) ~[activiti-engine-7.1.0.M6.jar:na]
at org.activiti.spring.SpringTransactionInterceptor$1.doInTransaction(SpringTransactionInterceptor.java:47) [activiti-spring-7.1.0.M6.jar:na]
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140) [spring-tx-5.3.22.jar:5.3.22]
at org.activiti.spring.SpringTransactionInterceptor.execute(SpringTransactionInterceptor.java:45) [activiti-spring-7.1.0.M6.jar:na]
at org.activiti.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:29) [activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:44) [activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.cfg.CommandExecutorImpl.execute(CommandExecutorImpl.java:39) [activiti-engine-7.1.0.M6.jar:na]
at org.activiti.engine.impl.RuntimeServiceImpl.startProcessInstanceByKeyAndTenantId(RuntimeServiceImpl.java:93) [activiti-engine-7.1.0.M6.jar:na]
at com.example.demo222.DemoTest.start(DemoTest.java:32) [test-classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_333]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_333]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_333]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_333]
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725) [junit-platform-commons-1.8.2.jar:1.8.2]
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60) [junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$ValidatingInvocation.proceed(InvocationInterceptorChain.java:131) [junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149) [junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140) [junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84) [junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$7(TestMethodTestDescriptor.java:214) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:210) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:135) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:66) ~[junit-jupiter-engine-5.8.2.jar:5.8.2]
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:151) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at java.util.ArrayList.forEach(ArrayList.java:1259) ~[na:1.8.0_333]
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at java.util.ArrayList.forEach(ArrayList.java:1259) ~[na:1.8.0_333]
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:41) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$6(NodeTestTask.java:155) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:141) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:137) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$9(NodeTestTask.java:139) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:138) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:95) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:35) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:54) ~[junit-platform-engine-1.8.2.jar:1.8.2]
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:107) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:88) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.lambda$execute$0(EngineExecutionOrchestrator.java:54) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.withInterceptedStreams(EngineExecutionOrchestrator.java:67) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
at org.junit.platform.launcher.core.EngineExecutionOrchestrator.execute(EngineExecutionOrchestrator.java:52) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:114) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:86) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
at org.junit.platform.launcher.core.DefaultLauncherSession$DelegatingLauncher.execute(DefaultLauncherSession.java:86) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
at org.junit.platform.launcher.core.SessionPerRequestLauncher.execute(SessionPerRequestLauncher.java:53) ~[junit-platform-launcher-1.8.2.jar:1.8.2]
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:57) ~[junit5-rt.jar:na]
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) ~[junit-rt.jar:na]
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) ~[idea_rt.jar:na]
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) ~[junit-rt.jar:na]
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235) ~[junit-rt.jar:na]
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54) ~[junit-rt.jar:na]
我一直以为启动流程和任务流转是分开的,流程启动以后就往后走,启动时启动,任务流转是流转,二者应该互不影响的,没想到流程节点执行时报错会导致流程启动失败。直到看了代码,我才直到我错了。流程启动会一直往下执行,直到遇到一个它无法自动处理的节点(比如:User Task)才停下来。
下面通过代码了解启动流程实例的过程:
创建流程实例后返回一个ExecutionEntity,这个ExecutionEntity就是ProcessInstance
然后,将流程实例(ExecutionEntity)丢到Agenda中
往Agenda里放的是一个ContinueProcessOperation,所以接下来看ContinueProcessOperation
本例中,整个流程有9个FlowElement
现在,程序运行到这里,当前FlowElement(对应代码中的currentFlowElement)是开始节点,即StartEvent
因为它是FlowNode,所以接下来走节点的处理逻辑
默认情况下,节点是同步执行的,如果在任务定义时指定activiti:async="true",那么将会异步执行
另外,我们还知道了,在流程执行过程中,是以同步方式调用监听器的,具体见org.activiti.engine.impl.bpmn.listener.ListenerNotificationHelper#executeExecutionListeners
这里涉及到行为Behavior了,在本例中,设计到下面这几种Behavior
Start节点对应的行为是NoneStartEventActivityBehavior,它的行为就是离开活动
通过往Agenda中放一个TakeOutgoingSequenceFlowsOperation来实现离开活动
org.activiti.engine.impl.agenda.TakeOutgoingSequenceFlowsOperation#leaveFlowNode
刚才是走的FlowNode,现在再看SequenceFlow
将下一个FlowElement设置为currentFlowElement,然后又是Context.getAgenda().planContinueProcessOperation(execution);
离开当前活动后,就来到了下一个活动,本例中下一个活动(Activity)是Manual Task
Manual Task是一个直接通过的活动
当走到第二个人工任务的时候,执行监听器时抛异常了,于是启动流程也失败了。假设他没有抛异常继续往下走就遇到一个User Task
那么在执行UserTaskActivityBehavior的时候,正常情况下是不会离开活动的,所以就停在这里了
所以,到这里,针对本例中的这个流程我们可以总结一下:
1、创建流程实例ExecutionEntity,放入Agenda中继续执行
2、一个流程包含很多FlowElement,FlowElement分两大类FlowNode和SequenceFlow。FlowNode有关联的ActivityBehavior。
3、从当前FlowElement开始,如果是FlowNode,则执行与之关联的行为,如果是SequenceFlow则继续执行
整个过程只有在FlowNode里才有可能停下来,像StartEvent和ManuTask这样的FlowNode它们的行为都是离开活动,遇到向UserTask这样的才会停下来
所以,通过源码的学习可以得出两个知识点:
1、启动流程实例的过程中不仅仅是启动就完了,它还会继续执行活动,直到遇到一个它无法自动通过的活动时才会停止,在这期间不抛异常才算流程启动成功
2、监听器是同步执行的,如果在监听器里抛异常了,会影响到活动的执行。就像本例中一样,在人工任务的完成监听器里抛异常导致流程实例启动失败
3、默认情况下,活动是同步执行的
手机扫一扫
移动阅读更方便
你可能感兴趣的文章