提问者:小点点

如何以编程方式删除/挂起Activiti中的进程实例?


我创建了流程引擎、存储库服务、运行时服务并部署了BPMN工作流。我还创建了一个Process实例并能够启动它。问题是,当我尝试使用“deleteProcessInstance”方法挂起或删除流程实例时,我会收到一个错误消息,说找不到ProcessInstanceId!!PS:我在某个地方了解到BPMN中的process Id在Activiti中被视为Process Key。

代码:

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

在上面的代码中,在启动流程实例时,我传递了BPMN. xml文件的“流程ID”。(这意味着BPMN.xml的进程ID=Activiti的进程InstanceKey)

我的BPM. 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个答案

匿名用户

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

runtimeService.deleteProcessInstance(processInstanceId, null);

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

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