Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

Generic Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring 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:

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;

    public void stringEvent(String event) {

3.3. Registering Listeners

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

3.4. Unregistering Listeners

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


3.5. Posting Events

We can post events as well with the EventBus:
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:

public void someCustomEvent(CustomEvent customEvent) {

We can now post our custom event:

public void givenCustomEvent_whenEventHandled_thenSuccess() {
    CustomEvent customEvent = new CustomEvent("Custom Event");

    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:

public void handleDeadEvent(DeadEvent deadEvent) {

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.

Generic bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

Generic footer banner
Comments are closed on this article!