如何使用 fluent builder 创建多实例 camunda 子流程 api

How to create a multi instance camunda subprocess with fluent builder api

我正在尝试开发一个 camunda 进程,但我不知道如何实现一个多实例子进程来遍历一个集合。

例如:

SubProcess subProcess = modelInstance.getModelElementById("elementVersionId-" + element.getId().toString());

subProcess.builder().multiInstance().multiInstanceDone() //Cant add a start event after multinstance done

将 multiInstanceDone 添加到子进程后,我无法使用 startEvent 启动子进程。

有没有人有想法,例子可以帮助我?

希望对您有所帮助:

import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.model.bpmn.Bpmn;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.bpmn.builder.MultiInstanceLoopCharacteristicsBuilder;
import org.camunda.bpm.model.bpmn.instance.*;

import java.io.File;

@Slf4j
public class MultiInstanceSubprocess {

  public static final String MULTI_INSTANCE_PROCESS = "myMultiInstanceProcess";

  // @see https://docs.camunda.org/manual/latest/user-guide/model-api/bpmn-model-api/fluent-builder-api/
  public static void main(String[] args) {

    BpmnModelInstance modelInst;
    try {
      File file = new File("./src/main/resources/multiInstance.bpmn");
      modelInst = Bpmn.createProcess()
          .id("MyParentProcess")
          .executable()
          .startEvent("ProcessStarted")
          .subProcess(MULTI_INSTANCE_PROCESS)
          //first create sub process content
          .embeddedSubProcess()
          .startEvent("subProcessStartEvent")
          .userTask("UserTask1")
          .endEvent("subProcessEndEvent")
          .subProcessDone()
          .endEvent("ParentEnded").done();

      // Add multi-instance loop characteristics to embedded sub process
      SubProcess subProcess = modelInst.getModelElementById(MULTI_INSTANCE_PROCESS);
      subProcess.builder()
          .multiInstance()
          .camundaCollection("myCollection")
          .camundaElementVariable("myVar")
          .multiInstanceDone();

      log.info("Flow Elements - Name : Id : Type Name");
      modelInst.getModelElementsByType(FlowNode.class).forEach(e -> log.info("{} : {} : {}", e.getName(), e.getId(), e.getElementType().getTypeName()));

      Bpmn.writeModelToFile(file, modelInst);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}