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>32.1.2-jre</version>
</dependency>
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
3.3. Registering Listeners
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);