Skip to content

Commit

Permalink
Handle Error in the execution of JavaDelegate
Browse files Browse the repository at this point in the history
  • Loading branch information
lseguin committed Sep 28, 2023
1 parent 6c5b934 commit a52f0fd
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,13 @@ public void execute(DelegateExecution execution) {
}

throw e;
} catch (Throwable t) {
if (processEngineConfiguration.isLoggingSessionEnabled()) {
BpmnLoggingSessionUtil.addErrorLoggingData(LoggingSessionConstants.TYPE_SERVICE_TASK_EXCEPTION,
"Service task with java class " + javaDelegate.getClass().getName() + " threw throwable " + t.getMessage(), t, execution);
}

throw new RuntimeException(t);
}

} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.Callable;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import org.flowable.common.engine.api.FlowableException;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
Expand All @@ -33,6 +34,7 @@
import org.flowable.engine.impl.cfg.ProcessEngineConfigurationImpl;
import org.flowable.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration;
import org.flowable.engine.impl.test.JobTestHelper;
import org.flowable.engine.runtime.ActivityInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.job.api.Job;
import org.flowable.job.api.JobInfo;
Expand Down Expand Up @@ -94,6 +96,47 @@ public void testRegularAsyncExecution() {
}
}

@Test
public void testThrowingErrorInJavaDelegate() throws InterruptedException {

ProcessEngine processEngine = null;
try {
// Deploy
processEngine = createProcessEngine(true);
processEngine.getProcessEngineConfiguration().getClock().reset();
deploy(processEngine, "AsyncExecutorTest.JobErrorCheck.bpmn20.xml");

processEngine.getProcessEngineConfiguration().setAsyncFailedJobWaitTime(1);
processEngine.getProcessEngineConfiguration().setDefaultFailedJobWaitTime(1);
processEngine.getProcessEngineConfiguration()
.getAsyncExecutor()
.getJobServiceConfiguration()
.setAsyncExecutorNumberOfRetries(1);

ProcessInstance processWithError = processEngine.getRuntimeService().startProcessInstanceByKey("JobErrorCheck");

assertThat(processWithError).isNotNull();

final ProcessEngine processEngineCopy = processEngine;
JobTestHelper.waitForJobExecutorOnCondition(processEngine.getProcessEngineConfiguration(), 10000L, 1000L, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return processEngineCopy.getManagementService().createDeadLetterJobQuery().count() == 1;
}
});

assertThat(processEngine.getRuntimeService().createActivityInstanceQuery()
.processInstanceId(processWithError.getId()).activityId("servicetask1").singleResult()).isNull();
} finally {

// Clean up
if (processEngine != null) {
cleanup(processEngine);
}
}

}

@Test
public void testAsyncExecutionForStraightThroughParallelMultiInstance() {

Expand Down Expand Up @@ -475,4 +518,11 @@ public String getOnTransaction() {
}
}

public static class TestErrorJavaDelegate implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) {
throw new Error();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" targetNamespace="http://www.activiti.org/test" exporter="bpmn-js (https://demo.bpmn.io)" exporterVersion="14.0.0">
<process id="JobErrorCheck" name="Job Error Check" isExecutable="true">
<startEvent id="startevent1" name="Start">
<outgoing>Flow_1ygz058</outgoing>
</startEvent>
<endEvent id="endevent1" name="End" />
<serviceTask id="servicetask1" name="Service Task" activiti:async="true" activiti:class="org.flowable.engine.test.jobexecutor.AsyncExecutorTest$TestErrorJavaDelegate">
<incoming>Flow_1ygz058</incoming>
</serviceTask>
<sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="endevent1" />
<sequenceFlow id="Flow_1ygz058" sourceRef="startevent1" targetRef="servicetask1" />
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_JobErrorCheck">
<bpmndi:BPMNPlane id="BPMNPlane_JobErrorCheck" bpmnElement="JobErrorCheck">
<bpmndi:BPMNShape id="BPMNShape_startevent1" bpmnElement="startevent1">
<omgdc:Bounds x="150" y="90" width="35" height="35" />
<bpmndi:BPMNLabel>
<omgdc:Bounds x="156" y="125" width="24" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="BPMNShape_servicetask1" bpmnElement="servicetask1">
<omgdc:Bounds x="327" y="80" width="105" height="55" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="BPMNShape_endevent1" bpmnElement="endevent1">
<omgdc:Bounds x="502" y="90" width="35" height="35" />
<bpmndi:BPMNLabel>
<omgdc:Bounds x="510" y="125" width="20" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="BPMNEdge_flow2" bpmnElement="flow2">
<omgdi:waypoint x="432" y="107" />
<omgdi:waypoint x="502" y="107" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_1ygz058_di" bpmnElement="Flow_1ygz058">
<omgdi:waypoint x="185" y="108" />
<omgdi:waypoint x="327" y="108" />
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>

0 comments on commit a52f0fd

Please sign in to comment.