Remove Incidents From Camunda Cockpit
Wondering how to remove incidents from Camunda Cockpit? Read this tutorial to learn how to do just that.
Join the DZone community and get the full member experience.
Join For FreeToday, we are learning how to remove incidents from Camunda Cockpit.
- Create a Spring Boot project with the embedded Camunda Engine. Use version 7.15.0.
Other Requirements:
- Spring Boot 2.4.3
- Camunda 7.15.0
- Eclipse or any other IDE
- Maven
Steps
- Install required dependencies.
- Now create one simple service task in Camunda Modeler and add the Java Class as Delegate Expression.
- Use Camunda Modeler to save the below XML content with .bpmn extension to get the process model, which has service tasks as components.
<?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:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_17f2lxv" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.5.0">
<bpmn:collaboration id="Collaboration_07k12v5">
<bpmn:participant id="Participant_1x8s9fh" name="Start Process" processRef="StartProcess" />
</bpmn:collaboration>
<bpmn:process id="StartProcess" isExecutable="true">
<bpmn:startEvent id="StartEvent_1" name="start">
<bpmn:outgoing>Flow_1jywq5w</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:endEvent id="Event_03t38ul" name="end">
<bpmn:incoming>Flow_1wvoxke</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_1jywq5w" sourceRef="StartEvent_1" targetRef="Activity_0oy4xym" />
<bpmn:serviceTask id="Activity_0oy4xym" name="Incidents Class" camunda:asyncBefore="true" camunda:delegateExpression="#{checkIncidentsDelegate}">
<bpmn:incoming>Flow_1jywq5w</bpmn:incoming>
<bpmn:outgoing>Flow_1wvoxke</bpmn:outgoing>
</bpmn:serviceTask>
<bpmn:sequenceFlow id="Flow_1wvoxke" sourceRef="Activity_0oy4xym" targetRef="Event_03t38ul" />
<bpmn:endEvent id="Event_07h1zeh">
<bpmn:incoming>Flow_1a8mncl</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_1a8mncl" sourceRef="Event_00wg3ai" targetRef="Event_07h1zeh" />
<bpmn:boundaryEvent id="Event_00wg3ai" attachedToRef="Activity_0oy4xym">
<bpmn:outgoing>Flow_1a8mncl</bpmn:outgoing>
<bpmn:errorEventDefinition id="ErrorEventDefinition_0oqk8gt" errorRef="Error_0jgkptt" />
</bpmn:boundaryEvent>
</bpmn:process>
<bpmn:error id="Error_0jgkptt" name="ERROR_CATCH" errorCode="ERROR_CATCH" />
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Collaboration_07k12v5">
<bpmndi:BPMNShape id="Participant_1x8s9fh_di" bpmnElement="Participant_1x8s9fh" isHorizontal="true">
<dc:Bounds x="129" y="60" width="600" height="290" />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="Flow_1jywq5w_di" bpmnElement="Flow_1jywq5w">
<di:waypoint x="268" y="190" />
<di:waypoint x="330" y="190" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_1wvoxke_di" bpmnElement="Flow_1wvoxke">
<di:waypoint x="430" y="190" />
<di:waypoint x="532" y="190" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_1a8mncl_di" bpmnElement="Flow_1a8mncl">
<di:waypoint x="390" y="248" />
<di:waypoint x="390" y="310" />
<di:waypoint x="462" y="310" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
<dc:Bounds x="232" y="172" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="239" y="215" width="23" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_03t38ul_di" bpmnElement="Event_03t38ul">
<dc:Bounds x="532" y="172" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="541" y="215" width="19" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_07zt6e5_di" bpmnElement="Activity_0oy4xym">
<dc:Bounds x="330" y="150" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_07h1zeh_di" bpmnElement="Event_07h1zeh">
<dc:Bounds x="462" y="292" width="36" height="36" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_0z0tiw2_di" bpmnElement="Event_00wg3ai">
<dc:Bounds x="372" y="212" width="36" height="36" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
4. Now, try to create one exception inside the Java Class so it will produce an incident in Camunda Cockpit. Create one more BPMN file which contains the time scheduler as in the below XML file. Here, I am creating a time scheduler to remove incidents that are created.
<?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: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" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_0tk5ewe" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.5.0">
<bpmn:collaboration id="Collaboration_0140ryf">
<bpmn:participant id="Participant_1dewiyn" name="DeleteIncidents" processRef="DeleteIncidents" />
</bpmn:collaboration>
<bpmn:process id="DeleteIncidents" name="Delete incident" isExecutable="true">
<bpmn:startEvent id="StartEvent_1">
<bpmn:outgoing>Flow_1g7a7p4</bpmn:outgoing>
<bpmn:timerEventDefinition id="TimerEventDefinition_1q5huzh">
<bpmn:timeCycle xsi:type="bpmn:tFormalExpression">R/PT2M</bpmn:timeCycle>
</bpmn:timerEventDefinition>
</bpmn:startEvent>
<bpmn:sequenceFlow id="Flow_1g7a7p4" sourceRef="StartEvent_1" targetRef="Activity_0beg4h9" />
<bpmn:serviceTask id="Activity_0beg4h9" name="Delete incident" camunda:asyncBefore="true" camunda:delegateExpression="#{incidentScheduler}">
<bpmn:incoming>Flow_1g7a7p4</bpmn:incoming>
</bpmn:serviceTask>
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Collaboration_0140ryf">
<bpmndi:BPMNShape id="Participant_1dewiyn_di" bpmnElement="Participant_1dewiyn" isHorizontal="true">
<dc:Bounds x="160" y="52" width="600" height="250" />
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="Flow_1g7a7p4_di" bpmnElement="Flow_1g7a7p4">
<di:waypoint x="285" y="177" />
<di:waypoint x="360" y="177" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="Event_0u4cb2o_di" bpmnElement="StartEvent_1">
<dc:Bounds x="249" y="159" width="36" height="36" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_124pwoy_di" bpmnElement="Activity_0beg4h9">
<dc:Bounds x="360" y="137" width="100" height="80" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
These BPMN files contain a timer that will clear the incidents based on the cycle which we have given. In this BPMN file, the timer definition is given like R/PT2M. It means it will repeat infinite times with 2 minutes of duration. For a service task, the Java code is given as below:
try {
IncidentQuery query = execution.getProcessEngine().getRuntimeService().createIncidentQuery().processDefinitionKeyIn("StartProcess");
List<Incident> incidents = query.list();
for (Incident incident : incidents) {
String incidentProcessId = incident.getProcessInstanceId();
execution.getProcessEngine().getRuntimeService().deleteProcessInstance(incidentProcessId, "Deleting unattached token");
}
} catch (Exception e) {
System.out.println("Exception in IncidentScheduler :: "+e.getLocalizedMessage());
}
These codes will take the incidents from the particular process definition which we have given and clear the incidents.
Opinions expressed by DZone contributors are their own.
Comments