I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE COURSE

1. Overview

Guava library provides the EventBus which allows publish-subscribe communication between components. In this tutorial, we will look at how to use some of the features of the EventBus.

2. Setup

To start we add the Google Guava library dependency in the pom.xml:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>21.0</version>
</dependency>
 The latest version can be found here.

3. Using the EventBus

Let’s start by using a simple example.

3.1. Setup

We start by looking at the EventBus object. It can register listeners and post events. Using it is as simple as instantiating the class:

EventBus eventBus = new EventBus();

Guava library gives you the freedom of using the EventBus in any way that best suits your development needs.

3.2. Creating Listeners

We create a listener class that has handler methods to receive specific events. We annotate the handler methods with @Subscribe. The method accepts as an argument an object of the same type as the event being posted:
public class EventListener {

    private static int eventsHandled;

    @Subscribe
    public void stringEvent(String event) {
        eventsHandled++;
    }
}

3.3. Registering Listeners

We can subscribe to an event by registering our EventListener class on the EventBus:
EventListener listener = new EventListener();
eventBus.register(listener);

3.4. Unregistering Listeners

If for any reason we want to unregister a class from the EventBus, that can also be easily done:

eventBus.unregister(listener);

3.5. Posting Events

We can post events as well with the EventBus:
@Test
public void givenStringEvent_whenEventHandled_thenSuccess() {
    eventBus.post("String Event");
    assertEquals(1, listener.getEventsHandled());
}

3.6. Posting Custom Events

 We can also specify a custom event class and post that event. We start by creating a custom event:
public class CustomEvent {
    private String action;

    // standard getters/setters and constructors
}

Adding a handler method in the EventListener class for that event:

@Subscribe
public void someCustomEvent(CustomEvent customEvent) {
    eventsHandled++;
}

We can now post our custom event:

@Test
public void givenCustomEvent_whenEventHandled_thenSuccess() {
    CustomEvent customEvent = new CustomEvent("Custom Event");
    eventBus.post(customEvent);

    assertEquals(1, listener.getEventsHandled());
}

3.7. Handling an Unsubscribed Event

We are provided with a DeadEvent class that allows us to handle any events that have no listeners. We can add a method to handle the DeadEvent class:

@Subscribe
public void handleDeadEvent(DeadEvent deadEvent) {
    eventsHandled++;
}

4. Conclusion

In this tutorial, we used a simple example as a guide on how to use the Guava EventBus.

You can find the complete source code and all code snippets for this article over on GitHub.

I just announced the new Spring 5 modules in REST With Spring:

>> CHECK OUT THE LESSONS

Sort by:   newest | oldest | most voted
Francois Marot
Guest
One thing is particularly important regarding the default Guava implementation: when you’re in code resulting directly from handling an event (I mean, in the stack trace, you’re treating event A even though you may be a few methods up the stack), if you send another event (call it B) then this event will not be treated right now: event B and the code handling this event will only be executed when event A is finished. Do NOT mistake it for threading stuffs, there is no threading involved. It’s just an event queue. Why do I bother posting a message here:… Read more »
Stephen Braimah
Guest

Great comment!!! Thanks