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.onap.appc.listener.impl;
27 import java.util.HashMap;
28 import java.util.Iterator;
31 import java.util.concurrent.ArrayBlockingQueue;
32 import java.util.concurrent.ThreadPoolExecutor;
33 import java.util.concurrent.TimeUnit;
35 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
36 import org.onap.appc.listener.Controller;
37 import org.onap.appc.listener.Listener;
38 import org.onap.appc.listener.ListenerProperties;
40 import com.att.eelf.configuration.EELFLogger;
41 import com.att.eelf.configuration.EELFManager;
45 * A common implementation of a controller. This controller should not need to be modified to implement new listeners
48 public class ControllerImpl implements Controller {
50 private final EELFLogger LOG = EELFManager.getInstance().getLogger(ControllerImpl.class);
52 private int LISTENER_COUNT = 1;
54 private Map<ListenerProperties, Listener> listeners = null;
56 private ThreadPoolExecutor executor;
59 * Creates a Controller with the set of listener properties which will be used to start listener threads.
62 * A non null Set of ListenerProperties
64 public ControllerImpl(Set<ListenerProperties> properties) {
65 listeners = new HashMap<ListenerProperties, Listener>();
66 for (ListenerProperties props : properties) {
67 if (props.getClass() != null) {
68 listeners.put(props, null);
70 LOG.error(String.format(
71 "The ListenerProperties %s has no Listener class associated with it and will not run.", props));
75 LISTENER_COUNT = properties.size();
77 executor = new ThreadPoolExecutor(LISTENER_COUNT, LISTENER_COUNT, 1, TimeUnit.SECONDS,
78 new ArrayBlockingQueue<Runnable>(LISTENER_COUNT));
80 // Custom Named thread factory
81 BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("Appc-Listener-%d").build();
82 executor.setThreadFactory(threadFactory);
87 LOG.info("Starting DMaaP Controller.");
88 for (ListenerProperties props : listeners.keySet()) {
90 if (props.isDisabled()) {
91 LOG.warn(String.format("The listener %s is disabled and will not be run", props.getPrefix()));
93 Listener l = props.getListenerClass().getConstructor(ListenerProperties.class).newInstance(props);
94 l.setListenerId(props.getPrefix());
95 listeners.put(props, l);
98 } catch (Exception e) {
99 LOG.error(String.format("Exception while starting listener %s.", props), e);
105 public void stop(boolean stopNow) {
106 LOG.info("Stopping DMaaP Controller.");
107 Iterator<Listener> itr = listeners.values().iterator();
108 while (itr.hasNext()) {
109 Listener l = itr.next();
117 // disable new tasks from being submitted
121 if (!executor.awaitTermination(timeout, TimeUnit.SECONDS)) {
122 LOG.error("Not all tasks completed execution after " + timeout + " seconds. " +
123 "Attempting to stop all actively executing tasks.");
124 executor.shutdownNow();
126 if (!executor.awaitTermination(timeout, TimeUnit.SECONDS)) {
127 LOG.error("Could not terminate all tasks after " + (timeout*2) + " seconds.");
129 } catch (InterruptedException e) {
130 executor.shutdownNow();
131 Thread.currentThread().interrupt();
136 public Map<ListenerProperties, Listener> getListeners() {