1 /* Copyright (c) 2019 AT&T Intellectual Property. #
\r
3 # Licensed under the Apache License, Version 2.0 (the "License"); #
\r
4 # you may not use this file except in compliance with the License. #
\r
5 # You may obtain a copy of the License at #
\r
7 # http://www.apache.org/licenses/LICENSE-2.0 #
\r
9 # Unless required by applicable law or agreed to in writing, software #
\r
10 # distributed under the License is distributed on an "AS IS" BASIS, #
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
\r
12 # See the License for the specific language governing permissions and #
\r
13 # limitations under the License. #
\r
14 ##############################################################################*/
\r
17 package org.oran.otf.camunda.listener;
\r
19 import org.oran.otf.camunda.exception.TestExecutionException;
\r
20 import org.oran.otf.camunda.workflow.utility.WorkflowUtility;
\r
21 import org.oran.otf.common.utility.Utility;
\r
22 import org.camunda.bpm.engine.RuntimeService;
\r
23 import org.camunda.bpm.engine.delegate.DelegateExecution;
\r
24 import org.camunda.bpm.engine.delegate.ExecutionListener;
\r
25 import org.camunda.bpm.engine.impl.context.Context;
\r
26 import org.camunda.bpm.engine.impl.interceptor.Command;
\r
27 import org.camunda.bpm.engine.impl.interceptor.CommandContext;
\r
28 import org.camunda.bpm.engine.runtime.ProcessInstance;
\r
29 import org.camunda.bpm.extension.reactor.bus.CamundaSelector;
\r
30 import org.camunda.bpm.extension.reactor.spring.listener.ReactorExecutionListener;
\r
31 import org.camunda.bpm.model.bpmn.instance.Task;
\r
32 import org.slf4j.Logger;
\r
33 import org.slf4j.LoggerFactory;
\r
34 import org.springframework.beans.factory.annotation.Autowired;
\r
35 import org.springframework.data.mongodb.core.MongoTemplate;
\r
36 import org.springframework.stereotype.Component;
\r
39 @CamundaSelector(event = ExecutionListener.EVENTNAME_END)
\r
40 public class TaskEndEventListener extends ReactorExecutionListener {
\r
43 WorkflowUtility utility;
\r
46 MongoTemplate mongoOperation;
\r
49 RuntimeService runtimeService;
\r
51 private static Logger LOGGER = LoggerFactory.getLogger(TaskEndEventListener.class);
\r
54 public void notify(DelegateExecution execution) {
\r
55 if(execution.getBpmnModelElementInstance() instanceof Task){
\r
56 String processInstanceId = execution.getProcessInstanceId();
\r
57 ProcessInstance processInstance;
\r
59 processInstance = checkProcessInstanceStatus(processInstanceId);
\r
60 }catch(Exception e){
\r
61 throw new TestExecutionException("Error trying to obtain process instance status, error: " + e) ;
\r
63 // if process instance not found then terminate the current process
\r
64 if(processInstance == null || processInstance.isEnded() || processInstance.isSuspended()){
\r
65 String logPrefix = Utility.getLoggerPrefix();
\r
67 LOGGER.info(logPrefix + "Process Instance not found. Terminating current job (thread).");
\r
68 Thread.currentThread().interrupt();
\r
69 throw new TestExecutionException("Terminated Process Instance: " + processInstanceId + ". Process Instance no longer exists, thread has been forcefully interrupted");
\r
74 private ProcessInstance checkProcessInstanceStatus(String processInstanceId){
\r
75 return Context.getProcessEngineConfiguration().getCommandExecutorTxRequiresNew().execute(new Command<ProcessInstance>() {
\r
77 public ProcessInstance execute(CommandContext commandContext){
\r
78 return runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
\r