Recently, I have been learning the Activiti workflow engine intermittently. In order to enhance my understanding of Activiti, I hereby sort out the learning content in my blog for my convenience.

There are four startup options for Activiti: Key startup, processDefinitionId startup, Message startup, and ProcessInstanceBuilder startup.

ProcessDefinitionKey is started

ProcessDefinitionKey startup is the most common startup method for Activiti (processDefinitionKey, 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>
Copy the code

ProcessDefinitionKey starts with the following code:

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

    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);
}
Copy the code

ProcessDefinitionId starts

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

    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());
}
Copy the code

3. Message starts

MessageEventDefinition = messageEventDefinition = 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>
Copy the code

Message starts with the following code:

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

    RuntimeService runtimeService = activitiRule.getRuntimeService();

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

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

ProcessDefinitionId is recommended for boot with processDefinitionId. The underlying code looks like this:

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;
}
Copy the code

Can see from the above that eventually generates a processDefinitionId then adjustable createAndStartProcessInstanceByMessage to start, can go to the source code.

4. ProcessInstanceBuilder starts

@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(" my process instance ").start(); logger.info("processInstance = {}", processInstance); }Copy the code

Several ways to complete this Activiti process startup are described.