Search on blog

Petition by Java EE Guardians

Twitter

vineri, 17 iunie 2016

CDI-JSF: Using the CDI @Observes

In this post we will discuss about using the CDI @Observes in a JSF application.

Basically, we will exploit the fact that Java EE provides an easier implementation of the observer design pattern via the @Observes annotation and javax.enterprise.event.Event<T> interface.

Note In the bellow examples, we will use CDI managed beans, but you can use EJB 3 beans also.

Basically, the observer pattern is based on a subject and some observers:subject - an object that changes its state

observers - objects notified when the subject has changed its state

This time, the subject is a CDI managed bean named, MainFireStationBean:

@Named

@RequestScoped

public class MainFireStationBean {

@Inject

Event<String> evt;

public void fireStarted(Stringaddress) {

evt.fire(address);

}

}

The container injects an Event object of type String into the evt instance variable of the MainFireStationBean class (practically, this String represents the fire address). To activate an event, call the javax.enterprise.event.Event.fire() method. This method fires an event and notifies any observer methods (observers). Now the observable part is completed, so it is time to create the observers that listens for our String events.

In Java EE the observers are marked with the @Observes annotation. The addition of the @Observes annotation to the method signature instructs the container that this method should act as an observer of events of the type it precedes.

We have three observers, ViningsFireStationBean , BrookhavenFireStationBean and DecaturFireStationBean:

@Named

@Dependent

public class ViningsFireStationBean {

public void update(@Observes String arg) {

System.out.println("Vinings fire department will go to " + arg);

}

}

@Named

@Dependent

public class BrookhavenFireStationBean {

public void update(@Observes String arg) {

System.out.println("Brookhaven fire department will go to " + arg);

}

}

@Named

@Dependent

public class DecaturFireStationBean {

public void update(@Observes String arg) {

System.out.println("Decatur fire department will go to " + arg);

}

}

So, the @Observes annotation precedes the type String and thus listens for events of that type. The @Observes annotation followed by an object type instruct the container will all the needed information.

In order to test it, we just need to report some fires. We can do this in several ways, but let's do it quickly via two JSF buttons:

If we suppose that the Report Fire at Home Park Atlanta button was pressed then the output will be:

Decatur fire department will go to Home Park Atlanta, GA

Vinings fire department will go to Home Park Atlanta, GA

Brookhaven fire department will go to Home Park Atlanta, GA

So, everything works as expected. The complete example is available here.

One step further and we will want to differentiate between the same object types of objects and set up different observers to listen for them. For example, we may need to distinguish between small fires and big fires. Depending on this aspect, a local fire station may send to the fire address one fire truck or multiple fire trucks. We can model this case via a qualifier:

@Qualifier

@Retention(RetentionPolicy.RUNTIME)

@Target({ElementType.FIELD, ElementType.PARAMETER})

public @interface FireType {

Type value();

enum Type {

SMALL, BIG

}

}

The two enum types (SMALL and BIG) will be used to act as annotation to mark the strings to be fired by the event instances. So, the MainFireStationBean will be:

Abonaţi-vă la JSF/OmniFaces Fans

Visitors Starting 4 September 2015

Others

[OmniFaces utilities]

Renderers

Collection of utility methods for the JSF API with respect to working with Renderer

Platform

This class provides access to (Java EE 6) platform services from the view point of JSF

Map Wrapper

Implementation of Map that wraps another map. This allows interception of one or more method on this wrapped map

JNDI

Utility class for simplifying some web related tasks that use JNDI under the hood, such as getting the <env-entry> from web.xml

State

Helper class for StateHelper that uses generic type-inference to make code that uses the StateHelper slightly less verbose

Beans

Collection of utility methods for the CDI API that are mainly shortcuts for obtaining stuff from the BeanManager

Components

Collection of utility methods for the JSF API with respect to working with UIComponent. There are several traversal/lookup methods, there are several UIForm and UIInput related methods which makes it easier to deal with forms and inputs

.Events

Collection of utility methods for the JSF API with respect to working with system and phase events