/** * Posts an event to all registered subscribers. This method will return successfully after the * event has been posted to all subscribers, and regardless of any exceptions thrown by * subscribers. * * <p>If no subscribers have been subscribed for {@code event}'s class, and {@code event} is not * already a {@link DeadEvent}, it will be wrapped in a DeadEvent and reposted. * * @param event event to post. */ public void post(Object event) { Iterator<Subscriber> eventSubscribers = subscribers.getSubscribers(event); if (eventSubscribers.hasNext()) { dispatcher.dispatch(event, eventSubscribers); } else if (!(event instanceof DeadEvent)) { // the event had no subscribers and was not itself a DeadEvent post(new DeadEvent(this, event)); } }
/** * Creates a new AsyncEventBus that will use {@code executor} to dispatch events. * * @param executor Executor to use to dispatch events. It is the caller's responsibility to shut * down the executor after the last event has been posted to this event bus. */ public AsyncEventBus(Executor executor) { super("default", executor, Dispatcher.legacyAsync(), LoggingHandler.INSTANCE); } }
/** * Creates a new EventBus with the given {@link SubscriberExceptionHandler}. * * @param exceptionHandler Handler for subscriber exceptions. * @since 16.0 */ public EventBus(SubscriberExceptionHandler exceptionHandler) { this( "default", MoreExecutors.directExecutor(), Dispatcher.perThreadDispatchQueue(), exceptionHandler); }
public void testPerThreadQueuedDispatcher() { dispatcher = Dispatcher.perThreadDispatchQueue(); dispatcher.dispatch(1, integerSubscribers.iterator()); assertThat(dispatchedSubscribers) .containsExactly( i1, i2, i3, // Integer subscribers are dispatched to first. s1, s2, // Though each integer subscriber dispatches to all string subscribers, s1, s2, // those string subscribers aren't actually dispatched to until all integer s1, s2 // subscribers have finished. ) .inOrder(); }
public void testImmediateDispatcher() { dispatcher = Dispatcher.immediate(); dispatcher.dispatch(1, integerSubscribers.iterator()); assertThat(dispatchedSubscribers) .containsExactly( i1, s1, s2, // Each integer subscriber immediately dispatches to 2 string subscribers. i2, s1, s2, i3, s1, s2) .inOrder(); }
public PackageSanityTests() throws Exception { DummySubscriber dummySubscriber = new DummySubscriber(); setDefault(Subscriber.class, dummySubscriber.toSubscriber()); setDefault(Method.class, DummySubscriber.subscriberMethod()); setDefault(SubscriberExceptionContext.class, dummySubscriber.toContext()); setDefault(Dispatcher.class, Dispatcher.immediate()); }
public void testPerThreadQueuedDispatcher() { dispatcher = Dispatcher.perThreadDispatchQueue(); dispatcher.dispatch(1, integerSubscribers.iterator()); assertThat(dispatchedSubscribers) .containsExactly( i1, i2, i3, // Integer subscribers are dispatched to first. s1, s2, // Though each integer subscriber dispatches to all string subscribers, s1, s2, // those string subscribers aren't actually dispatched to until all integer s1, s2 // subscribers have finished. ).inOrder(); }
public void testImmediateDispatcher() { dispatcher = Dispatcher.immediate(); dispatcher.dispatch(1, integerSubscribers.iterator()); assertThat(dispatchedSubscribers) .containsExactly( i1, s1, s2, // Each integer subscriber immediately dispatches to 2 string subscribers. i2, s1, s2, i3, s1, s2 ).inOrder(); }
public ReentrantEventBus() { super(IDENTIFIER, directExecutor(), immediate(), new Slf4jSubscriberExceptionHandler(IDENTIFIER)); } }
/** * Creates a new AsyncEventBus that will use {@code executor} to dispatch events. Assigns {@code * identifier} as the bus's name for logging purposes. * * @param identifier short name for the bus, for logging purposes. * @param executor Executor to use to dispatch events. It is the caller's responsibility to shut * down the executor after the last event has been posted to this event bus. */ public AsyncEventBus(String identifier, Executor executor) { super(identifier, executor, Dispatcher.legacyAsync(), LoggingHandler.INSTANCE); }
@Subscribe public void handleInteger(Integer integer) { dispatchedSubscribers.add(this); dispatcher.dispatch("hello", stringSubscribers.iterator()); }
/** * Creates a new EventBus with the given {@code identifier}. * * @param identifier a brief name for this bus, for logging purposes. Should be a valid Java * identifier. */ public EventBus(String identifier) { this( identifier, MoreExecutors.directExecutor(), Dispatcher.perThreadDispatchQueue(), LoggingHandler.INSTANCE); }
public ReentrantAsyncEventBus(final Executor executor) { super(IDENTIFIER, executor, immediate(), new Slf4jSubscriberExceptionHandler(IDENTIFIER)); } }
/** * Creates a new AsyncEventBus that will use {@code executor} to dispatch events. * * @param executor Executor to use to dispatch events. It is the caller's responsibility to shut * down the executor after the last event has been posted to this event bus. * @param subscriberExceptionHandler Handler used to handle exceptions thrown from subscribers. * See {@link SubscriberExceptionHandler} for more information. * @since 16.0 */ public AsyncEventBus(Executor executor, SubscriberExceptionHandler subscriberExceptionHandler) { super("default", executor, Dispatcher.legacyAsync(), subscriberExceptionHandler); }
@Override public void run() { try { barrier.await(); } catch (Exception e) { throw new AssertionError(e); } dispatcher.dispatch(2, integerSubscribers.iterator()); latch.countDown(); } })
/** * Creates a new EventBus with the given {@code identifier}. * * @param identifier a brief name for this bus, for logging purposes. Should be a valid Java * identifier. */ public EventBus(String identifier) { this( identifier, MoreExecutors.directExecutor(), Dispatcher.perThreadDispatchQueue(), LoggingHandler.INSTANCE); }
public EventBusThatThrowsException(final String identifier) { super(identifier, // See postWithException below MoreExecutors.directExecutor(), Dispatcher.immediate(), new SubscriberExceptionsTrackerHandler()); this.subscribers = getSubscribers(); this.dispatcher = getDispatcher(); this.exceptionHandler = getExceptionHandler(); }
/** * Creates a new AsyncEventBus that will use {@code executor} to dispatch events. * * @param executor Executor to use to dispatch events. It is the caller's responsibility to shut * down the executor after the last event has been posted to this event bus. */ public AsyncEventBus(Executor executor) { super("default", executor, Dispatcher.legacyAsync(), LoggingHandler.INSTANCE); } }
@Override public void run() { try { barrier.await(); } catch (Exception e) { throw new AssertionError(e); } dispatcher.dispatch("foo", stringSubscribers.iterator()); latch.countDown(); } })
/** * Creates a new EventBus with the given {@link SubscriberExceptionHandler}. * * @param exceptionHandler Handler for subscriber exceptions. * @since 16.0 */ public EventBus(SubscriberExceptionHandler exceptionHandler) { this( "default", MoreExecutors.directExecutor(), Dispatcher.perThreadDispatchQueue(), exceptionHandler); }