--- /dev/null
+/* Copyright (c) 2019 AT&T Intellectual Property. #\r
+# #\r
+# Licensed under the Apache License, Version 2.0 (the "License"); #\r
+# you may not use this file except in compliance with the License. #\r
+# You may obtain a copy of the License at #\r
+# #\r
+# http://www.apache.org/licenses/LICENSE-2.0 #\r
+# #\r
+# Unless required by applicable law or agreed to in writing, software #\r
+# distributed under the License is distributed on an "AS IS" BASIS, #\r
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #\r
+# See the License for the specific language governing permissions and #\r
+# limitations under the License. #\r
+##############################################################################*/\r
+\r
+\r
+package org.oran.otf.camunda.listener;\r
+\r
+import org.oran.otf.camunda.exception.TestExecutionException;\r
+import org.oran.otf.camunda.workflow.utility.WorkflowUtility;\r
+import org.oran.otf.common.utility.Utility;\r
+import org.camunda.bpm.engine.RuntimeService;\r
+import org.camunda.bpm.engine.delegate.DelegateExecution;\r
+import org.camunda.bpm.engine.delegate.ExecutionListener;\r
+import org.camunda.bpm.engine.impl.context.Context;\r
+import org.camunda.bpm.engine.impl.interceptor.Command;\r
+import org.camunda.bpm.engine.impl.interceptor.CommandContext;\r
+import org.camunda.bpm.engine.runtime.ProcessInstance;\r
+import org.camunda.bpm.extension.reactor.bus.CamundaSelector;\r
+import org.camunda.bpm.extension.reactor.spring.listener.ReactorExecutionListener;\r
+import org.camunda.bpm.model.bpmn.instance.Task;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+import org.springframework.beans.factory.annotation.Autowired;\r
+import org.springframework.data.mongodb.core.MongoTemplate;\r
+import org.springframework.stereotype.Component;\r
+\r
+@Component\r
+@CamundaSelector(event = ExecutionListener.EVENTNAME_END)\r
+public class TaskEndEventListener extends ReactorExecutionListener {\r
+\r
+ @Autowired\r
+ WorkflowUtility utility;\r
+\r
+ @Autowired\r
+ MongoTemplate mongoOperation;\r
+\r
+ @Autowired\r
+ RuntimeService runtimeService;\r
+\r
+ private static Logger LOGGER = LoggerFactory.getLogger(TaskEndEventListener.class);\r
+\r
+ @Override\r
+ public void notify(DelegateExecution execution) {\r
+ if(execution.getBpmnModelElementInstance() instanceof Task){\r
+ String processInstanceId = execution.getProcessInstanceId();\r
+ ProcessInstance processInstance;\r
+ try {\r
+ processInstance = checkProcessInstanceStatus(processInstanceId);\r
+ }catch(Exception e){\r
+ throw new TestExecutionException("Error trying to obtain process instance status, error: " + e) ;\r
+ }\r
+ // if process instance not found then terminate the current process\r
+ if(processInstance == null || processInstance.isEnded() || processInstance.isSuspended()){\r
+ String logPrefix = Utility.getLoggerPrefix();\r
+\r
+ LOGGER.info(logPrefix + "Process Instance not found. Terminating current job (thread).");\r
+ Thread.currentThread().interrupt();\r
+ throw new TestExecutionException("Terminated Process Instance: " + processInstanceId + ". Process Instance no longer exists, thread has been forcefully interrupted");\r
+ }\r
+ }\r
+ }\r
+\r
+ private ProcessInstance checkProcessInstanceStatus(String processInstanceId){\r
+ return Context.getProcessEngineConfiguration().getCommandExecutorTxRequiresNew().execute(new Command<ProcessInstance>() {\r
+ @Override\r
+ public ProcessInstance execute(CommandContext commandContext){\r
+ return runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();\r
+ }\r
+ });\r
+ }\r
+\r
+}\r