My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

marți, 8 septembrie 2015

JSF and Observer design pattern - part II (Java EE)

Read also:
JSF and Observer design pattern - part I (plain code)

In this part, we will "transform" the example presented in JSF and Observer design pattern - part I from a plain Java code example into a Java EE example. Moreover, we will place it in a JSF application context. 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.

Remember that in our plain code example the subject was named, MainFireStation. This time we will "transform" this class into a CDI managed bean named, MainFireStationBean:

@Named
@RequestScoped
public class MainFireStationBean {

 @Inject
 Event<String> evt;

 public void fireStarted(String address) {
  evt.fire(address);
 }
}

Notice that this time we have the method for reporting fires (fireStarted()), but we dropped the method for registering new observers and the method for notifying local fire stations about the reported fires. Basically, we don't need those methods anymore thanks to the container which will supply these tasks automatically. This time, 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. Remember that in our plain code we had three observers, ViningsFireStation, BrookhavenFireStation and DecaturFireStation. This time we "transform" these classes into CDI managed beans, as below:

@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:

<h:form>
 <h:commandButton value="Report Fire at Home Park Atlanta" 
                  action="#{mainFireStationBean.fireStarted('Home Park Atlanta, GA')}"/>
 <h:commandButton value="Report Fire at High Museum of Art" 
                  action="#{mainFireStationBean.fireStarted('High Museum of Art 1280 Peachtree St NE Atlanta, GA 30309')}"/>
</h:form>

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.

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:

@Named
@RequestScoped
public class MainFireStationBean {

 @Inject
 @FireType(Type.SMALL)
 Event<String> small;

 @Inject
 @FireType(Type.BIG)
 Event<String> big;

 public void fireStarted(String address, boolean t) {
  if (t) {
      small.fire(address);
  } else {
      big.fire(address);
  }
 }
}

Finally, add the annotations to the observer part:

@Named
@Dependent
public class ViningsFireStationBean {

 public void updateSmallFire(@Observes @FireType(FireType.Type.SMALL) String arg) {
  System.out.println("Vinings fire department will go to a small fire at " + arg);
 }

 public void updateBigFire(@Observes @FireType(FireType.Type.BIG) String arg) {
  System.out.println("Vinings fire department will go to a big fire at " + arg);
 }
}

@Named
@Dependent
public class BrookhavenFireStationBean {

 public void updateSmallFire(@Observes @FireType(FireType.Type.SMALL) String arg) {
  System.out.println("Brookhaven fire department will go to a small fire at " + arg);
 }

 public void updateBigFire(@Observes @FireType(FireType.Type.BIG) String arg) {
  System.out.println("Brookhaven fire department will go to a big fire at " + arg);
 }
}

@Named
@Dependent
public class DecaturFireStationBean {

 public void updateSmallFire(@Observes @FireType(FireType.Type.SMALL) String arg) {
  System.out.println("Decatur fire department will go to a small fire at " + arg);
 }

 public void updateBigFire(@Observes @FireType(FireType.Type.BIG) String arg) {
  System.out.println("Decatur fire department will go to a big fire at " + arg);
 }
}

Now, let's report a big fire and a small fire:

<h:form>
 <h:commandButton value="Report a Small Fire at Home Park Atlanta" 
                  action="#{mainFireStationBean.fireStarted('Home Park Atlanta, GA', true)}"/>
 <h:commandButton value="Report a Big Fire at High Museum of Art" 
                  action="#{mainFireStationBean.fireStarted('High Museum of Art 1280 Peachtree St NE Atlanta, GA 30309', false)}"/>
</h:form>

In case of a small fire the output will be:

Brookhaven fire department will go to a small fire at Home Park Atlanta, GA
Vinings fire department will go to a small fire at Home Park Atlanta, GA
Decatur fire department will go to a small fire at Home Park Atlanta, GA

Note that in case of your own object types you don't need qualifiers. Since the object type is unique, you can fire/observe your own object types by using the object.

In part three we discuss about the observer pattern in JSF implementation.

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors