import java.util.concurrent.DelayQueue;
import java.util.concurrent.TimeUnit;
-
import org.onap.so.logger.ErrorCode;
import org.onap.so.logger.MessageEnum;
import org.slf4j.Logger;
/**
* Workflow Context Holder instance which can be accessed elsewhere either in groovy scripts or Java
+ *
* @version 1.0
*
*/
@Component
public class WorkflowContextHolder {
- private static Logger logger = LoggerFactory.getLogger(WorkflowContextHolder.class);
- private static final String logMarker = "[WORKFLOW-CONTEXT-HOLDER]";
- private static WorkflowContextHolder instance = null;
-
-
- private long defaultContextTimeout=60000;
-
- /**
- * Delay Queue which holds workflow context holder objects
- */
- private final DelayQueue<WorkflowContext> responseQueue = new DelayQueue<>();
- private final TimeoutThread timeoutThread = new TimeoutThread();
-
- private WorkflowContextHolder() {
- timeoutThread.start();
- }
-
- /**
- * Singleton holder which eliminates hot lock
- * Since the JVM synchronizes static method there is no synchronization needed for this method
- * @return
- */
- public static synchronized WorkflowContextHolder getInstance() {
- if (instance == null) {
- instance = new WorkflowContextHolder();
- }
- return instance;
- }
-
- public void put(WorkflowContext context) {
- logger.debug("{} Adding context to the queue: {}", logMarker, context.getRequestId());
- responseQueue.put(context);
- }
-
- public void remove(WorkflowContext context) {
- logger.debug("{} Removing context from the queue: {}", logMarker, context.getRequestId());
- responseQueue.remove(context);
- }
-
- public WorkflowContext getWorkflowContext(String requestId) {
- // Note: DelayQueue interator is threadsafe
- for (WorkflowContext context : responseQueue) {
- if (requestId.equals(context.getRequestId())) {
- return context;
- }
- }
- return null;
- }
-
- /**
- * Builds the callback response object to respond to client
- * @param processKey
- * @param processInstanceId
- * @param requestId
- * @param callbackResponse
- * @return
- */
- public void processCallback(String processKey, String processInstanceId,
- String requestId, WorkflowCallbackResponse callbackResponse) {
- WorkflowResponse workflowResponse = new WorkflowResponse();
- workflowResponse.setResponse(callbackResponse.getResponse());
- workflowResponse.setProcessInstanceID(processInstanceId);
- workflowResponse.setMessageCode(callbackResponse.getStatusCode());
- workflowResponse.setMessage(callbackResponse.getMessage());
- WorkflowContext context = new WorkflowContext(processKey, requestId, defaultContextTimeout,workflowResponse);
- put(context);
- }
-
-
- /**
- * Timeout thread which monitors the delay queue for expired context and send timeout response
- * to client
- *
- * */
- private class TimeoutThread extends Thread {
- @Override
- public void run() {
- while (!isInterrupted()) {
- try {
- WorkflowContext requestObject = responseQueue.take();
- logger.debug("Time remaining for request id: {}:{}", requestObject.getRequestId(), requestObject
- .getDelay
- (TimeUnit.MILLISECONDS));
- logger.debug("Preparing timeout response for {}:{}", requestObject.getProcessKey(), requestObject
- .getRequestId());
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- } catch (Exception e) {
- logger.debug("WorkflowContextHolder timeout thread caught exception: ", e);
- logger.error("{} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION.toString(), "BPMN",
- ErrorCode.UnknownError.getValue(), "Error in WorkflowContextHolder timeout thread");
- }
- }
- logger.debug("WorkflowContextHolder timeout thread interrupted, quitting");
- }
- }
+ private static Logger logger = LoggerFactory.getLogger(WorkflowContextHolder.class);
+ private static final String logMarker = "[WORKFLOW-CONTEXT-HOLDER]";
+ private static WorkflowContextHolder instance = null;
+
+
+ private long defaultContextTimeout = 60000;
+
+ /**
+ * Delay Queue which holds workflow context holder objects
+ */
+ private final DelayQueue<WorkflowContext> responseQueue = new DelayQueue<>();
+ private final TimeoutThread timeoutThread = new TimeoutThread();
+
+ private WorkflowContextHolder() {
+ timeoutThread.start();
+ }
+
+ /**
+ * Singleton holder which eliminates hot lock Since the JVM synchronizes static method there is no synchronization
+ * needed for this method
+ *
+ * @return
+ */
+ public static synchronized WorkflowContextHolder getInstance() {
+ if (instance == null) {
+ instance = new WorkflowContextHolder();
+ }
+ return instance;
+ }
+
+ public void put(WorkflowContext context) {
+ logger.debug("{} Adding context to the queue: {}", logMarker, context.getRequestId());
+ responseQueue.put(context);
+ }
+
+ public void remove(WorkflowContext context) {
+ logger.debug("{} Removing context from the queue: {}", logMarker, context.getRequestId());
+ responseQueue.remove(context);
+ }
+
+ public WorkflowContext getWorkflowContext(String requestId) {
+ // Note: DelayQueue interator is threadsafe
+ for (WorkflowContext context : responseQueue) {
+ if (requestId.equals(context.getRequestId())) {
+ return context;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Builds the callback response object to respond to client
+ *
+ * @param processKey
+ * @param processInstanceId
+ * @param requestId
+ * @param callbackResponse
+ * @return
+ */
+ public void processCallback(String processKey, String processInstanceId, String requestId,
+ WorkflowCallbackResponse callbackResponse) {
+ WorkflowResponse workflowResponse = new WorkflowResponse();
+ workflowResponse.setResponse(callbackResponse.getResponse());
+ workflowResponse.setProcessInstanceID(processInstanceId);
+ workflowResponse.setMessageCode(callbackResponse.getStatusCode());
+ workflowResponse.setMessage(callbackResponse.getMessage());
+ WorkflowContext context = new WorkflowContext(processKey, requestId, defaultContextTimeout, workflowResponse);
+ put(context);
+ }
+
+
+ /**
+ * Timeout thread which monitors the delay queue for expired context and send timeout response to client
+ *
+ */
+ private class TimeoutThread extends Thread {
+ @Override
+ public void run() {
+ while (!isInterrupted()) {
+ try {
+ WorkflowContext requestObject = responseQueue.take();
+ logger.debug("Time remaining for request id: {}:{}", requestObject.getRequestId(),
+ requestObject.getDelay(TimeUnit.MILLISECONDS));
+ logger.debug("Preparing timeout response for {}:{}", requestObject.getProcessKey(),
+ requestObject.getRequestId());
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ } catch (Exception e) {
+ logger.debug("WorkflowContextHolder timeout thread caught exception: ", e);
+ logger.error("{} {} {} {}", MessageEnum.BPMN_GENERAL_EXCEPTION.toString(), "BPMN",
+ ErrorCode.UnknownError.getValue(), "Error in WorkflowContextHolder timeout thread");
+ }
+ }
+ logger.debug("WorkflowContextHolder timeout thread interrupted, quitting");
+ }
+ }
}