activiti流程启动的⼏种⽅式
zhangxingr.github.io/2018/09/06/activiti%E6%B5%81%E7%A8%8B%E5%90%AF%E5%8A%A8%E7%9A%84%E5%87%A0%E7%A7%8D%E6%96%B9%E5%BC%8F/#more 前⾔
最近在断断续续的学习activiti⼯作流引擎,为了增强⾃⼰对activiti的理解,特此将学习内容整理在博⽂上,⽅便⾃⼰回忆。
流程启动⽅式
activiti的流程启动总结来说有四种启动⽅式,分别是根据key启动、根据processDefinitionId启动、根据message启动,通过ProcessInstanceBuilder启动。
根据processDefinitionKey启动
根据processDefinitionKey启动也就是根据流程定义⽂件的key启动,是activiti最常⽤的启动⽅式。
⽰例
流程定义⽂件,key为my-process
<process id="my-process">
<startEvent id="start"/>
<quenceFlow id="flow1" sourceRef="start" targetRef="someTask"/>
<urTask id="someTask" name="Activiti is awesome!"/>
<quenceFlow id="flow2" sourceRef="someTask" targetRef="end"/>
<endEvent id="end"/>
</process>
java代码:
@Test
@st.Deployment(resources = "l")
public void testStartProcessInstanceByKey() {
RuntimeService runtimeService = RuntimeService();
Map<String, Object> map = wHashMap();
map.put("name", "zhangxingr");
map.put("x", "man");
map.put("age", "21");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("my-process", map);
logger.info("processInstance = {}", processInstance);负重俯卧撑
鲍鱼的烹饪方法}
根据processDefinitionId启动
⽰例
Java代码
销售实习总结@Test
@st.Deployment(resources = "l")
public void testStartProcessInstanceById() {
RuntimeService runtimeService = RuntimeService();
Map<String, Object> map = wHashMap();
过期酸奶
map.put("name", "zhangxingr");
map.put("x", "man");
map.put("age", "21");
ProcessDefinition processDefinition = RepositoryService()
.createProcessDefinitionQuery().singleResult();
ProcessInstance processInstance = runtimeService
.
Id(), map);
logger.info("processInstance = {}, process'key = {}, process'name = {}",
利用价值processInstance, ProcessDefinitionKey(),
}
根据message启动
根据message启动就要复杂⼀些,需要改动⼀下流程定义⽂件的startEvent,增加messageEventDefinition。
⽰例
流程定义⽂件
<message id="messageStart" name="my-message"/>
<process id="my-process">
<startEvent id="start">
<messageEventDefinition messageRef="messageStart"/>
</startEvent>
<quenceFlow id="flow1" sourceRef="start" targetRef="someTask"/>
<urTask id="someTask" name="Activiti is awesome!"/>
<quenceFlow id="flow2" sourceRef="someTask" targetRef="end"/>
<endEvent id="end"/>
</process>
Java代码
元宵节灯谜简单
@Test
@st.Deployment(resources = "l")
public void testMessageStart() {
RuntimeService runtimeService = RuntimeService();
ProcessInstance processInstance = runtimeService
.startProcessInstanceByMessage("my-message");
logger.info("processInstance = {}", processInstance);
}
根据message启动最终其实还是会⾛到⽤processDefinitionId来启动。。。
源码解析
public ProcessInstance execute(CommandContext commandContext) {
if (messageName == null) {
throw new ActivitiIllegalArgumentException("Cannot start process instance by message: message name is null");
}
MessageEventSubscriptionEntity messageEventSubscription = EventSubscriptionEntityManager().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 = Configuration();
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 = ProcessEngineConfiguration().getDeploymentManager();
ProcessDefinition processDefinition = deploymentCache.findDeployedProcessDefinitionById(processDefinitionId);
if (processDefinition == null) {
throw new ActivitiObjectNotFoundException("No process definition found for id '" + processDefinitionId + "'", ProcessDefinition.class);
}
ProcessInstanceHelper processInstanceHelper = ProcessEngineConfiguration().getProcessInstanceHelper();
ProcessInstance processInstance = ateAndStartProcessInstanceByMessage(processDefinition, messageName, processVariables, transientVariables);
return processInstance;
}
从上⾯可以看到,最终还是⽣成了⼀个processDefinitionId然后调createAndStartProcessInstanceByMessage来启动,具体可以去跟下源码。
根据processInstanceBuilder启动
Java代码
@Test
@st.Deployment(resources = "l")
public void testProcessInstanceBuilder() {
RuntimeService runtimeService = RuntimeService();
Map<String, Object> map = wHashMap();
map.put("name", "zhangxingr");
map.put("x", "man");
map.put("age", "21");
ProcessInstanceBuilder builder = ateProcessInstanceBuilder();运筹帷幄近义词
ProcessInstance processInstance = builder.processDefinitionKey("my-process")
.businessKey("businessKey001")
.name("我的流程实例")
.start();祝父母身体健康的句子
logger.info("processInstance = {}", processInstance);
}