CDI Events
CDI events allow your application components to communicate with each other in a loosely coupled manner. Those more familiar with JMS can also think of CDI events as a Synchronous equivalent of JMS (with Java EE 8, CDI 2.0 will offer asynchronous event mechanism).
Event Qualifiers
Events can be enriched using qualifiers. Think of these as message selectors (the concept is not very different from Message Selectors in JMS).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//firing a (statically) qualified event | |
@Inject | |
@Approved | |
private Event<RequestOutcome> outcomeEvent; | |
public void notify(){ | |
outcomeEvent.fire(outcomeEvent.getOutcome()); | |
} | |
//observers | |
public void handleApprovedRequest(@Observes @Approved RequestOutcome approvedOutcome){ | |
//logic… | |
} | |
public void handleRejectedRequest(@Observes @Rejected RequestOutcome rejectedOutcome){ | |
//logic… | |
} |
Using Dynamic CDI event qualifiers
The above example demonstrates the static way of declaring qualifiers. CDI also provides a more dynamic version of the same feature (qualifier declaration).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Inject | |
private Event<RequestOutcome> outcomeEvent; //no qualifier specified | |
public void notify(){ | |
RequestOutcome outcome = outcomeEvent.getOutcome(); | |
Annotation qualifier = null; | |
//determine event qualifier dynamically | |
if(outcome == Outcome.APPROVED){ | |
qualifier = new Approved(); | |
} | |
else if(outcome == Outcome.REJECTED){ | |
qualifier = new Rejected(); | |
} | |
//fire the (dynamically qualified) event | |
outcomeEvent.select(qualifier).fire(outcome); | |
} |
Please note that you can specify multiple qualifiers with this mechanism
Other options
Although not discussed here, the Event interface also exposes couple of other methods to allow dynamic qualifier selection.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public <U extends T> Event<U> select(Class<U> subtype, Annotation… qualifiers); | |
public <U extends T> Event<U> select(TypeLiteral<U> subtype, Annotation… qualifiers); |
The benefits are obvious…
The select method (and its overloaded counterparts) provide a flexible way of handling qualified events and helps avoid proliferation of injected Event instances for specific set of qualifiers.
Cheers!
Pingback: Dynamic event qualifiers in CDI | Dinesh Ram Kali.
Pingback: Java Weekly 1/16: GC Overhead, Monitor Microservices, CDI