2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.listener.impl;
24 import java.util.HashMap;
25 import java.util.Iterator;
28 import java.util.concurrent.ArrayBlockingQueue;
29 import java.util.concurrent.ThreadPoolExecutor;
30 import java.util.concurrent.TimeUnit;
32 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
33 import org.openecomp.appc.listener.Controller;
34 import org.openecomp.appc.listener.Listener;
35 import org.openecomp.appc.listener.ListenerProperties;
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
42 * A common implementation of a controller. This controller should not need to be modified to implement new listeners
45 public class ControllerImpl implements Controller {
47 private final EELFLogger LOG = EELFManager.getInstance().getLogger(ControllerImpl.class);
49 private int LISTENER_COUNT = 1;
51 private Map<ListenerProperties, Listener> listeners = null;
53 private ThreadPoolExecutor executor;
56 * Creates a Controller with the set of listener properties which will be used to start listener threads.
59 * A non null Set of ListenerProperties
61 public ControllerImpl(Set<ListenerProperties> properties) {
62 listeners = new HashMap<ListenerProperties, Listener>();
63 for (ListenerProperties props : properties) {
64 if (props.getClass() != null) {
65 listeners.put(props, null);
67 LOG.error(String.format(
68 "The ListenerProperties %s has no Listener class associated with it and will not run.", props));
72 LISTENER_COUNT = properties.size();
74 executor = new ThreadPoolExecutor(LISTENER_COUNT, LISTENER_COUNT, 1, TimeUnit.SECONDS,
75 new ArrayBlockingQueue<Runnable>(LISTENER_COUNT));
77 // Custom Named thread factory
78 BasicThreadFactory threadFactory = new BasicThreadFactory.Builder().namingPattern("Appc-Listener-%d").build();
79 executor.setThreadFactory(threadFactory);
84 LOG.info("Starting DMaaP Controller.");
85 for (ListenerProperties props : listeners.keySet()) {
87 if (props.isDisabled()) {
88 LOG.warn(String.format("The listener %s is disabled and will not be run", props.getPrefix()));
90 Listener l = props.getListenerClass().getConstructor(ListenerProperties.class).newInstance(props);
91 l.setListenerId(props.getPrefix());
92 listeners.put(props, l);
95 } catch (Exception e) {
97 LOG.error(String.format("Exception while starting listener %s.", props), e);
103 public void stop(boolean stopNow) {
104 LOG.info("Stopping DMaaP Controller.");
105 Iterator<Listener> itr = listeners.values().iterator();
106 while (itr.hasNext()) {
107 Listener l = itr.next();
117 executor.awaitTermination(10, TimeUnit.SECONDS);
118 } catch (InterruptedException e) {
123 public Map<ListenerProperties, Listener> getListeners() {