2 * Copyright 2015-present Open Networking Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package io.atomix.utils.event;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 import java.util.concurrent.CopyOnWriteArraySet;
24 import static com.google.common.base.Preconditions.checkNotNull;
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.
30 public class ListenerRegistry<E extends Event, L extends EventListener<E>>
31 implements ListenerService<E, L>, EventSink<E> {
33 private static final long LIMIT = 1_800; // ms
35 private final Logger log = LoggerFactory.getLogger(getClass());
37 private long lastStart;
38 private L lastListener;
41 * Set of listeners that have registered.
43 protected final Set<L> listeners = new CopyOnWriteArraySet<>();
46 public void addListener(L listener) {
47 checkNotNull(listener, "Listener cannot be null");
48 listeners.add(listener);
52 public void removeListener(L listener) {
53 checkNotNull(listener, "Listener cannot be null");
54 if (!listeners.remove(listener)) {
55 log.warn("Listener {} not registered", listener);
60 public void process(E event) {
61 for (L listener : listeners) {
63 lastListener = listener;
64 lastStart = System.currentTimeMillis();
65 if (listener.isRelevant(event)) {
66 listener.event(event);
69 } catch (Exception error) {
70 reportProblem(event, error);
76 public void onProcessLimit() {
78 long duration = System.currentTimeMillis() - lastStart;
79 if (duration > LIMIT) {
80 log.error("Listener {} exceeded execution time limit: {} ms; ejected",
81 lastListener.getClass().getName(),
83 removeListener(lastListener);
90 * Reports a problem encountered while processing an event.
92 * @param event event being processed
93 * @param error error encountered while processing
95 protected void reportProblem(E event, Throwable error) {
96 log.warn("Exception encountered while processing event " + event, error);