2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.executionqueue.impl;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
29 import java.util.concurrent.LinkedBlockingQueue;
31 import org.openecomp.appc.executionqueue.MessageExpirationListener;
32 import org.openecomp.appc.executionqueue.helper.Util;
33 import org.openecomp.appc.executionqueue.impl.object.QueueMessage;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
38 public class QueueManager {
40 private LinkedBlockingQueue<QueueMessage<? extends Runnable>> queue;
42 private MessageExpirationListener listener;
44 private static int MAX_QUEUE_SIZE = Util.getExecutionQueSize();
46 private static int MAX_THREAD_SIZE = Util.getThreadPoolSize();
48 private ExecutorService messageExecutor;
50 private static final EELFLogger logger =
51 EELFManager.getInstance().getLogger(QueueManager.class);
53 private QueueManager(){
57 private static class QueueManagerHolder {
58 private static final QueueManager INSTANCE = new QueueManager();
61 public static QueueManager getInstance() {
62 return QueueManagerHolder.INSTANCE;
66 queue = new LinkedBlockingQueue<QueueMessage<? extends Runnable>>(MAX_QUEUE_SIZE);
67 messageExecutor = Executors.newFixedThreadPool(MAX_THREAD_SIZE,Util.getThreadFactory(true));
69 for(int i=0;i<MAX_THREAD_SIZE;i++){
70 messageExecutor.submit(new Runnable() {
75 QueueMessage<? extends Runnable> queueMessage = queue.take();
76 if (queueMessage.isExpired()) {
77 logger.debug("Message expired "+ queueMessage.getMessage());
79 listener.onMessageExpiration(queueMessage.getMessage());
82 logger.warn("Listener not available for expired message ");
86 queueMessage.getMessage().run();
88 } catch (Exception e) {
89 logger.error("Error in startMessagePolling method of ExecutionQueueServiceImpl" + e.getMessage());
97 public void setListener(MessageExpirationListener listener) {
98 this.listener = listener;
101 public boolean enqueueTask(QueueMessage<? extends Runnable> queueMessage) {
102 return queue.offer(queueMessage);