如何以编程方式删除/暂停Activiti中的流程实例?

时间:2016-03-26 10:38:42

标签: java activiti bpmn

我创建了一个Process Engine,Repository Service,Runtime Service并部署了一个BPMN工作流程。 我还创建了一个Process实例,并且能够启动它。 问题是,当我尝试使用" deleteProcessInstance"暂停或删除流程实例时方法我得到一个错误,说无法找到ProcessInstanceId !! PS:我在某处了解到BPMN中的processId在Activiti中被视为processKey。

代码:

public void startProcess(String processDefinitionKey)
    {

         ListenerClass.processInstance=   ListenerClass.runtimeService.startProcessInstanceByKey(processDefinitionKey);

    }

    public void deleteProcess(String processInstanceId)
    {


           System.out.println("Stopping Process instance id " +processInstanceId);

           ListenerClass.runtimeService.deleteProcessInstance(processInstanceId, "Ok");



    }

}

在启动流程实例的上面的代码中,我传递了"流程ID" BPMN.xml文件。(表示BPMN.xml的processId = Activiti的processInstanceKey)

我的BPMN.xml:

<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" 
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" 
xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_1" xmlns:activiti="http://activiti.org/bpmn" 
targetNamespace="http://bpmn.io/schema/bpmn">
  <bpmn:process id="Process_11" isExecutable="true" name="Hima">
    <bpmn:startEvent id="StartEvent_1">
      <bpmn:outgoing>SequenceFlow_06sykhd</bpmn:outgoing>
    </bpmn:startEvent>  
    <bpmn:sequenceFlow id="SequenceFlow_06sykhd" sourceRef="StartEvent_1" targetRef="Task_1dt8brv" />
    <bpmn:endEvent id="EndEvent_0cb0ioi">
      <bpmn:incoming>SequenceFlow_1doj4n6</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:serviceTask id="Task_1dt8brv"
             name="My Java Service Task"
             activiti:class="com.bosch.bip.Service.task1" >

      <bpmn:incoming>SequenceFlow_06sykhd</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_156te78</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:serviceTask id="Task_10bfll9" activiti:class="com.bosch.bip.Service.task1">
      <bpmn:incoming>SequenceFlow_156te78</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_1doj4n6</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:sequenceFlow id="SequenceFlow_156te78" sourceRef="Task_1dt8brv" targetRef="Task_10bfll9" />
    <bpmn:sequenceFlow id="SequenceFlow_1doj4n6" sourceRef="Task_10bfll9" targetRef="EndEvent_0cb0ioi" />
  </bpmn:process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_11">
      <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
        <dc:Bounds x="173" y="102" width="36" height="36" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_06sykhd_di" bpmnElement="SequenceFlow_06sykhd">
        <di:waypoint xsi:type="dc:Point" x="209" y="120" />
        <di:waypoint xsi:type="dc:Point" x="264" y="120" />
        <di:waypoint xsi:type="dc:Point" x="264" y="147" />
        <di:waypoint xsi:type="dc:Point" x="318" y="147" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="219" y="123.5" width="90" height="20" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNShape id="EndEvent_0cb0ioi_di" bpmnElement="EndEvent_0cb0ioi">
        <dc:Bounds x="768" y="173" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="741" y="209" width="90" height="20" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Task_1dt8brv_di" bpmnElement="Task_1dt8brv">
        <dc:Bounds x="318" y="107" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="Task_10bfll9_di" bpmnElement="Task_10bfll9">
        <dc:Bounds x="528" y="94" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_156te78_di" bpmnElement="SequenceFlow_156te78">
        <di:waypoint xsi:type="dc:Point" x="418" y="147" />
        <di:waypoint xsi:type="dc:Point" x="473" y="147" />
        <di:waypoint xsi:type="dc:Point" x="473" y="134" />
        <di:waypoint xsi:type="dc:Point" x="528" y="134" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="386" y="202.5" width="90" height="20" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_1doj4n6_di" bpmnElement="SequenceFlow_1doj4n6">
        <di:waypoint xsi:type="dc:Point" x="628" y="134" />
        <di:waypoint xsi:type="dc:Point" x="698" y="134" />
        <di:waypoint xsi:type="dc:Point" x="698" y="191" />
        <di:waypoint xsi:type="dc:Point" x="768" y="191" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="653" y="152.5" width="90" height="20" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>

这方面的任何帮助都会非常有用。

1 个答案:

答案 0 :(得分:3)

对于正在运行的进程,请使用此方法,如上所述:

runtimeService.deleteProcessInstance(processInstanceId, null);

如果流程已经结束,您需要将其从历史记录中删除,也许您的流程已经结束了?

HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
        .processInstanceId(processInstanceId).singleResult();
if (historicProcessInstance != null) {
    historyService.deleteHistoricProcessInstance(historicProcessInstance.getId());
}