EventBus post - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for EventBus post method.

This code example shows how to use the following methods:post, register, unregister
 
    @Test
    public void shouldNotReceiveEventAfterUnsubscribe() throws Exception {
 
        // given 
        EventBus eventBus = new EventBus("test");
        EventListener listener = new EventListener(); 
        EventReader reader = new EventReader();
 
        eventBus.register(listener);
        eventBus.register(reader);
 
        // when 
        eventBus.post(new OurTestEvent(200));
 
 
        // then 
        assertThat(reader.getLastMessage()).isEqualTo(200);
        assertThat(listener.getLastMessage()).isEqualTo(200); 
         

7
CodeRank
This code example shows how to use the following methods:post
 
    @Override 
    public void process(Exchange exchange) throws Exception {
        Object body = exchange.getIn().getBody();
        if (body != null) { 
            log.debug("Posting: {} to EventBus: {}", body, eventBus);
            eventBus.post(body);
        } else { 
            log.debug("Body is null, cannot post to EventBus"); 
        } 
    } 
 
} 
7
CodeRank
This code example shows how to use the following methods:post
 
    private ChunkMonitor() { 
    } 
 
    private static void post(Object event) { 
        EVENT_BUS.post(event);
    } 
 
    private static synchronized ChunkMonitorEntry registerChunk(Chunk chunk) {
        Preconditions.checkNotNull(chunk, "The parameter 'chunk' must not be null");
        final Vector3i pos = chunk.getPosition();
        ChunkMonitorEntry entry = CHUNKS.get(pos);
        if (entry == null) {
            entry = new ChunkMonitorEntry(pos);
            CHUNKS.put(pos, entry);
        } 
        entry.addChunk(chunk);
        return entry;
    } 
 
6
CodeRank
This code example shows how to use the following methods:post, register
   * it can fail here. 
   */ 
  public void testAnonymous() { 
    final AtomicReference<String> holder = new AtomicReference<String>();
    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());
  } 
} 
6
CodeRank
This code example shows how to use the following methods:post
        this.eventStore = eventStore;
    } 
 
    @Override 
    public void publishEvent(DomainEvent event) {
        eventBus.post(event);
        eventStore.append(event);
    } 
 
    @Override 
    public void registerSubscriber(Object subscriber) { 
        eventBus.register(subscriber);
    } 
 
    @Override 
    public void unregisterSubscriber(Object subscriber) { 
        eventBus.unregister(subscriber);
    } 
} 
6
CodeRank
This code example shows how to use the following methods:post, register
    * WARNING: Caller is responsible for also unsubscribing to avoid leaking memory. 
    * 
    * @param observer 
    */ 
   public void subscribe(Object observer) { 
      eventBus.register(observer);
      // Sends current state directly to listener when subscribing to sync up. 
      eventBus.post(new InstancerStateChange(state, state, self));
   } 
 
   public void unsubscribe(Object observer) { 
      eventBus.unregister(observer);
   } 
 
   protected void setState(Instancer.State state) {
      InstancerStateChange change = new InstancerStateChange(this.state, state, self);
      this.state = state;
      eventBus.post(change);
   } 
} 
6
CodeRank
This code example shows how to use the following methods:post
	public Publisher(EventBus eventBus) {
		this.eventBus = eventBus;
	} 
 
	public void publishMsgEvent(String message) {
		eventBus.post(new MsgEvent(message));
	} 
 
	public void publishSpecialMsgEvent(String message, String user) {
		eventBus.post(new SpecialMsgEvent(message, user));
	} 
 
	public void publishTimeEvent() { 
		eventBus.post(new TimeEvent(new Date()));
	} 
 
} 
5
CodeRank
This code example shows how to use the following methods:post
    } 
  } 
 
  @Override 
  public void onReadError(int error) {
    eventBus.post(new AudioReaderErrorEvent());
  } 
} 
5
CodeRank
This code example shows how to use the following methods:post
 
	@Override 
	public Optional<DisplayEvent> handle(final xcb_generic_event_t event_t) {
		final xcb_generic_error_t request_error = new xcb_generic_error_t(	xcb_generic_event_t.getCPtr(event_t),
																			false); 
		this.xEventBus.post(request_error);
 
		throw new RuntimeException(XcbErrorUtil.toString(request_error));
		// TODO error event? 
	} 
 
	@Override 
	public Optional<AsyncListenable> getTarget(final xcb_generic_event_t event_t) {
		// TODO return display server? 
		return Optional.absent();
	} 
 
	@Override 
	public Integer getEventCode() {
		return this.eventCode;
5
CodeRank
This code example shows how to use the following methods:post
    void process(byte[] packet, int index)
    { 
      int heartRate = Math.abs(packet[12 + index]);
      SensorEvent event = heartRateEvent(heartRate);
      event.setAddress(address);
      eventBus.post(event);
    } 
 
    private SensorEvent heartRateEvent(int heartRate)
    { 
      return new SensorEvent("Zephyr", "Zephyr HxM", "Heart Rate", "HR", "beats per minute", "bpm", 40, 85, 130, 175, 220, heartRate);
    } 
  } 
 
  public void setBus(EventBus eventBus)
  { 
    this.eventBus = eventBus;
  } 
} 
5
CodeRank
post Method Overview
Posts an event to all registered subscribers.
See Code Examples for other Guava EventBus Methods: