2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * ================================================================================
9 * Modifications Copyright (C) 2019 Ericsson
10 * =============================================================================
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.listener.impl;
28 import java.util.HashMap;
29 import java.util.Iterator;
32 import java.util.concurrent.ArrayBlockingQueue;
33 import java.util.concurrent.ThreadPoolExecutor;
34 import java.util.concurrent.TimeUnit;
36 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
37 import org.onap.appc.listener.Controller;
38 import org.onap.appc.listener.Listener;
39 import org.onap.appc.listener.ListenerProperties;
41 import com.att.eelf.configuration.EELFLogger;
42 import com.att.eelf.configuration.EELFManager;
46 * A common implementation of a controller. This controller should not need to be modified to implement new listeners
49 public class ControllerImpl implements Controller {
51 private final EELFLogger LOG = EELFManager.getInstance().getLogger(ControllerImpl.class);
53 private int LISTENER_COUNT = 1;
55 private Map<ListenerProperties, Listener> listeners = null;
57 private ThreadPoolExecutor executor;
60 * Creates a Controller with the set of listener properties which will be used to start listener threads.
63 * A non null Set of ListenerProperties
65 public ControllerImpl(Set<ListenerProperties> properties) {
66 listeners = new HashMap<ListenerProperties, Listener>();
67 for (ListenerProperties props : properties) {
68 if (props.getListenerClass() != null) {
69 listeners.put(props, null);
71 LOG.error(String.format(
72 "The ListenerProperties %s has no Listener class associated with it and will not run.", props));
73 properties.remove(props);
77 LISTENER_COUNT = properties.size();
79 // Only create executor if listeners are configured
80 if (LISTENER_COUNT > 0) {
81 executor = new ThreadPoolExecutor(LISTENER_COUNT, LISTENER_COUNT, 1, TimeUnit.SECONDS,
82 new ArrayBlockingQueue<Runnable>(LISTENER_COUNT));
84 // Custom Named thread factory
85 BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("Appc-Listener-%d").build();
86 executor.setThreadFactory(threadFactory);
92 LOG.info("Starting DMaaP Controller.");
93 for (ListenerProperties props : listeners.keySet()) {
95 if (props.isDisabled()) {
96 LOG.warn(String.format("The listener %s is disabled and will not be run", props.getPrefix()));
98 Listener l = props.getListenerClass().getConstructor(ListenerProperties.class).newInstance(props);
99 l.setListenerId(props.getPrefix());
100 listeners.put(props, l);
103 } catch (Exception e) {
104 LOG.error(String.format("Exception while starting listener %s.", props), e);
110 public void stop(boolean stopNow) {
111 LOG.info("Stopping DMaaP Controller.");
112 Iterator<Listener> itr = listeners.values().iterator();
113 while (itr.hasNext()) {
114 Listener l = itr.next();
115 if (stopNow && l != null) {
117 } else if(l != null){
122 // disable new tasks from being submitted
123 if(executor != null) {
127 if (!executor.awaitTermination(timeout, TimeUnit.SECONDS)) {
128 LOG.error("Not all tasks completed execution after " + timeout + " seconds. " +
129 "Attempting to stop all actively executing tasks.");
130 executor.shutdownNow();
132 if (!executor.awaitTermination(timeout, TimeUnit.SECONDS)) {
133 LOG.error("Could not terminate all tasks after " + (timeout * 2) + " seconds.");
135 } catch (InterruptedException e) {
136 executor.shutdownNow();
137 Thread.currentThread().interrupt();
143 public Map<ListenerProperties, Listener> getListeners() {