Java EE Revisits Design Patterns: Observer

Aside from being implemented in many languages and many applications, Observer Pattern has been a part of Java since version 1.0. Observer Pattern is also a good implementation of Hollywood Principle. Just like Agents in Hollywood like to callback the candidates for a role instead of being called daily to be asked about available jobs, most of the server side resources like the push the available data to the appropriate client instead of being asked for updates on a time interval.

Such time interval queries can be both consuming to much resource on the server and also cause more network traffic than actually needed. Although Java had support for Observer Pattern since day 0, it is always argued to be not the best implementation (Have a look at Observer and Observable). Being on JavaEE world may even complicate things. However JavaEE6 comes with an alternative.

JavaEE6 offers ‘@Observable’ annotation as an easy out of box implementation of Observer Pattern. Lets visit the previous post and extend it to use observers.

package com.devchronicles.observer;


import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.enterprise.event.Event;
import javax.inject.Inject;


/**
 *
 * @author Murat Yener
 */
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class EventService {
    @Inject
    private String message;
    
    @Inject 
    Event<String> event;
    
    public void startService(){
        System.out.println("start service call "+message);
        event.fire("this is my "+message);
        System.out.println("done...");
    }
}

The EventService class will be injected an Event object of type String which can be used to fire String objects. If you had not read the previous post, message object is a String which will be produced by a factory and injected to the EventService class. To make it simpler you can just type any string constant to the variable called message.

Since we are already done with the observable part, now it is time to create an observer to listen our events.

package com.devchronicles.observer;


import javax.ejb.Stateless;
import javax.enterprise.event.Observes;


/**
 *
 * @author Murat Yener
 */
@Stateless
public class EventObserver {


    public void doLogging(@Observes String message){
        System.out.println("Observed:"+message);
    }
}

The Observes annotation marks the method as an observer for fired String events. If you run the server and fire up start service method, you will realize how magically a string will be injected to EventService class and than fired where it will be caughed (observed) by EventObserver class. Surprisingly that is all you need to implement the observer pattern in JavaEE6.

As seen before, JavaEE6 offers an easy way to implement the Observer Pattern. After publishing the post, I had receive few questions on how to differentiate string types that are fired and observed.

Although in real world scenarios you wouldn’t probably firing and observing plain strings but your own objects which would be observed by their type, still it is pretty easy to differentiate same type of objects and setup different observers to listen them.

First lets start with the part to differentiate plain strings.

package com.devchronicles.observer;


import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;


/**
 *
 * @author Murat Yener
 */
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.PARAMETER})
public @interface MyEvent {
    
    Type value();
    
    enum Type{
        LOGGING, MESSAGE
    } 
}

The interface above will act as annotation to mark the string to be fired and later to be observed just by annotating the appropriate parts.

package com.devchronicles.observer;


import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.enterprise.event.Event;
import javax.inject.Inject;


/**
 *
 * @author Murat Yener
 */
@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class EventService {
    @Inject
    private String message;
    
    @Inject @MyEvent(MyEvent.Type.LOGGING)
    Event<String> event;


    @Inject @MyEvent(MyEvent.Type.MESSAGE)
    Event<String> anotherEvent;
    
    public void startService(){
        System.out.println("start service call "+message);
        event.fire("this is my "+message);
        System.out.println("done...");
        anotherEvent.fire("done with the service!");
    }
}

We just add MyEvent annotation with the desired type and later fire the events as we did before. The parts marked with red is all we added to the example in the previous post.

Now lets annotate the observer part. Again We will be just adding the red parts to the previous example.

package com.devchronicles.observer;

import javax.ejb.Stateless;
import javax.enterprise.event.Observes;

/**
 *
 * @author Murat Yener
 */
@Stateless
public class EventObserver {

    public void doLogging(@Observes @MyEvent(MyEvent.Type.LOGGING) String message){
        System.out.println("Observed:"+message);
    }



    public void doLogging(@Observes @MyEvent(MyEvent.Type.MESSAGE) String message){
        System.out.println("Observed another type of message:"+message);
    }
}

That would be all you would need to differentiate even the same type of objects to observe.

Reference: JavaEE Revisits Design Patterns: Observer , JavaEE Revisits Design Patterns: Observer Part 2 from our JCG partner Murat Yener at the Developer Chronicles blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

One Response to "Java EE Revisits Design Patterns: Observer"

  1. Noah White says:

    You show one EJB which is the Event producer and another  EJB which is the Event consumer. Normally if the producer startService method called the consumer’s doLogging method directly the Transaction context would automatically propagate. Do TX contexts propagate when the consumer class’s method is called as the result of the Observable event or is that method in its own separate TX context and the original one is closed when the producer method exits? I am guessing they are not.

    Are observers called asynchronously? If so would it not be possible that the Observer code is called before the producer method finishes and its TX is closed/flushed? If that’s the case then one has to be careful that the code in their consumer method does not rely on the state of the producers TX being completed first.  

Leave a Reply


1 + = nine



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
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.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books