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>> |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 31 | implements ListenerService<E, L>, EventSink<E> { |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 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 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 45 | @Override |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 46 | public void addListener(L listener) { |
| 47 | checkNotNull(listener, "Listener cannot be null"); |
| 48 | listeners.add(listener); |
| 49 | } |
| 50 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 51 | @Override |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 52 | public void removeListener(L listener) { |
| 53 | checkNotNull(listener, "Listener cannot be null"); |
Thomas Vachuska | 1deab53 | 2015-05-19 13:49:55 -0700 | [diff] [blame] | 54 | if (!listeners.remove(listener)) { |
| 55 | log.warn("Listener {} not registered", listener); |
Simon Hunt | ff66374 | 2015-05-14 13:33:05 -0700 | [diff] [blame] | 56 | } |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void process(E event) { |
| 61 | for (L listener : listeners) { |
| 62 | try { |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 63 | lastListener = listener; |
| 64 | lastStart = System.currentTimeMillis(); |
Thomas Vachuska | 1827562 | 2015-07-22 22:28:25 -0700 | [diff] [blame] | 65 | if (listener.isRelevant(event)) { |
| 66 | listener.event(event); |
| 67 | } |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 68 | lastStart = 0; |
tom | 19bf421 | 2014-08-29 13:08:29 -0700 | [diff] [blame] | 69 | } catch (Exception error) { |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 70 | reportProblem(event, error); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 75 | @Override |
| 76 | public void onProcessLimit() { |
| 77 | if (lastStart > 0) { |
Thomas Vachuska | a09c0ed | 2015-07-17 18:58:55 -0700 | [diff] [blame] | 78 | 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 Vachuska | 99c92fd | 2015-06-01 11:44:53 -0700 | [diff] [blame] | 85 | lastStart = 0; |
Thomas Vachuska | b17c41f | 2015-05-19 11:16:05 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
tom | b36046e | 2014-08-27 00:22:24 -0700 | [diff] [blame] | 89 | /** |
| 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 Vachuska | 1deab53 | 2015-05-19 13:49:55 -0700 | [diff] [blame] | 96 | log.warn("Exception encountered while processing event " + event, error); |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 97 | } |
| 98 | |
tom | d735672 | 2014-08-26 01:07:39 -0700 | [diff] [blame] | 99 | } |