February 27, 2021
Hot Topics:

Implement Automatic Discovery in Your Java Code with Annotations

  • By Jason Morris
  • Send Email »
  • More Articles »

Putting It All Together

The EventBus project results in a single JAR file with both the compile-time and runtime code (although you could break it into two JARs). Now you need to write a sub-class of BusEventObject that can be dispatched to listeners though the EventBus. You'll also need an @EventListener method to receive instances of your new event class. Finally: you'll need a class to dispatch the events from (a source).

To verify that the @EventListener method has an EventDispatcher generated, you'll need to make sure that the compiler knows to run the EventListenerAnnotationProcessor. This process varies from one IDE to another, but simply verifying that the JAR is on the classpath or in your project libraries often is enough. In some IDEs (such as Eclipse), you need to register the annotation processor by hand. For this example, a MessageEvent class will be dispatched through the event bus:

public class MessageEvent extends BusEventObject {
    private final String message;
    // constructor, etc.
    public String getMessage() {
        return message;

You'll need an @EventListener to pick up the MessageEvent objects and do something with them. Remember that you can do this in any class, as long as the annotation processor is given a chance to see the code. For this example, this code opens a JOptionPane with the message.

public static void onMessage(final MessageEvent event) {
            "Message Event",

This example listener MessageListener is a catch-all listener that will receive all MessageEvent objects dispatched through the event bus.

The only thing left to do is dispatch a MessageEvent through the EventBus:

EventBus.dispatch(new MessageEvent(
        "Hello World!"));

The MessageEvent constructor takes the source of the event, the event name, and the message. This event will go to any number of methods annotated by @EventListener and accepting a MessageEvent as their parameter.

Where's the Output?

When you've compiled the code, take a look at the build directory. Next to each class file with an @EventListener method, there should be a *EventDispatcher Java source file and the resulting class file. If those files aren't there, make sure that you've set up your build environment so that the EventListenerAnnotationProcessor is visible to the compiler and annotation processing tools (refer back to the "Organizing Your Code" section).

Hopefully, you'll find that using annotations with the ServiceLoader will make your life easier.

Code Download

For Further Reading

About the Author

Jason Morris is the IS architect for a company named Travelstart Services. When he's not coding or coming up with new ways to tackle development problems, he's working on his lightweight O/R mapping layer: EoD SQL; or blogging technical ideas and rants on lemnik.wordpress.com. Reach him at jason.morris@travelstart.net.

Page 3 of 3

This article was originally published on December 16, 2009

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date