Ilias Tsagklis

About Ilias Tsagklis

Ilias Tsagklis is a senior software engineer working in the telecom domain. He is an applications developer in a wide variety of applications/services. Ilias is co-founder and Executive Editor at Java Code Geeks.

Java EE6 Events: A lightweight alternative to JMS

The feature I’m going to talk about today is the event mechanism that is in java EE 6. The general idea is to fire an event and let an event listener pick it up.

I have created this example that is totally useless, but it simplicity helps me to focus on the important stuff. I’m going to fire a LogEvent from my backing action, that will log to the java.util.Logger.

The first thing I need is to create a POJO that contains my log message and my LogLevel.

public class LogMessage implements Serializable {
 
    private final String message;
    private final Level level;
 
    LogMessage(String message, Level level) {
        this.message = message;
        this.level = level;
    }
 
    public String getMessage() {
        return message;
    }
 
    public Level getLevel() {
        return level;
    }
}

Now that I have my data wrapper, I need something to fire the event and something to pick it up. The first thing I create is my method where I fire the event.

Due to CDI I can inject an event.

@Inject Event<LogMessage> event;

So we just need to fire it.

event.fire(new LogMessage("Log it baby!", Level.INFO));

Now the event is fired, if no one is registerd to pick it up, it disappears into oblivion, thus we create a listener. The listeners needs a method that has one parameter, the generic type that is given to the previous event. LogMessage.

public class LogListener {
    private static final Logger LOGGER = Logger.getAnonymousLogger();
    public void process(@Observes LogMessage message){
        LOGGER.log(message.getLevel(), message.getMessage());
    }
}

The @Observes annotation listens to all events with a LogMessage. When the event is fired, this method will be triggered.

This is a very nice way to create a loosely coupled application, you can separate heavy operations or encapsulate less essential operations in these event listeners.

All of this all happens synchronously. When we want to replace the log statement with a slow database call to a logging table, we could make our operation heavier than it should be.

What I’m looking for is to create an asynchronous call. As long as we support EJB, we can transform our Listener to an EJB by adding the @Stateless annotation on top of it. Now it’s a statless enterprise bean. This changes nothing to our sync/async problem, but EJB 3.1 support async operations. So if we also add the @Asynchronous annotation on top of it. It will asynchronously execute our logging statement.

@Stateless
@Asynchronous
public class LogListener {
    private static final Logger LOGGER = Logger.getAnonymousLogger();
    public void process(@Observes LogMessage message){
        LOGGER.log(message.getLevel(), message.getMessage());
    }
}

If we would want to combine the database logging and the console logging, we can just create multiple methods that listen to the same event.

This is a great way to create a lightweight application with a very flexible components. The alternative solution to this problem is to use JMS, but you don’t want a heavyweight configuration for this kind of loosely coupling.

Reference: Java EE6 Events, a lightweight alternative to JMS from our JCG partner Jelle Victoor at the Styled Ideas Blog.

Related Articles :

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "Java EE6 Events: A lightweight alternative to JMS"

  1. I was expecting a comparison of the two technologies, JMS & CDI Event mechanism, but you didnt compare or justify why you think CDI Event mechanism is a better or lightweight alternative to JMS. I am a huge fan of JMS and would really like you to clarify these few things:
    1. Can another EJB module (or a separate application entirely) in the same server or server cluster listen to a CDI event?
    2. Can CDI events be made persistent?
    3. If the the listening application is down for maintenance after having registered for an event, will it get the information when its back in operation?
    These among other things were the criteria i was expecting you to use to justify your topic “Java EE6 Events: A lightweight alternative to JMS”.

Leave a Reply


× 6 = thirty



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close