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

vineri, 27 februarie 2015

[OmniFaces utilities (2.1/2.0)] Subscribe to application system events using serializable/un-serializable Callbacks


[OmniFaces utilities] The subscribeToApplicationEvent() methods subscribes the given callback (serializable (in 2.1) /un-serializable (in 2.0) void without arguments/void with one argument) to the current application that get invoked every time when the given system event type is published in the current application.

Method:

- using OmniFaces 2.1 serializable callback

- using OmniFaces 2.0 un-serializable callback

Note By OmniFaces implementation, the listened emitter of system events is the UIViewRoot, so use only system events types that can be emitted by the component tree's view root. OmniFaces relies on its org.omnifaces.eventlistener.DefaultViewEventListenerlisted below:

public abstract class DefaultViewEventListener implements SystemEventListener {
 @Override
 public void processEvent(SystemEvent event) throws AbortProcessingException {
  // NOOP
 }

 @Override
 public boolean isListenerForSource(Object source) {
  return source instanceof UIViewRoot;
 }
}

Usage:

Before calling the Events#subscribeToApplicationEvent() methods, let's have a quick overview of how this methods works. As the post title said, this method allows to subscribe the given serializable (in 2.1)/un-serializable (in 2.0) callback to the current application that get invoked every time when the given system event type is published in the current application. So, we pass to this method the system event type to be observed, and one of the following OmniFaces Callbacks (this becomes the listener):

- using OmniFaces 2.1 serializable callback

- using OmniFaces 2.0 un-serializable callback

Basically, all we need to do is to subscribe to the desired event(s) with a Callback instance and implement a behavior to the corresponding invoke() method. Behind the scene, OmniFaces takes the provided Callback and creates a custom system event listener for it. When the specified event(s) occurs, OmniFaces will call our invoke() implementation. Knowing this, we can create a simple example. Let's suppose that we have a custom component that  needs to know when the view root was just added to the view. For this, the custom component can subscribe with a Callback to the PostAddToViewEvent event, as below:

import org.omnifaces.util.Callback;
import static org.omnifaces.util.Events.subscribeToApplicationEvent;
...
//OmniFaces 2.1
// serializable void Callback with argument
subscribeToApplicationEvent(PostAddToViewEvent.class, new Callback.SerializableWithArgument<SystemEvent>() {

 private static final long serialVersionUID = 1L;

 @Override
 public void invoke(SystemEvent event) {
  System.out.println("..: PostAddToViewEvent event emitted by UIViewRoot :.." + event.getSource());
  //do something ...
 }
});

//OmniFaces 2.0
// un-serializable void Callback with argument
subscribeToApplicationEvent(PostAddToViewEvent.class, new Callback.WithArgument<SystemEvent>() {
 @Override
 public void invoke(SystemEvent event) {
  System.out.println("..: PostAddToViewEvent event emitted by UIViewRoot :.." + event.getSource());
  //do something ...
 }
});

// OmniFaces 2.1
// serializable void Callback
subscribeToApplicationEvent(PostAddToViewEvent.class, new Callback.SerializableVoid() {
 @Override

 private static final long serialVersionUID = 1L;

 public void invoke() {
  System.out.println("..: PostAddToViewEvent event emitted by UIViewRoot :..");
  //do something ...
 }
});

// OmniFaces 2.0
// un-serializable void Callback
subscribeToApplicationEvent(PostAddToViewEvent.class, new Callback.Void() {
 @Override
 public void invoke() {
  System.out.println("..: PostAddToViewEvent event emitted by UIViewRoot :..");
  //do something ...
 }
});
...


Complete code on GitHub
API 2.1   GH 2.1

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors