Activiti 流程启动的几种方式

processDefinitionKey启动,代码如下:

@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);

}

2、processDefinitionId启动

@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());

}

3、message启动

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

message启动,代码如下:

@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来启动来,所以建议直接使用processDefinitionId方式启动,底层代码如下:

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;

最后

对于很多Java工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。

整理的这些资料希望对Java开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

再分享一波我的Java面试真题+视频学习详解+技能进阶书籍

美团二面惜败,我的凉经复盘(附学习笔记+面试整理+进阶书籍)

e;

最后

对于很多Java工程师而言,想要提升技能,往往是自己摸索成长,不成体系的学习效果低效漫长且无助。

整理的这些资料希望对Java开发的朋友们有所参考以及少走弯路,本文的重点是你有没有收获与成长,其余的都不重要,希望读者们能谨记这一点。

再分享一波我的Java面试真题+视频学习详解+技能进阶书籍

[外链图片转存中…(img-y7yvmf5M-1718729511496)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值