@Subscribe public void listenForStrings(String event) { bus.post(SECOND); } }
/** * 发布事件. * * @param event 作业事件 */ public void post(final JobEvent event) { if (isRegistered && !executorServiceObject.isShutdown()) { eventBus.post(event); } } }
@Subscribe public void listenForStrings(String event) { eventsReceived.add(event); ready = false; try { bus.post(SECOND); } finally { ready = true; } }
@Override public void onChange(final DataChangedEvent dataChangedEvent) { if (watchedChangedTypeList.contains(dataChangedEvent.getChangedType())) { eventBus.post(createShardingOrchestrationEvent(dataChangedEvent)); } } });
@Subscribe public void handleRuleChanges(RulesChangedEvent event) { event.deletedRuleIds().forEach(id -> { log.debug("Invalidated rule {}", id); metricRegistry.removeMatching((name, metric) -> name.startsWith(name(Rule.class, id))); }); event.updatedRuleIds().forEach(id -> log.debug("Refreshing rule {}", id)); scheduler.schedule(() -> serverEventBus.post(reloadAndSave()), 0, TimeUnit.SECONDS); }
@Subscribe public void handlePipelineChanges(PipelinesChangedEvent event) { event.deletedPipelineIds().forEach(id -> { log.debug("Invalidated pipeline {}", id); metricRegistry.removeMatching((name, metric) -> name.startsWith(name(Pipeline.class, id))); }); event.updatedPipelineIds().forEach(id -> log.debug("Refreshing pipeline {}", id)); scheduler.schedule(() -> serverEventBus.post(reloadAndSave()), 0, TimeUnit.SECONDS); }
@Timed @POST @Path("/cluster") @Consumes(MediaType.TEXT_PLAIN) @ApiOperation(value = "Create and send a cluster debug event.") @NoAuditEvent("only used to create a debug event") public void generateClusterDebugEvent(@ApiParam(name = "text", defaultValue = "Cluster Test") @Nullable String text) { clusterEventBus.post(DebugEvent.create(nodeId.toString(), isNullOrEmpty(text) ? "Cluster Test" : text)); }
/** * 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)); } }
/** * Tests that bridge methods are not subscribed to events. In Java 8, annotations are included on * the bridge method in addition to the original method, which causes both the original and bridge * methods to be subscribed (since both are annotated @Subscribe) without specifically checking * for bridge methods. */ public void testRegistrationWithBridgeMethod() { final AtomicInteger calls = new AtomicInteger(); bus.register( new Callback<String>() { @Subscribe @Override public void call(String s) { calls.incrementAndGet(); } }); bus.post("hello"); assertEquals(1, calls.get()); }
public void testNoReentrantEvents() { ReentrantEventsHater hater = new ReentrantEventsHater(); bus.register(hater); bus.post(FIRST); assertEquals( "ReentrantEventHater expected 2 events", Lists.<Object>newArrayList(FIRST, SECOND), hater.eventsReceived); }
@Override protected void setUp() throws Exception { subscriber = createSubscriber(); EventBus bus = new EventBus(); bus.register(subscriber); bus.post(EVENT); }
public void testEventOrderingIsPredictable() { EventProcessor processor = new EventProcessor(); bus.register(processor); EventRecorder recorder = new EventRecorder(); bus.register(recorder); bus.post(FIRST); assertEquals( "EventRecorder expected events in order", Lists.<Object>newArrayList(FIRST, SECOND), recorder.eventsReceived); }
public void testAnonymous() { final AtomicReference<String> holder = new AtomicReference<>(); final AtomicInteger deliveries = new AtomicInteger(); EventBus bus = new EventBus(); bus.register( new Object() { @Subscribe public void accept(String str) { holder.set(str); deliveries.incrementAndGet(); } }); String EVENT = "Hello!"; bus.post(EVENT); assertEquals("Only one event should be delivered.", 1, deliveries.get()); assertEquals("Correct string should be delivered.", EVENT, holder.get()); } }
public void testSubscriberThrowsExceptionHandlerThrowsException() throws Exception { final EventBus eventBus = new EventBus( new SubscriberExceptionHandler() { @Override public void handleException(Throwable exception, SubscriberExceptionContext context) { throw new RuntimeException(); } }); final Object subscriber = new Object() { @Subscribe public void throwExceptionOn(String message) { throw new RuntimeException(); } }; eventBus.register(subscriber); try { eventBus.post(EVENT); } catch (RuntimeException e) { fail("Exception should not be thrown."); } }
public void testBasicCatcherDistribution() { StringCatcher catcher = new StringCatcher(); bus.register(catcher); bus.post(EVENT); List<String> events = catcher.getEvents(); assertEquals("Only one event should be delivered.", 1, events.size()); assertEquals("Correct string should be delivered.", EVENT, events.get(0)); }
public void testRegisterThreadSafety() throws Exception { List<StringCatcher> catchers = Lists.newCopyOnWriteArrayList(); List<Future<?>> futures = Lists.newArrayList(); ExecutorService executor = Executors.newFixedThreadPool(10); int numberOfCatchers = 10000; for (int i = 0; i < numberOfCatchers; i++) { futures.add(executor.submit(new Registrator(bus, catchers))); } for (int i = 0; i < numberOfCatchers; i++) { futures.get(i).get(); } assertEquals("Unexpected number of catchers in the list", numberOfCatchers, catchers.size()); bus.post(EVENT); List<String> expectedEvents = ImmutableList.of(EVENT); for (StringCatcher catcher : catchers) { assertEquals( "One of the registered catchers did not receive an event.", expectedEvents, catcher.getEvents()); } }
public void testDeadEventForwarding() { GhostCatcher catcher = new GhostCatcher(); bus.register(catcher); // A String -- an event for which noone has registered. bus.post(EVENT); List<DeadEvent> events = catcher.getEvents(); assertEquals("One dead event should be delivered.", 1, events.size()); assertEquals("The dead event should wrap the original event.", EVENT, events.get(0).getEvent()); }
public void testDeadEventPosting() { GhostCatcher catcher = new GhostCatcher(); bus.register(catcher); bus.post(new DeadEvent(this, EVENT)); List<DeadEvent> events = catcher.getEvents(); assertEquals("The explicit DeadEvent should be delivered.", 1, events.size()); assertEquals("The dead event must not be re-wrapped.", EVENT, events.get(0).getEvent()); }
public void testSubscriberThrowsException() throws Exception { final RecordingSubscriberExceptionHandler handler = new RecordingSubscriberExceptionHandler(); final EventBus eventBus = new EventBus(handler); final RuntimeException exception = new RuntimeException("but culottes have a tendancy to ride up!"); final Object subscriber = new Object() { @Subscribe public void throwExceptionOn(String message) { throw exception; } }; eventBus.register(subscriber); eventBus.post(EVENT); assertEquals("Cause should be available.", exception, handler.exception); assertEquals("EventBus should be available.", eventBus, handler.context.getEventBus()); assertEquals("Event should be available.", EVENT, handler.context.getEvent()); assertEquals("Subscriber should be available.", subscriber, handler.context.getSubscriber()); assertEquals( "Method should be available.", subscriber.getClass().getMethod("throwExceptionOn", String.class), handler.context.getSubscriberMethod()); }
public void close(String indexName) { if (isReopened(indexName)) { JestUtils.execute(jestClient, new ModifyAliases.Builder(new RemoveAliasMapping.Builder(indexName, indexName + REOPENED_ALIAS_SUFFIX).build()).build(), () -> "Couldn't remove reopened alias for index " + indexName + " before closing."); } JestUtils.execute(jestClient, new CloseIndex.Builder(indexName).build(), () -> "Couldn't close index " + indexName); eventBus.post(IndicesClosedEvent.create(indexName)); }