blob: 0de62b0b4d17c9285a11fd6efb7cdbe51e4e7833 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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>>
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070031 implements ListenerService<E, L>, EventSink<E> {
tomd7356722014-08-26 01:07:39 -070032
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
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070045 @Override
tomd7356722014-08-26 01:07:39 -070046 public void addListener(L listener) {
47 checkNotNull(listener, "Listener cannot be null");
48 listeners.add(listener);
49 }
50
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070051 @Override
tomd7356722014-08-26 01:07:39 -070052 public void removeListener(L listener) {
53 checkNotNull(listener, "Listener cannot be null");
Thomas Vachuska1deab532015-05-19 13:49:55 -070054 if (!listeners.remove(listener)) {
55 log.warn("Listener {} not registered", listener);
Simon Huntff663742015-05-14 13:33:05 -070056 }
tomd7356722014-08-26 01:07:39 -070057 }
58
59 @Override
60 public void process(E event) {
61 for (L listener : listeners) {
62 try {
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070063 lastListener = listener;
64 lastStart = System.currentTimeMillis();
Thomas Vachuska18275622015-07-22 22:28:25 -070065 if (listener.isRelevant(event)) {
66 listener.event(event);
67 }
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070068 lastStart = 0;
tom19bf4212014-08-29 13:08:29 -070069 } catch (Exception error) {
tomd7356722014-08-26 01:07:39 -070070 reportProblem(event, error);
71 }
72 }
73 }
74
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070075 @Override
76 public void onProcessLimit() {
77 if (lastStart > 0) {
Thomas Vachuskaa09c0ed2015-07-17 18:58:55 -070078 long duration = System.currentTimeMillis() - lastStart;
79 if (duration > LIMIT) {
80 log.error("Listener {} exceeded execution time limit: {} ms; ejected",
81 lastListener.getClass().getName(),
82 duration);
83 removeListener(lastListener);
84 }
Thomas Vachuska99c92fd2015-06-01 11:44:53 -070085 lastStart = 0;
Thomas Vachuskab17c41f2015-05-19 11:16:05 -070086 }
87 }
88
tomb36046e2014-08-27 00:22:24 -070089 /**
90 * Reports a problem encountered while processing an event.
91 *
92 * @param event event being processed
93 * @param error error encountered while processing
94 */
95 protected void reportProblem(E event, Throwable error) {
Thomas Vachuska1deab532015-05-19 13:49:55 -070096 log.warn("Exception encountered while processing event " + event, error);
tomd7356722014-08-26 01:07:39 -070097 }
98
tomd7356722014-08-26 01:07:39 -070099}