Java EE 6 – Observer Pattern


The Observer Pattern has been one of the most widely used design pattern in Software Development. If not most, many use the pattern for their development to call different independent modules to do processes after the initial action from another module. Certain scenarios like, a display table was updated after an insert call, a window resize after a clicked of a button.

JEE6 introduced a more easy way of using the observer pattern.

Assuming that you have a ManagedBean, with a register transaction method.

Observe this:

  1. Event<?> will be the one responsible for storing entity or object to be observed.
  2. A fire method is called passing the entity. (memberEventSrc.fire(newMember));

Once the fire event method is called, it will search for all ManagedBean observing this entity.

Observer this:

  1. @Observe annotation is used for the final Member class in the method, this is our flag for the method call
  2. This is located on the ManagedBean – We need a POJO that is class loaded for this process.

The MessageProducerList class is also a ManagedBean that is called after a transaction is made. The fire event from the MemberRegistration ManagedBean will call the method (on the screenshot) to load all the members. These list of members is then loaded to the JSF Page.

Download Source Code
Note: You need Eclipse EE with JBoss Tools installed.

Enjoy! 🙂

2 thoughts on “Java EE 6 – Observer Pattern

  1. Pingback: JavaPins
  2. I know, Java and NetBeans community will definitely attend this prestigious nd great conference this year too…..
    You all can Sign up here http://bit.ly/YMPeJ8 to find out more details and launch date for the 2013 JavaOne. JavaOne India 2013 goin to happen on 8-9 May in Hyderabad at the same place as last year guys….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s