1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
/*-
* ============LICENSE_START=======================================================
* ONAP : APPC
* ================================================================================
* Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Copyright (C) 2017 Amdocs
* =============================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* ============LICENSE_END=========================================================
*/
package org.onap.appc.executionqueue.impl;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import org.onap.appc.executionqueue.MessageExpirationListener;
import org.onap.appc.executionqueue.helper.Util;
import org.onap.appc.executionqueue.impl.object.QueueMessage;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class QueueManager {
private final EELFLogger logger = EELFManager.getInstance().getLogger(QueueManager.class);
private ExecutorService messageExecutor;
private LinkedBlockingQueue<QueueMessage> queue;
private int max_thread_size;
private int max_queue_size;
private Util executionQueueUtil;
public QueueManager() {
}
/**
* Initialization method used by blueprint
*/
public void init() {
max_thread_size = executionQueueUtil.getThreadPoolSize();
max_queue_size = executionQueueUtil.getExecutionQueueSize();
messageExecutor = new ThreadPoolExecutor(
max_thread_size,
max_thread_size,
0L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue(max_queue_size),
executionQueueUtil.getThreadFactory(true, "appc-dispatcher"),
new ThreadPoolExecutor.AbortPolicy());
}
/**
* Destory method used by blueprint
*/
public void stop() {
// Disable new tasks from being submitted
messageExecutor.shutdown();
List<Runnable> rejectedRunnables = messageExecutor.shutdownNow();
logger.info(String.format("Rejected %d waiting tasks include ", rejectedRunnables.size()));
try {
messageExecutor.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
while (!messageExecutor.awaitTermination(100, TimeUnit.MILLISECONDS)) {
logger.debug("QueueManager is being shut down because it still has threads not interrupted");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
messageExecutor.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
/**
* Injected by blueprint
*
* @param executionQueueUtil
*/
public void setExecutionQueueUtil(Util executionQueueUtil) {
this.executionQueueUtil = executionQueueUtil;
}
public boolean enqueueTask(QueueMessage queueMessage) {
boolean isEnqueued = true;
try {
messageExecutor.execute(() -> queueMessage.getMessage().run());
} catch (RejectedExecutionException ree) {
isEnqueued = false;
}
return isEnqueued;
}
private boolean messageExpired(QueueMessage queueMessage) {
return queueMessage.getExpirationTime() != null &&
queueMessage.getExpirationTime().getTime() < System.currentTimeMillis();
}
}
|