001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package org.openstreetmap.josm.eventbus; 016 017import java.lang.reflect.Method; 018import java.util.Iterator; 019import java.util.Locale; 020import java.util.Objects; 021import java.util.concurrent.Executor; 022import java.util.logging.Level; 023import java.util.logging.Logger; 024 025/** 026 * Dispatches events to listeners, and provides ways for listeners to register themselves. 027 * 028 * <p>The EventBus allows publish-subscribe-style communication between components without requiring 029 * the components to explicitly register with one another (and thus be aware of each other). It is 030 * designed exclusively to replace traditional Java in-process event distribution using explicit 031 * registration. It is <em>not</em> a general-purpose publish-subscribe system, nor is it intended 032 * for interprocess communication. 033 * 034 * <h2>Receiving Events</h2> 035 * 036 * <p>To receive events, an object should: 037 * 038 * <ol> 039 * <li>Expose a public method, known as the <i>event subscriber</i>, which accepts a single 040 * argument of the type of event desired; 041 * <li>Mark it with a {@link Subscribe} annotation; 042 * <li>Pass itself to an EventBus instance's {@link #register(Object)} method. 043 * </ol> 044 * 045 * <h2>Posting Events</h2> 046 * 047 * <p>To post an event, simply provide the event object to the {@link #post(Object)} method. The 048 * EventBus instance will determine the type of event and route it to all registered listeners. 049 * 050 * <p>Events are routed based on their type — an event will be delivered to any subscriber for 051 * any type to which the event is <em>assignable.</em> This includes implemented interfaces, all 052 * superclasses, and all interfaces implemented by superclasses. 053 * 054 * <p>When {@code post} is called, all registered subscribers for an event are run in sequence, so 055 * subscribers should be reasonably quick. If an event may trigger an extended process (such as a 056 * database load), spawn a thread or queue it for later. (For a convenient way to do this, use an 057 * {@link AsyncEventBus}.) 058 * 059 * <h2>Subscriber Methods</h2> 060 * 061 * <p>Event subscriber methods must accept only one argument: the event. 062 * 063 * <p>Subscribers should not, in general, throw. If they do, the EventBus will catch and log the 064 * exception. This is rarely the right solution for error handling and should not be relied upon; it 065 * is intended solely to help find problems during development. 066 * 067 * <p>The EventBus guarantees that it will not call a subscriber method from multiple threads 068 * simultaneously, unless the method explicitly allows it by bearing the {@link 069 * AllowConcurrentEvents} annotation. If this annotation is not present, subscriber methods need not 070 * worry about being reentrant, unless also called from outside the EventBus. 071 * 072 * <h2>Dead Events</h2> 073 * 074 * <p>If an event is posted, but no registered subscribers can accept it, it is considered "dead." 075 * To give the system a second chance to handle dead events, they are wrapped in an instance of 076 * {@link DeadEvent} and reposted. 077 * 078 * <p>If a subscriber for a supertype of all events (such as Object) is registered, no event will 079 * ever be considered dead, and no DeadEvents will be generated. Accordingly, while DeadEvent 080 * extends {@link Object}, a subscriber registered to receive any Object will never receive a 081 * DeadEvent. 082 * 083 * <p>This class is safe for concurrent use. 084 * 085 * <p>See the Guava User Guide article on <a 086 * href="https://github.com/google/guava/wiki/EventBusExplained">{@code EventBus}</a>. 087 * 088 * @author Cliff Biffle 089 * @since 10.0 090 */ 091public class EventBus { 092 093 private static final Logger logger = Logger.getLogger(EventBus.class.getName()); 094 095 private final String identifier; 096 private final Executor executor; 097 private final SubscriberExceptionHandler exceptionHandler; 098 099 private final SubscriberRegistry subscribers = new SubscriberRegistry(this); 100 private final Dispatcher dispatcher; 101 102 /** 103 * Simple executor singleton that runs commands in the same thread. 104 */ 105 enum DirectExecutor implements Executor { 106 /** unique instance */ 107 INSTANCE; 108 109 @Override 110 public void execute(Runnable command) { 111 command.run(); 112 } 113 } 114 115 /** Creates a new EventBus named "default". */ 116 public EventBus() { 117 this("default"); 118 } 119 120 /** 121 * Creates a new EventBus with the given {@code identifier}. 122 * 123 * @param identifier a brief name for this bus, for logging purposes. Should be a valid Java 124 * identifier. 125 */ 126 public EventBus(String identifier) { 127 this( 128 identifier, 129 DirectExecutor.INSTANCE, 130 Dispatcher.perThreadDispatchQueue(), 131 LoggingHandler.INSTANCE); 132 } 133 134 /** 135 * Creates a new EventBus with the given {@link SubscriberExceptionHandler}. 136 * 137 * @param exceptionHandler Handler for subscriber exceptions. 138 * @since 16.0 139 */ 140 public EventBus(SubscriberExceptionHandler exceptionHandler) { 141 this( 142 "default", 143 DirectExecutor.INSTANCE, 144 Dispatcher.perThreadDispatchQueue(), 145 exceptionHandler); 146 } 147 148 /** 149 * Constructs a new {@code EventBus}. 150 * @param identifier the identifier for this event bus. Must not be null 151 * @param executor the default executor to use for dispatching events to subscribers. Must not be null 152 * @param dispatcher the event dispatcher. Must not be null 153 * @param exceptionHandler handles the exceptions thrown by a subscriber. Must not be null 154 */ 155 EventBus( 156 String identifier, 157 Executor executor, 158 Dispatcher dispatcher, 159 SubscriberExceptionHandler exceptionHandler) { 160 this.identifier = Objects.requireNonNull(identifier); 161 this.executor = Objects.requireNonNull(executor); 162 this.dispatcher = Objects.requireNonNull(dispatcher); 163 this.exceptionHandler = Objects.requireNonNull(exceptionHandler); 164 } 165 166 /** 167 * Returns the identifier for this event bus. 168 * @return the identifier for this event bus 169 * 170 * @since 19.0 171 */ 172 public final String identifier() { 173 return identifier; 174 } 175 176 /** 177 * Returns the default executor this event bus uses for dispatching events to subscribers. 178 * @return the default executor this event bus uses for dispatching events to subscribers 179 */ 180 final Executor executor() { 181 return executor; 182 } 183 184 /** 185 * Handles the given exception thrown by a subscriber with the given context. 186 * @param e exception thrown by a subscriber 187 * @param context subscriber context 188 */ 189 void handleSubscriberException(Throwable e, SubscriberExceptionContext context) { 190 Objects.requireNonNull(e); 191 Objects.requireNonNull(context); 192 try { 193 exceptionHandler.handleException(e, context); 194 } catch (Throwable e2) { 195 // if the handler threw an exception... well, just log it 196 logger.log( 197 Level.SEVERE, 198 String.format(Locale.ROOT, "Exception %s thrown while handling exception: %s", e2, e), 199 e2); 200 } 201 } 202 203 /** 204 * Registers all subscriber methods on {@code object} to receive events. 205 * 206 * @param object object whose subscriber methods should be registered. 207 */ 208 public void register(Object object) { 209 subscribers.register(object); 210 } 211 212 /** 213 * Unregisters all subscriber methods on a registered {@code object}. 214 * 215 * @param object object whose subscriber methods should be unregistered. 216 * @throws IllegalArgumentException if the object was not previously registered. 217 */ 218 public void unregister(Object object) { 219 subscribers.unregister(object); 220 } 221 222 /** 223 * Posts an event to all registered subscribers. This method will return successfully after the 224 * event has been posted to all subscribers, and regardless of any exceptions thrown by 225 * subscribers. 226 * 227 * <p>If no subscribers have been subscribed for {@code event}'s class, and {@code event} is not 228 * already a {@link DeadEvent}, it will be wrapped in a DeadEvent and reposted. 229 * 230 * @param event event to post. 231 */ 232 public void post(Object event) { 233 Iterator<Subscriber> eventSubscribers = subscribers.getSubscribers(event); 234 if (eventSubscribers.hasNext()) { 235 dispatcher.dispatch(event, eventSubscribers); 236 } else if (!(event instanceof DeadEvent)) { 237 // the event had no subscribers and was not itself a DeadEvent 238 post(new DeadEvent(this, event)); 239 } 240 } 241 242 @Override 243 public String toString() { 244 return "EventBus [" + identifier + ']'; 245 } 246 247 /** Simple logging handler for subscriber exceptions. */ 248 static final class LoggingHandler implements SubscriberExceptionHandler { 249 /** unique instance */ 250 static final LoggingHandler INSTANCE = new LoggingHandler(); 251 252 @Override 253 public void handleException(Throwable exception, SubscriberExceptionContext context) { 254 Logger logger = logger(context); 255 if (logger.isLoggable(Level.SEVERE)) { 256 logger.log(Level.SEVERE, message(context), exception); 257 } 258 } 259 260 private static Logger logger(SubscriberExceptionContext context) { 261 return Logger.getLogger(EventBus.class.getName() + "." + context.getEventBus().identifier()); 262 } 263 264 private static String message(SubscriberExceptionContext context) { 265 Method method = context.getSubscriberMethod(); 266 return "Exception thrown by subscriber method " 267 + method.getName() 268 + '(' 269 + method.getParameterTypes()[0].getName() 270 + ')' 271 + " on subscriber " 272 + context.getSubscriber() 273 + " when dispatching event: " 274 + context.getEvent(); 275 } 276 } 277}