Moving all files to root directory
[appc.git] / appc-dispatcher / appc-dispatcher-common / execution-queue-management-lib / src / main / java / org / openecomp / appc / executionqueue / impl / QueueManager.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.executionqueue.impl;
23
24 import java.util.concurrent.ExecutorService;
25 import java.util.concurrent.Executors;
26 import java.util.concurrent.LinkedBlockingQueue;
27
28 import org.openecomp.appc.executionqueue.MessageExpirationListener;
29 import org.openecomp.appc.executionqueue.helper.Util;
30 import org.openecomp.appc.executionqueue.impl.object.QueueMessage;
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33
34 public class QueueManager {
35
36     private LinkedBlockingQueue<QueueMessage> queue;
37
38     private MessageExpirationListener listener;
39
40     private static int MAX_QUEUE_SIZE = Util.getExecutionQueSize();
41
42     private static int MAX_THREAD_SIZE = Util.getThreadPoolSize();
43
44     private ExecutorService messageExecutor;
45
46     private static final EELFLogger logger =
47             EELFManager.getInstance().getLogger(QueueManager.class);
48
49     private QueueManager(){
50         init();
51     }
52
53     private static class QueueManagerHolder {
54         private static final QueueManager INSTANCE = new QueueManager();
55     }
56
57     public static QueueManager getInstance() {
58         return QueueManagerHolder.INSTANCE;
59     }
60
61     private void init(){
62         queue = new LinkedBlockingQueue(MAX_QUEUE_SIZE);
63         messageExecutor = Executors.newFixedThreadPool(MAX_THREAD_SIZE,Util.getThreadFactory(true));
64
65         for(int i=0;i<MAX_THREAD_SIZE;i++){
66             messageExecutor.submit(new Runnable() {
67                 @Override
68                 public void run() {
69                     while (true){
70                         try{
71                             QueueMessage queueMessage = queue.take();
72                             if(messageExpired(queueMessage)){
73                                 logger.debug("Message expired "+ queueMessage.getMessage());
74                                 if(listener != null){
75                                     listener.onMessageExpiration(queueMessage.getMessage());
76                                 }
77                                 else{
78                                     logger.warn("Listener not available for expired message ");
79                                 }
80                             }
81                             else{
82                                 queueMessage.getMessage().run();
83                             }
84                         } catch (Exception e) {
85                             logger.error("Error in startMessagePolling method of ExecutionQueueServiceImpl" + e.getMessage());
86                         }
87                     }
88                 }
89             });
90         }
91     }
92
93     public void setListener(MessageExpirationListener listener) {
94         this.listener = listener;
95     }
96
97     public boolean enqueueTask(QueueMessage queueMessage) {
98         return queue.offer(queueMessage);
99     }
100
101     private boolean messageExpired(QueueMessage queueMessage) {
102         if(queueMessage.getExpirationTime() != null){
103             return queueMessage.getExpirationTime().getTime() < System.currentTimeMillis();
104         }
105         return false;
106     }
107
108 }