Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete/suspend a process instance in Activiti programmatically?

I created a Process Engine, Repository Service, Runtime Service and deployed a BPMN workflow. I also created a Process instance and was able to start it. The issue is that when I try to either suspend or delete the process instance using the "deleteProcessInstance" method I get an error saying that the ProcessInstanceId could not be found!! PS: I learnt somewhere that the processId in BPMN is treated as processKey in Activiti.

Code:

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

In the code above while starting the process instance, I pass the "Process Id" of the BPMN.xml file.(which means processId of BPMN.xml=processInstanceKey of Activiti)

My 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>

Any help in this regards would be really helpful.

like image 799
Himangshu Chowdhary Avatar asked Dec 19 '25 19:12

Himangshu Chowdhary


1 Answers

For a running process use this, as you did above:

runtimeService.deleteProcessInstance(processInstanceId, null);

If the process has been ended you need to delete it from the history, maybe your process has already ended?

HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
        .processInstanceId(processInstanceId).singleResult();
if (historicProcessInstance != null) {
    historyService.deleteHistoricProcessInstance(historicProcessInstance.getId());
}
like image 143
flavio.donze Avatar answered Dec 22 '25 07:12

flavio.donze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!