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