[OmniFaces utilities] The
fireEvent()
fires the given CDI event, optionally with the given qualifiers.Method:
See also: Beans#getManager()
Usages: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. Check out the below example:
@Named
@Dependent
public class
ViningsFireStationBean {
public void updateSmallFire(@Observes String
arg) {
System.out.println("Vinings
fire department will go to a small fire at " + arg);
}
public void updateBigFire(@Observes String
arg) {
System.out.println("Vinings
fire department will go to a big fire at " + arg);
}
}
Now, a CDI managed bean can fire an event that will be observed by ViningsFireStationBean like
this:
@Named
@RequestScoped
public class
MainFireStationBean {
@Inject
Event<String> evt;
public void fireStarted(String address) {
evt.fire(address);
}
}
The above case is pretty classical. Now, if we choose to use the
OmniFaces Beans#fireEvent()
then we can re-write the MainFireStationBean
as below:
import
org.omnifaces.util.Beans;
...
@Named
@RequestScoped
public class
MainFireStationBean {
public void fireStarted(String address) {
Beans.fireEvent(address);
}
}
But, let's suppose that we need 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);
}
}
In order to re-write this case to take advantage of OmniFaces Beans#fireEvent(), we
need to need to declare an abstract qualifier that extends AnnotationLiteral and
implements FireType:
public class
FireTypeQualifier extends AnnotationLiteral<FireType> implements FireType
{
private final Type type;
public FireTypeQualifier(Type t) {
this.type = t;
}
@Override
public Type value() {
return
type;
}
}
And the MainFireStationBean
become:
import org.omnifaces.util.Beans;
...
@Named
@RequestScoped
public class
MainFireStationBean {
public void fireStarted(String address,
boolean t) {
if (t) {
Beans.fireEvent(address, new FireTypeQualifier(Type.BIG));
} else {
Beans.fireEvent(address, new FireTypeQualifier(Type.SMALL));
}
}
}
Niciun comentariu :
Trimiteți un comentariu