f6ed2fb40d8083f5edc762f688a3e2fe957db5e4
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.executionqueue.impl;
24
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27 import java.util.concurrent.LinkedBlockingQueue;
28
29 import org.openecomp.appc.executionqueue.MessageExpirationListener;
30 import org.openecomp.appc.executionqueue.helper.Util;
31 import org.openecomp.appc.executionqueue.impl.object.QueueMessage;
32
33 import com.att.eelf.configuration.EELFLogger;
34 import com.att.eelf.configuration.EELFManager;
35
36 public class QueueManager {
37
38     private LinkedBlockingQueue<QueueMessage<? extends Runnable>> queue;
39
40     private MessageExpirationListener listener;
41
42     private static int MAX_QUEUE_SIZE = Util.getExecutionQueSize();
43
44     private static int MAX_THREAD_SIZE = Util.getThreadPoolSize();
45
46     private ExecutorService messageExecutor;
47
48     private static final EELFLogger logger =
49             EELFManager.getInstance().getLogger(QueueManager.class);
50
51     private QueueManager(){
52         init();
53     }
54
55     private static class QueueManagerHolder {
56         private static final QueueManager INSTANCE = new QueueManager();
57     }
58
59     public static QueueManager getInstance() {
60         return QueueManagerHolder.INSTANCE;
61     }
62
63     private void init(){
64         queue = new LinkedBlockingQueue<QueueMessage<? extends Runnable>>(MAX_QUEUE_SIZE);
65         messageExecutor = Executors.newFixedThreadPool(MAX_THREAD_SIZE,Util.getThreadFactory(true));
66
67         for(int i=0;i<MAX_THREAD_SIZE;i++){
68             messageExecutor.submit(new Runnable() {
69                 @Override
70                 public void run() {
71                     while (true){
72                         try{
73                             QueueMessage<? extends Runnable> queueMessage = queue.take();
74                             if (queueMessage.isExpired()) {
75                                 logger.debug("Message expired "+ queueMessage.getMessage());
76                                 if(listener != null){
77                                     listener.onMessageExpiration(queueMessage.getMessage());
78                                 }
79                                 else{
80                                     logger.warn("Listener not available for expired message ");
81                                 }
82                             }
83                             else{
84                                 queueMessage.getMessage().run();
85                             }
86                         } catch (Exception e) {
87                             logger.error("Error in startMessagePolling method of ExecutionQueueServiceImpl" + e.getMessage());
88                         }
89                     }
90                 }
91             });
92         }
93     }
94
95     public void setListener(MessageExpirationListener listener) {
96         this.listener = listener;
97     }
98
99     public boolean enqueueTask(QueueMessage<? extends Runnable> queueMessage) {
100         return queue.offer(queueMessage);
101     }
102
103 }