Cancel activity execution programatically

Hi there.
I have a timer that is starting a process periodically. But since I’m running Camunda in a cluster and I only want to run this process on a single node so I have a listener for the start event which checks if that particular execution is already being processed by another node and if it is, I want to skip the execution on the current node.
I tried deleting the deleteProcessInstance(...) but I get a NullPointerException,

My code is something like this (simplified. This would terminate every timer execution):

public class CamundaStartEventDelegate implements DelegateListener<ActivityExecution> {

    @Override
    public void notify(ActivityExecution instance) throws Exception {
        if (!startedByTimer(instance)) {
            return;
        }
        processEngine.getRuntimeService().deleteProcessInstance(instance.getProcessInstanceId(), "Ran in another node");
    }

But I get this error:

2017-06-22T17:38:20,856 [ERROR] pool-3-thread-1 [context logError:156] ENGINE-16006 BPMN Stack Trace:                                                                                                 
        null (activity-start, ProcessInstance[13])                                                                                                                                                    
        StartEvent_1i6cslv                                                                                                                                                                            
                                                                                                                                                                                                      
2017-06-22T17:38:20,857 [ERROR] pool-3-thread-1 [context logError:156] ENGINE-16004 Exception while closing command context: null                                                                     
java.lang.NullPointerException: null                                                                                                                                                                  
        at org.camunda.bpm.engine.impl.core.operation.AbstractEventAtomicOperation.getListeners(AbstractEventAtomicOperation.java:78) ~[camunda-engine-7.6.0.jar:7.6.0]                               
        at org.camunda.bpm.engine.impl.core.operation.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:37) ~[camunda-engine-7.6.0.jar:7.6.0]                                    
        at org.camunda.bpm.engine.impl.interceptor.AtomicOperationInvocation.execute(AtomicOperationInvocation.java:89) ~[camunda-engine-7.6.0.jar:7.6.0]                                             
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.invokeNext(CommandInvocationContext.java:125) ~[camunda-engine-7.6.0.jar:7.6.0]                                           
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performNext(CommandInvocationContext.java:104) ~[camunda-engine-7.6.0.jar:7.6.0]                                          
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:79) ~[camunda-engine-7.6.0.jar:7.6.0]                                      
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:69) ~[camunda-engine-7.6.0.jar:7.6.0]                                      
        at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:622) ~[camunda-engine-7.6.0.jar:7.6.0]                                            
        at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:597) ~[camunda-engine-7.6.0.jar:7.6.0]                                            
        at org.camunda.bpm.engine.impl.core.operation.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:58) ~[camunda-engine-7.6.0.jar:7.6.0]                                    
        at org.camunda.bpm.engine.impl.interceptor.AtomicOperationInvocation.execute(AtomicOperationInvocation.java:89) ~[camunda-engine-7.6.0.jar:7.6.0]                                             
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.invokeNext(CommandInvocationContext.java:125) ~[camunda-engine-7.6.0.jar:7.6.0]                                           
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performNext(CommandInvocationContext.java:104) ~[camunda-engine-7.6.0.jar:7.6.0]                                          
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:79) ~[camunda-engine-7.6.0.jar:7.6.0]                                      
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:69) ~[camunda-engine-7.6.0.jar:7.6.0]                                      
        at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:622) ~[camunda-engine-7.6.0.jar:7.6.0]                                            
        at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:597) ~[camunda-engine-7.6.0.jar:7.6.0]                                            
        at org.camunda.bpm.engine.impl.core.operation.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:58) ~[camunda-engine-7.6.0.jar:7.6.0]                                    
        at org.camunda.bpm.engine.impl.interceptor.AtomicOperationInvocation.execute(AtomicOperationInvocation.java:89) ~[camunda-engine-7.6.0.jar:7.6.0]                                             
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.invokeNext(CommandInvocationContext.java:125) ~[camunda-engine-7.6.0.jar:7.6.0]                                           
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performNext(CommandInvocationContext.java:104) ~[camunda-engine-7.6.0.jar:7.6.0]                                          
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:79) ~[camunda-engine-7.6.0.jar:7.6.0]                                      
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:69) ~[camunda-engine-7.6.0.jar:7.6.0]                                      
        at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:622) ~[camunda-engine-7.6.0.jar:7.6.0]                                            
        at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperationSync(ExecutionEntity.java:597) ~[camunda-engine-7.6.0.jar:7.6.0]                                            
        at org.camunda.bpm.engine.impl.core.operation.AbstractEventAtomicOperation.execute(AbstractEventAtomicOperation.java:58) ~[camunda-engine-7.6.0.jar:7.6.0]                                    
        at org.camunda.bpm.engine.impl.interceptor.AtomicOperationInvocation.execute(AtomicOperationInvocation.java:89) ~[camunda-engine-7.6.0.jar:7.6.0]                                             
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.invokeNext(CommandInvocationContext.java:125) ~[camunda-engine-7.6.0.jar:7.6.0]                                           
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performNext(CommandInvocationContext.java:104) ~[camunda-engine-7.6.0.jar:7.6.0]                                          
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:79) ~[camunda-engine-7.6.0.jar:7.6.0]                                      
        at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:611) ~[camunda-engine-7.6.0.jar:7.6.0]                                                
        at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:587) ~[camunda-engine-7.6.0.jar:7.6.0]                                                
        at org.camunda.bpm.engine.impl.pvm.runtime.operation.PvmAtomicOperationActivityStartCreateScope.scopeCreated(PvmAtomicOperationActivityStartCreateScope.java:39) ~[camunda-engine-7.6.0.jar:7.
6.0]                                                                                                                                                                                                  
        at org.camunda.bpm.engine.impl.pvm.runtime.operation.PvmAtomicOperationCreateScope.execute(PvmAtomicOperationCreateScope.java:50) ~[camunda-engine-7.6.0.jar:7.6.0]                           
        at org.camunda.bpm.engine.impl.pvm.runtime.operation.PvmAtomicOperationCreateScope.execute(PvmAtomicOperationCreateScope.java:24) ~[camunda-engine-7.6.0.jar:7.6.0]                           
        at org.camunda.bpm.engine.impl.interceptor.AtomicOperationInvocation.execute(AtomicOperationInvocation.java:89) ~[camunda-engine-7.6.0.jar:7.6.0]                                             
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.invokeNext(CommandInvocationContext.java:125) ~[camunda-engine-7.6.0.jar:7.6.0]                                           
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performNext(CommandInvocationContext.java:112) ~[camunda-engine-7.6.0.jar:7.6.0]                                          
        at org.camunda.bpm.engine.impl.interceptor.CommandInvocationContext.performOperation(CommandInvocationContext.java:79) ~[camunda-engine-7.6.0.jar:7.6.0]                                      
        at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:611) ~[camunda-engine-7.6.0.jar:7.6.0]                                                
        at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.performOperation(ExecutionEntity.java:587) ~[camunda-engine-7.6.0.jar:7.6.0]                                                
        at org.camunda.bpm.engine.impl.pvm.runtime.PvmExecutionImpl.start(PvmExecutionImpl.java:231) ~[camunda-engine-7.6.0.jar:7.6.0]                                                                
        at org.camunda.bpm.engine.impl.persistence.entity.ExecutionEntity.start(ExecutionEntity.java:432) ~[camunda-engine-7.6.0.jar:7.6.0]                                                           
        at org.camunda.bpm.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:55) ~[camunda-engine-7.6.0.jar:7.6.0]                                                         
        at org.camunda.bpm.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:31) ~[camunda-engine-7.6.0.jar:7.6.0]                                                         
        at org.camunda.bpm.engine.impl.interceptor.CommandExecutorImpl.execute(CommandExecutorImpl.java:24) ~[camunda-engine-7.6.0.jar:7.6.0]                                                         
        at org.camunda.bpm.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:104) ~[camunda-engine-7.6.0.jar:7.6.0]                                            
        at org.camunda.bpm.engine.impl.interceptor.ProcessApplicationContextInterceptor.execute(ProcessApplicationContextInterceptor.java:66) ~[camunda-engine-7.6.0.jar:7.6.0]                       
        at org.camunda.bpm.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:30) ~[camunda-engine-7.6.0.jar:7.6.0]                                                                   
        at org.camunda.bpm.engine.impl.ProcessInstantiationBuilderImpl.executeWithVariablesInReturn(ProcessInstantiationBuilderImpl.java:156) ~[camunda-engine-7.6.0.jar:7.6.0]                       
        at org.camunda.bpm.engine.impl.ProcessInstantiationBuilderImpl.execute(ProcessInstantiationBuilderImpl.java:122) ~[camunda-engine-7.6.0.jar:7.6.0]                                            
        at org.camunda.bpm.engine.impl.ProcessInstantiationBuilderImpl.execute(ProcessInstantiationBuilderImpl.java:118) ~[camunda-engine-7.6.0.jar:7.6.0]                                            
        at org.camunda.bpm.engine.impl.jobexecutor.TimerStartEventJobHandler.startProcessInstance(TimerStartEventJobHandler.java:54) ~[camunda-engine-7.6.0.jar:7.6.0]                                
        at org.camunda.bpm.engine.impl.jobexecutor.TimerStartEventJobHandler.execute(TimerStartEventJobHandler.java:43) ~[camunda-engine-7.6.0.jar:7.6.0]                                             
        at org.camunda.bpm.engine.impl.jobexecutor.TimerStartEventJobHandler.execute(TimerStartEventJobHandler.java:24) ~[camunda-engine-7.6.0.jar:7.6.0]                                             
        at org.camunda.bpm.engine.impl.persistence.entity.JobEntity.execute(JobEntity.java:129) ~[camunda-engine-7.6.0.jar:7.6.0]                                                                     
        at org.camunda.bpm.engine.impl.cmd.ExecuteJobsCmd.execute(ExecuteJobsCmd.java:99) ~[camunda-engine-7.6.0.jar:7.6.0]                                                                           
        at org.camunda.bpm.engine.impl.cmd.ExecuteJobsCmd.execute(ExecuteJobsCmd.java:36) ~[camunda-engine-7.6.0.jar:7.6.0]                                                                           
        at org.camunda.bpm.engine.impl.interceptor.CommandExecutorImpl.execute(CommandExecutorImpl.java:24) ~[camunda-engine-7.6.0.jar:7.6.0]                                                         
        at org.camunda.bpm.engine.impl.interceptor.CommandContextInterceptor.execute(CommandContextInterceptor.java:104) [camunda-engine-7.6.0.jar:7.6.0]                                             
        at org.camunda.bpm.engine.impl.interceptor.ProcessApplicationContextInterceptor.execute(ProcessApplicationContextInterceptor.java:66) [camunda-engine-7.6.0.jar:7.6.0]                        
        at org.camunda.bpm.engine.impl.interceptor.LogInterceptor.execute(LogInterceptor.java:30) [camunda-engine-7.6.0.jar:7.6.0]                                                                    
        at org.camunda.bpm.engine.impl.jobexecutor.ExecuteJobHelper.executeJob(ExecuteJobHelper.java:35) [camunda-engine-7.6.0.jar:7.6.0]                                                             
        at org.camunda.bpm.engine.impl.jobexecutor.ExecuteJobHelper.executeJob(ExecuteJobHelper.java:28) [camunda-engine-7.6.0.jar:7.6.0]                                                             
        at org.camunda.bpm.engine.impl.jobexecutor.ExecuteJobsRunnable.executeJob(ExecuteJobsRunnable.java:82) [camunda-engine-7.6.0.jar:7.6.0]                                                       
        at org.camunda.bpm.engine.impl.jobexecutor.ExecuteJobsRunnable.run(ExecuteJobsRunnable.java:56) [camunda-engine-7.6.0.jar:7.6.0]                                                              
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_121]                                                                                              
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_121]                                                                                              
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121]                                                                                                                                        

Does anybody have any idea why I can’t do this?

Hello @JohnT,

Which version of Camunda BPM do you use?

Best regards,
Yana

I’m using version 7.6.0.

Hi,

I want to verify that I understand the scenario: You have a cluster and this process definition is deployed on every node, right? There is a listener for the start event which checks if that particular execution is already being processed by another node and if it is, you trigger the deleteProcessInstance. And sometimes or each time it fails with NPE?
Also it would be great if you provide a bpmn example so we can reproduce the issue.

Best regards,
Yana

Hello!
You got the scenario right. This fails every time with NPE.
Just to make this scenario easier to test, I’m now on a single node trying to delete every execution started by the timer, which gets me the same results.
I’ll try to provide a bpmn later today.

Many thanks

Hello. I’m sorry for the delay. This is the BPMN I’m using:

<?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" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn">
  <bpmn:collaboration id="Collaboration_0m7sldo">
    <bpmn:participant id="Participant_1hl0h5a" name="PizzaLounge" processRef="Process_1" />
  </bpmn:collaboration>
  <bpmn:process id="Process_1" isExecutable="true">
    <bpmn:startEvent id="StartEvent_1tx0pno" name="I want Pizza">
      <bpmn:extensionElements>
        <camunda:properties>
          <camunda:property name="COM.COMPANY.BOT.WF.ENGAGINGPATTERN" value="INTENT.IWANTPIZZA" />
        </camunda:properties>
      </bpmn:extensionElements>
      <bpmn:outgoing>SequenceFlow_1f9j79u</bpmn:outgoing>
      <bpmn:messageEventDefinition messageRef="Message_1fyege1" />
    </bpmn:startEvent>
    <bpmn:startEvent id="StartEvent_1i6cslv" name="Run every minute">
      <bpmn:outgoing>SequenceFlow_08u7u71</bpmn:outgoing>
      <bpmn:timerEventDefinition>
        <bpmn:timeCycle xsi:type="bpmn:tFormalExpression">0 * * * * ?</bpmn:timeCycle>
      </bpmn:timerEventDefinition>
    </bpmn:startEvent>
    <bpmn:endEvent id="EndEvent_1jbvbt3" name="END">
      <bpmn:incoming>SequenceFlow_1lp0voc</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:sendTask id="Task_19vm1nz" name="SAY OK">
      <bpmn:extensionElements>
        <camunda:properties>
          <camunda:property name="COM.COMPANY.BOT.WF.RESPONSETYPE" value="TEXT" />
          <camunda:property name="COM.COMPANY.BOT.WF.SENDTYPE" value="PATTERN" />
          <camunda:property name="COM.COMPANY.BOT.WF.SENDVALUE" value="PATTERN.SAYOKTXS" />
          <camunda:property name="COM.COMPANY.BOT.WF.PROPERTY" />
          <camunda:property name="COM.COMPANY.BOT.WF.USERS" />
        </camunda:properties>
      </bpmn:extensionElements>
      <bpmn:incoming>SequenceFlow_1f9j79u</bpmn:incoming>
      <bpmn:incoming>SequenceFlow_08u7u71</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_1lp0voc</bpmn:outgoing>
    </bpmn:sendTask>
    <bpmn:sequenceFlow id="SequenceFlow_1f9j79u" sourceRef="StartEvent_1tx0pno" targetRef="Task_19vm1nz" />
    <bpmn:sequenceFlow id="SequenceFlow_08u7u71" sourceRef="StartEvent_1i6cslv" targetRef="Task_19vm1nz" />
    <bpmn:sequenceFlow id="SequenceFlow_1lp0voc" sourceRef="Task_19vm1nz" targetRef="EndEvent_1jbvbt3" />
  </bpmn:process>
  <bpmn:message id="Message_1fyege1" name="Message_2dnlgbs" />
  <bpmn:message id="Message_1ncw7s7" name="Message_2ak983d" />
  <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Collaboration_0m7sldo">
      <bpmndi:BPMNShape id="Participant_1hl0h5a_di" bpmnElement="Participant_1hl0h5a">
        <dc:Bounds x="507" y="236" width="1298" height="463" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="EndEvent_1jbvbt3_di" bpmnElement="EndEvent_1jbvbt3">
        <dc:Bounds x="769" y="587" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="777" y="627" width="23" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="StartEvent_0r6xzvi_di" bpmnElement="StartEvent_1tx0pno">
        <dc:Bounds x="570" y="363" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="559" y="403" width="59" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="StartEvent_0au1nyx_di" bpmnElement="StartEvent_1i6cslv">
        <dc:Bounds x="570" y="278" width="36" height="36" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="550" y="318" width="77" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape id="SendTask_0xvrg0l_di" bpmnElement="Task_19vm1nz">
        <dc:Bounds x="737" y="425" width="100" height="80" />
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge id="SequenceFlow_1f9j79u_di" bpmnElement="SequenceFlow_1f9j79u">
        <di:waypoint xsi:type="dc:Point" x="606" y="381" />
        <di:waypoint xsi:type="dc:Point" x="672" y="381" />
        <di:waypoint xsi:type="dc:Point" x="672" y="465" />
        <di:waypoint xsi:type="dc:Point" x="737" y="465" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="687" y="417" width="0" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_08u7u71_di" bpmnElement="SequenceFlow_08u7u71">
        <di:waypoint xsi:type="dc:Point" x="606" y="296" />
        <di:waypoint xsi:type="dc:Point" x="672" y="296" />
        <di:waypoint xsi:type="dc:Point" x="672" y="465" />
        <di:waypoint xsi:type="dc:Point" x="737" y="465" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="687" y="374.5" width="0" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge id="SequenceFlow_1lp0voc_di" bpmnElement="SequenceFlow_1lp0voc">
        <di:waypoint xsi:type="dc:Point" x="787" y="505" />
        <di:waypoint xsi:type="dc:Point" x="787" y="587" />
        <bpmndi:BPMNLabel>
          <dc:Bounds x="802" y="540" width="0" height="12" />
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</bpmn:definitions>

Hello @JohnT,

You receive the exception because you are trying to delete the process instance inside of it. I would suggest a different approach for your case. For example you can deploy the process definition to only one of the nodes. That way you will not need to check if the execution is already being process by another node.

Best regards,
Yana

That is not an option for me. The process definition will have to be deployed to every node on startup.
So that’s a limitation of Camunda? I cannot abort the process instance from inside of it? Isn’t there any way of doing it? For instance if I throw an exception from inside the listener the execution seems to stop for a while but then the same execution will start again. Can’t I just disable that restart, for instance?