activiti流程启动的几种方式

  • 前言
  • 流程启动方式
      • 根据processDefinitionKey启动
      • 根据processDefinitionId启动
      • 根据message启动
      • 根据processInstanceBuilder启动

博客原文
https://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" />
        <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代码

@Test
    @org.activiti.engine.test.Deployment(resources = "my-process.bpmn20.xml")
    public void testStartProcessInstanceByKey() {

       RuntimeService runtimeService = activitiRule.getRuntimeService();

       Map 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代码

@Test
    @org.activiti.engine.test.Deployment(resources = "my-process.bpmn20.xml")
    public void testStartProcessInstanceById() {

        RuntimeService runtimeService = activitiRule.getRuntimeService();

        Map 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。

示例

流程定义文件

<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代码

@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来启动。。。

源码解析

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代码

@Test
    @org.activiti.engine.test.Deployment(resources = "my-process.bpmn20.xml")
    public void testProcessInstanceBuilder() {

        RuntimeService runtimeService = activitiRule.getRuntimeService();

        Map 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);
    }

你可能感兴趣的:(java)