/** * 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)); } }
@Subscribe public void handleInteger(Integer integer) { dispatchedSubscribers.add(this); dispatcher.dispatch("hello", stringSubscribers.iterator()); }
@Override public void run() { try { barrier.await(); } catch (Exception e) { throw new AssertionError(e); } dispatcher.dispatch(2, integerSubscribers.iterator()); latch.countDown(); } })
@Override public void run() { try { barrier.await(); } catch (Exception e) { throw new AssertionError(e); } dispatcher.dispatch("foo", stringSubscribers.iterator()); latch.countDown(); } })
/** * 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)); } }
/** * 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)); } }
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 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(); }
/** * 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)); } }
/** * 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)); } }
@Subscribe public void handleInteger(Integer integer) { dispatchedSubscribers.add(this); dispatcher.dispatch("hello", stringSubscribers.iterator()); }
@Override public void run() { try { barrier.await(); } catch (Exception e) { throw new AssertionError(e); } dispatcher.dispatch("foo", stringSubscribers.iterator()); latch.countDown(); } }).start();
@Override public void run() { try { barrier.await(); } catch (Exception e) { throw new AssertionError(e); } dispatcher.dispatch(2, integerSubscribers.iterator()); latch.countDown(); } }).start();
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 void postWithException(final Object event) throws EventBusException { final Iterator<Subscriber> eventSubscribers = subscribers.getSubscribers(event); if (eventSubscribers.hasNext()) { // Just in case... exceptionHandler.reset(); RuntimeException guavaException = null; final Exception subscriberException; try { dispatcher.dispatch(event, eventSubscribers); } catch (final RuntimeException e) { guavaException = e; } finally { // This works because we are both using the immediate dispatcher and the direct executor // Note: we always want to dequeue here to avoid any memory leaks subscriberException = exceptionHandler.caught(); } if (guavaException != null) { throw guavaException; } if (subscriberException != null) { throw new EventBusException(subscriberException); } } else if (!(event instanceof DeadEvent)) { // the event had no subscribers and was not itself a DeadEvent post(new DeadEvent(this, event)); } }