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,流程定义文件如下:
<process id="my-pro
【一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义】
浏览器打开:qq.cn.hn/FTf 免费领取
cess">
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);
}