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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/*-
* ============LICENSE_START=======================================================
* ONAP : APPC
* ================================================================================
* Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Copyright (C) 2017 Amdocs
* =============================================================================
* Modifications Copyright (C) 2019 IBM
* =============================================================================
* 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.listener;
import java.security.SecureRandom;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.onap.appc.listener.impl.EventHandlerImpl;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
public abstract class AbstractListener implements Listener {
private final EELFLogger LOG = EELFManager.getInstance().getLogger(AbstractListener.class);
protected AtomicBoolean run = new AtomicBoolean(false);
protected int QUEUED_MIN = 1;
protected int QUEUED_MAX = 10;
protected int THREAD_MIN = 4;
protected int THREAD_MAX = THREAD_MIN; // Fixed thread pool
protected int THREAD_SCALE_DOWN_SEC = 10; // Number of seconds to wait until we remove idle threads
protected ThreadPoolExecutor executor;
protected EventHandler dmaap;
protected ListenerProperties props;
private String listenerId;
public AbstractListener(ListenerProperties props) {
updateProperties(props);
dmaap = new EventHandlerImpl(props);
if (dmaap.getClientId().equals("0")) {
dmaap.setClientId(String.valueOf(new SecureRandom().nextInt(1000)));
}
BlockingQueue<Runnable> threadQueue = new ArrayBlockingQueue<Runnable>(QUEUED_MAX + QUEUED_MIN + 1);
executor = new ThreadPoolExecutor(THREAD_MIN, THREAD_MAX, THREAD_SCALE_DOWN_SEC, TimeUnit.SECONDS, threadQueue,
new JobRejectionHandler());
// Custom Named thread factory
BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("DMaaP-Worker-%d").build();
executor.setThreadFactory(threadFactory);
run.set(true);
}
/**
* Starts a loop that will only end after stop() or stopNow() are called. The loop will read messages off the DMaaP
* topic and perform some action on them while writing messages back to DMaaP at critical points in the execution.
* Inherited from Runnable.
*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
LOG.error("Listener.run() has not been implemented");
}
@Override
public void stop() {
run.set(false);
LOG.info(String.format("Stopping with %d messages in queue", executor.getQueue().size()));
executor.shutdown();
try {
executor.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
LOG.error("Listener graceful stop() failed", e);
Thread.currentThread().interrupt();
}
// close DMaaP clients
if (dmaap != null) {
dmaap.closeClients();
}
LOG.info("Listener Thread Pool Finished");
}
@Override
public void stopNow() {
run.set(false);
LOG.info(String.format("StopNow called. Orphaning %d messages in the queue", executor.getQueue().size()));
executor.getQueue().clear();
stop();
}
@Override
public String getBenchmark() {
return String.format("%s - No benchmarking implemented.", getListenerId());
}
@Override
public String getListenerId() {
return listenerId;
}
// Sets the id of the listener in
@Override
public void setListenerId(String id) {
listenerId = id;
}
private void updateProperties(ListenerProperties properties) {
this.props = properties;
QUEUED_MIN =
Integer.valueOf(props.getProperty(ListenerProperties.KEYS.THREADS_MIN_QUEUE, String.valueOf(QUEUED_MIN)));
QUEUED_MAX =
Integer.valueOf(props.getProperty(ListenerProperties.KEYS.THREADS_MAX_QUEUE, String.valueOf(QUEUED_MAX)));
THREAD_MIN =
Integer.valueOf(props.getProperty(ListenerProperties.KEYS.THREADS_MIN_POOL, String.valueOf(THREAD_MIN)));
THREAD_MAX =
Integer.valueOf(props.getProperty(ListenerProperties.KEYS.THREADS_MAX_POOL, String.valueOf(THREAD_MAX)));
listenerId = props.getPrefix();
}
/**
* This class will be used to handle what happens when we cannot add a job because of a ThreadPool issue. It does
* not get invoked if there is any fault with the job. NOTE: So far, this has only been seen when doing a
* {@link Listener#stopNow}
*
*/
class JobRejectionHandler implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
LOG.error(String.format("A job was rejected. [%s]", r));
}
}
}
|