Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.event; |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 17 | |
| 18 | import org.slf4j.Logger; |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 19 | |
| 20 | import java.util.Set; |
| 21 | import java.util.concurrent.CopyOnWriteArraySet; |
| 22 | |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 23 | import static com.google.common.base.Preconditions.checkNotNull; |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 24 | import static org.slf4j.LoggerFactory.getLogger; |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 27 | * 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. |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 29 | */ |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 30 | public class ListenerRegistry<E extends Event, L extends EventListener<E>> |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 31 | implements EventSink<E> { |
| 32 | |
Thomas Vachuska | a09c0ed | 2015-07-17 18:58:55 -0700 | [diff] [blame] | 33 | private static final long LIMIT = 1_800; // ms |
| 34 | |
tom | 5f38b3a | 2014-08-27 23:50:54 -0700 | [diff] [blame] | 35 | private final Logger log = getLogger(getClass()); |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 36 | |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 37 | private long lastStart; |
| 38 | private L lastListener; |
| 39 | |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 40 | /** |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 41 | * Set of listeners that have registered. |
| 42 | */ |
| 43 | protected final Set<L> listeners = new CopyOnWriteArraySet<>(); |
| 44 | |
| 45 | |
| 46 | /** |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 47 | * 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 Vachuska | 1deab53 | 2015-05-19 13:49:55 -0700 | [diff] [blame] | 63 | if (!listeners.remove(listener)) { |
| 64 | log.warn("Listener {} not registered", listener); |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 65 | } |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void process(E event) { |
| 70 | for (L listener : listeners) { |
| 71 | try { |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 72 | lastListener = listener; |
| 73 | lastStart = System.currentTimeMillis(); |
Thomas Vachuska | 1827562 | 2015-07-22 22:28:25 -0700 | [diff] [blame] | 74 | if (listener.isRelevant(event)) { |
| 75 | listener.event(event); |
| 76 | } |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 77 | lastStart = 0; |
tom | 19bf421 | 2014-08-29 13:08:29 -0700 | [diff] [blame] | 78 | } catch (Exception error) { |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 79 | reportProblem(event, error); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 84 | @Override |
| 85 | public void onProcessLimit() { |
| 86 | if (lastStart > 0) { |
Thomas Vachuska | a09c0ed | 2015-07-17 18:58:55 -0700 | [diff] [blame] | 87 | 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 Vachuska | 99c92fd | 2015-06-01 11:44:53 -0700 | [diff] [blame] | 94 | lastStart = 0; |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 98 | /** |
| 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 Vachuska | 1deab53 | 2015-05-19 13:49:55 -0700 | [diff] [blame] | 105 | log.warn("Exception encountered while processing event " + event, error); |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 106 | } |
| 107 | |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 108 | } |