camunda没有按预期设置变量

时间:2018-01-17 08:20:16

标签: java camunda

我有这个bpmn模型:

enter image description here

所有任务都只有一个名称字段的表单,task1的名称字段值应该确定从专用网关到哪里。

它存储在名为test1.bpmn的文件中,格式为XML:

<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="1.11.3">
  <bpmn:process id="process1" isExecutable="true">
    <bpmn:startEvent id="StartEvent_1">
      <bpmn:outgoing>SequenceFlow_0ce6se1</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:sequenceFlow id="SequenceFlow_0ce6se1" sourceRef="StartEvent_1" targetRef="task1" />
    <bpmn:exclusiveGateway id="gateway1">
      <bpmn:incoming>SequenceFlow_0gm7v19</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_1oyhcn0</bpmn:outgoing>
      <bpmn:outgoing>SequenceFlow_0b9aff3</bpmn:outgoing>
    </bpmn:exclusiveGateway>
    <bpmn:sequenceFlow id="SequenceFlow_1oyhcn0" sourceRef="gateway1" targetRef="task3">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression"><![CDATA[${name=="amer"}]]></bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:sequenceFlow id="SequenceFlow_0b9aff3" sourceRef="gateway1" targetRef="task2">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression"><![CDATA[${name=="amer"}]]></bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:endEvent id="EndEvent_11431dt">
      <bpmn:incoming>SequenceFlow_0xtdrz5</bpmn:incoming>
      <bpmn:incoming>SequenceFlow_12digm0</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:sequenceFlow id="SequenceFlow_0xtdrz5" sourceRef="task3" targetRef="EndEvent_11431dt" />
    <bpmn:sequenceFlow id="SequenceFlow_12digm0" sourceRef="task2" targetRef="EndEvent_11431dt" />
    <bpmn:userTask id="task2" name="task2" camunda:formKey="task2form">
      <bpmn:extensionElements>
        <camunda:formData>
          <camunda:formField id="name" type="string" />
        </camunda:formData>
      </bpmn:extensionElements>
      <bpmn:incoming>SequenceFlow_0b9aff3</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_12digm0</bpmn:outgoing>
    </bpmn:userTask>
    <bpmn:userTask id="task3" name="task3" camunda:formKey="task3form">
      <bpmn:extensionElements>
        <camunda:formData>
          <camunda:formField id="name" type="string" />
        </camunda:formData>
      </bpmn:extensionElements>
      <bpmn:incoming>SequenceFlow_1oyhcn0</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_0xtdrz5</bpmn:outgoing>
    </bpmn:userTask>
    <bpmn:userTask id="task1" name="task1" camunda:formKey="task1form">
      <bpmn:extensionElements>
        <camunda:formData>
          <camunda:formField id="name" label="name" type="string" />
        </camunda:formData>
      </bpmn:extensionElements>
      <bpmn:incoming>SequenceFlow_0ce6se1</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_0gm7v19</bpmn:outgoing>
    </bpmn:userTask>
    <bpmn:sequenceFlow id="SequenceFlow_0gm7v19" sourceRef="task1" targetRef="gateway1" />
  </bpmn:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="process1">
      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
        <dc:Bounds x="153" y="102" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="126" y="138" width="90" height="20" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_0ce6se1_di" bpmnElement="SequenceFlow_0ce6se1">
        <di:waypoint xsi:type="dc:Point" x="189" y="120" />
        <di:waypoint xsi:type="dc:Point" x="235" y="120" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="167" y="99" width="90" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="ExclusiveGateway_1t2paul_di" bpmnElement="gateway1" isMarkerVisible="true">
        <dc:Bounds x="385" y="95" width="50" height="50" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="410" y="149" width="0" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_1oyhcn0_di" bpmnElement="SequenceFlow_1oyhcn0">
        <di:waypoint xsi:type="dc:Point" x="410" y="145" />
        <di:waypoint xsi:type="dc:Point" x="410" y="306" />
        <di:waypoint xsi:type="dc:Point" x="394" y="306" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="425" y="219.5" width="0" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_0b9aff3_di" bpmnElement="SequenceFlow_0b9aff3">
        <di:waypoint xsi:type="dc:Point" x="435" y="120" />
        <di:waypoint xsi:type="dc:Point" x="465" y="120" />
        <di:waypoint xsi:type="dc:Point" x="465" y="134" />
        <di:waypoint xsi:type="dc:Point" x="495" y="134" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="480" y="121" width="0" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="EndEvent_11431dt_di" bpmnElement="EndEvent_11431dt">
        <dc:Bounds x="471" y="346" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="489" y="386" width="0" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_0xtdrz5_di" bpmnElement="SequenceFlow_0xtdrz5">
        <di:waypoint xsi:type="dc:Point" x="394" y="306" />
        <di:waypoint xsi:type="dc:Point" x="433" y="306" />
        <di:waypoint xsi:type="dc:Point" x="433" y="364" />
        <di:waypoint xsi:type="dc:Point" x="471" y="364" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="448" y="329" width="0" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_12digm0_di" bpmnElement="SequenceFlow_12digm0">
        <di:waypoint xsi:type="dc:Point" x="545" y="174" />
        <di:waypoint xsi:type="dc:Point" x="545" y="260" />
        <di:waypoint xsi:type="dc:Point" x="489" y="260" />
        <di:waypoint xsi:type="dc:Point" x="489" y="346" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="517" y="239" width="0" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="UserTask_15n4esu_di" bpmnElement="task2">
        <dc:Bounds x="495" y="94" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="UserTask_0q3p8hg_di" bpmnElement="task3">
        <dc:Bounds x="294" y="266" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="UserTask_17boswh_di" bpmnElement="task1">
        <dc:Bounds x="235" y="80" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_0gm7v19_di" bpmnElement="SequenceFlow_0gm7v19">
        <di:waypoint xsi:type="dc:Point" x="335" y="120" />
        <di:waypoint xsi:type="dc:Point" x="385" y="120" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="315" y="99" width="90" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>

我有这个球衣资源:

@Path("process")
public class Process {

    private static ProcessEngine engine = ProcessEngineManager.getEngine();
    private static ProcessInstance instance;
    /**
     * Method handling HTTP GET requests. The returned object will be sent
     * to the client as "text/plain" media type.
     *
     */
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Path("start")
    public void start(){
        instance = engine.getRuntimeService().startProcessInstanceByKey("process1");
    }

    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Path("next")
    public String next(@FormParam("name") String name){
        engine.getRuntimeService().setVariable(instance.getId(),"name",name);
        Map<String,Object> formParams = new HashMap<>();
        formParams.put("name",name);
        Task testTask = engine.getTaskService().createTaskQuery()
                .list().get(0);
        engine.getFormService().submitTaskForm(testTask.getId(),formParams);
        return testTask.getName();
    }
}

和ProcessEngineManager:

public class ProcessEngineManager {
    private static ProcessEngine engine;
    public static ProcessEngine getEngine(){
        if(engine==null) {
            engine = ProcessEngines.getDefaultProcessEngine();
            engine.getRepositoryService().createDeployment()
                    .addClasspathResource("workflowtest/test1.bpmn")
                    .deploy();
        }
        return engine;
    }
}

和camunda.cfg.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="processEngineConfiguration" class="org.camunda.bpm.engine.impl.cfg.StandaloneProcessEngineConfiguration">

        <property name="jdbcUrl" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
        <property name="jdbcDriver" value="org.h2.Driver" />
        <property name="jdbcUsername" value="sa" />
        <property name="jdbcPassword" value="" />

        <property name="databaseSchemaUpdate" value="true" />
    </bean>

</beans>

问题在于,当我提出启动请求然后请求下一个名称为&#34; amer&#34;时,此行:

engine.getRuntimeService().setVariable(instance.getId(),"name",name);

不会将变量设置为instance.getVariables验证的变量,这会导致submitTaskForm行的异常,如下所示:

  

org.camunda.bpm.engine.ProcessEngineException:ENGINE-02004否   具有id&#39; gateway1&#39;的元素的传出序列流可能   选择继续这个过程。

我尝试了setVariableLocal,结果相同。

为什么不设置变量?我该怎么做才能解决问题。

1 个答案:

答案 0 :(得分:0)

事实证明,我以错误的方式调用REST API,导致name为空,现在效果很好。