activiti流程启动的几种方式

前言

最近在断断续续的学习activiti工作流引擎,为了增强自己对activiti的理解,特此将学习内容整理在博文上,方便自己回忆。

流程启动方式

activiti的流程启动总结来说有四种启动方式,分别是根据key启动、根据processDefinitionId启动、根据message启动,通过ProcessInstanceBuilder启动。

根据processDefinitionKey启动

根据processDefinitionKey启动也就是根据流程定义文件的key启动,是activiti最常用的启动方式。

示例

流程定义文件,key为my-process

1
2
3
4
5
6
7
8
9
10
11
<process id="my-process">

<startEvent id="start" />
<sequenceFlow id="flow1" sourceRef="start" targetRef="someTask" />

<userTask id="someTask" name="Activiti is awesome!" />
<sequenceFlow id="flow2" sourceRef="someTask" targetRef="end" />

<endEvent id="end" />

</process>

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Test
@org.activiti.engine.test.Deployment(resources = "my-process.bpmn20.xml")
public void testStartProcessInstanceByKey() {

RuntimeService runtimeService = activitiRule.getRuntimeService();

Map<String, Object> map = Maps.newHashMap();
map.put("name", "zhangxingr");
map.put("sex", "man");
map.put("age", "21");

ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process", map);
logger.info("processInstance = {}", processInstance);
}

根据processDefinitionId启动

示例

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Test
@org.activiti.engine.test.Deployment(resources = "my-process.bpmn20.xml")
public void testStartProcessInstanceById() {

RuntimeService runtimeService = activitiRule.getRuntimeService();

Map<String, Object> map = Maps.newHashMap();
map.put("name", "zhangxingr");
map.put("sex", "man");
map.put("age", "21");

ProcessDefinition processDefinition = activitiRule.getRepositoryService()
.createProcessDefinitionQuery().singleResult();

ProcessInstance processInstance = runtimeService
.startProcessInstanceById(processDefinition.getId(), map);
logger.info("processInstance = {}, process'key = {}, process'name = {}",
processInstance, processInstance.getProcessDefinitionKey(),
processInstance.getName());
}

根据message启动

根据message启动就要复杂一些,需要改动一下流程定义文件的startEvent,增加messageEventDefinition。

示例

流程定义文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<message id="messageStart" name="my-message"/>

<process id="my-process">

<startEvent id="start">
<messageEventDefinition messageRef="messageStart"/>
</startEvent>
<sequenceFlow id="flow1" sourceRef="start" targetRef="someTask" />

<userTask id="someTask" name="Activiti is awesome!" />
<sequenceFlow id="flow2" sourceRef="someTask" targetRef="end" />

<endEvent id="end" />

</process>

Java代码

1
2
3
4
5
6
7
8
9
10
11
@Test
@org.activiti.engine.test.Deployment(resources = "my-process-message.bpmn20.xml")
public void testMessageStart() {

RuntimeService runtimeService = activitiRule.getRuntimeService();

ProcessInstance processInstance = runtimeService
.startProcessInstanceByMessage("my-message");

logger.info("processInstance = {}", processInstance);
}

根据message启动最终其实还是会走到用processDefinitionId来启动。。。

源码解析

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public ProcessInstance execute(CommandContext commandContext) {

if (messageName == null) {
throw new ActivitiIllegalArgumentException("Cannot start process instance by message: message name is null");
}

MessageEventSubscriptionEntity messageEventSubscription = commandContext.getEventSubscriptionEntityManager().findMessageStartEventSubscriptionByName(messageName, tenantId);

if (messageEventSubscription == null) {
throw new ActivitiObjectNotFoundException("Cannot start process instance by message: no subscription to message with name '" + messageName + "' found.", MessageEventSubscriptionEntity.class);
}

String processDefinitionId = messageEventSubscription.getConfiguration();
if (processDefinitionId == null) {
throw new ActivitiException("Cannot start process instance by message: subscription to message with name '" + messageName + "' is not a message start event.");
}

DeploymentManager deploymentCache = commandContext.getProcessEngineConfiguration().getDeploymentManager();

ProcessDefinition processDefinition = deploymentCache.findDeployedProcessDefinitionById(processDefinitionId);
if (processDefinition == null) {
throw new ActivitiObjectNotFoundException("No process definition found for id '" + processDefinitionId + "'", ProcessDefinition.class);
}

ProcessInstanceHelper processInstanceHelper = commandContext.getProcessEngineConfiguration().getProcessInstanceHelper();
ProcessInstance processInstance = processInstanceHelper.createAndStartProcessInstanceByMessage(processDefinition, messageName, processVariables, transientVariables);

return processInstance;
}

从上面可以看到,最终还是生成了一个processDefinitionId然后调createAndStartProcessInstanceByMessage来启动,具体可以去跟下源码。

根据processInstanceBuilder启动

Java代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Test
@org.activiti.engine.test.Deployment(resources = "my-process.bpmn20.xml")
public void testProcessInstanceBuilder() {

RuntimeService runtimeService = activitiRule.getRuntimeService();

Map<String, Object> map = Maps.newHashMap();
map.put("name", "zhangxingr");
map.put("sex", "man");
map.put("age", "21");

ProcessInstanceBuilder builder = runtimeService.createProcessInstanceBuilder();
ProcessInstance processInstance = builder.processDefinitionKey("my-process")
.businessKey("businessKey001")
.name("我的流程实例")
.start();

logger.info("processInstance = {}", processInstance);
}
zhangxingrui wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!