blob: e6af2016a39c9fd65afe77b6b46b46223b09f8e2 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.event;
tomd7356722014-08-26 01:07:39 -070017
18import org.slf4j.Logger;
tomd7356722014-08-26 01:07:39 -070019
20import java.util.Set;
21import java.util.concurrent.CopyOnWriteArraySet;
22
tomd7356722014-08-26 01:07:39 -070023import static com.google.common.base.Preconditions.checkNotNull;
tom5f38b3a2014-08-27 23:50:54 -070024import static org.slf4j.LoggerFactory.getLogger;
tomd7356722014-08-26 01:07:39 -070025
26/**
tom96dfcab2014-08-28 09:26:03 -070027 * Base implementation of an event sink and a registry capable of tracking
28 * listeners and dispatching events to them as part of event sink processing.
tomd7356722014-08-26 01:07:39 -070029 */
Simon Huntff663742015-05-14 13:33:05 -070030public class ListenerRegistry<E extends Event, L extends EventListener<E>>
tomd7356722014-08-26 01:07:39 -070031 implements EventSink<E> {
32
Thomas Vachuskaa09c0ed2015-07-17 18:58:55 -070033 private static final long LIMIT = 1_800; // ms
34
tom5f38b3a2014-08-27 23:50:54 -070035 private final Logger log = getLogger(getClass());
tomd7356722014-08-26 01:07:39 -070036
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070037 private long lastStart;
38 private L lastListener;
39
tomd7356722014-08-26 01:07:39 -070040 /**
Simon Huntff663742015-05-14 13:33:05 -070041 * Set of listeners that have registered.
42 */
43 protected final Set<L> listeners = new CopyOnWriteArraySet<>();
44
45
46 /**
tomd7356722014-08-26 01:07:39 -070047 * Adds the specified listener.
48 *
49 * @param listener listener to be added
50 */
51 public void addListener(L listener) {
52 checkNotNull(listener, "Listener cannot be null");
53 listeners.add(listener);
54 }
55
56 /**
57 * Removes the specified listener.
58 *
59 * @param listener listener to be removed
60 */
61 public void removeListener(L listener) {
62 checkNotNull(listener, "Listener cannot be null");
Thomas Vachuska1deab532015-05-19 13:49:55 -070063 if (!listeners.remove(listener)) {
64 log.warn("Listener {} not registered", listener);
Simon Huntff663742015-05-14 13:33:05 -070065 }
tomd7356722014-08-26 01:07:39 -070066 }
67
68 @Override
69 public void process(E event) {
70 for (L listener : listeners) {
71 try {
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070072 lastListener = listener;
73 lastStart = System.currentTimeMillis();
Thomas Vachuska18275622015-07-22 22:28:25 -070074 if (listener.isRelevant(event)) {
75 listener.event(event);
76 }
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070077 lastStart = 0;
tom19bf4212014-08-29 13:08:29 -070078 } catch (Exception error) {
tomd7356722014-08-26 01:07:39 -070079 reportProblem(event, error);
80 }
81 }
82 }
83
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070084 @Override
85 public void onProcessLimit() {
86 if (lastStart > 0) {
Thomas Vachuskaa09c0ed2015-07-17 18:58:55 -070087 long duration = System.currentTimeMillis() - lastStart;
88 if (duration > LIMIT) {
89 log.error("Listener {} exceeded execution time limit: {} ms; ejected",
90 lastListener.getClass().getName(),
91 duration);
92 removeListener(lastListener);
93 }
Thomas Vachuska99c92fd2015-06-01 11:44:53 -070094 lastStart = 0;
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070095 }
96 }
97
tomb36046e2014-08-27 00:22:24 -070098 /**
99 * Reports a problem encountered while processing an event.
100 *
101 * @param event event being processed
102 * @param error error encountered while processing
103 */
104 protected void reportProblem(E event, Throwable error) {
Thomas Vachuska1deab532015-05-19 13:49:55 -0700105 log.warn("Exception encountered while processing event " + event, error);
tomd7356722014-08-26 01:07:39 -0700106 }
107
tomd7356722014-08-26 01:07:39 -0700108}