CDI allows you to extend business code by the Notifier/Observer pattern.
To do it you simply inject a field
Event in the notifier class. This class is a template
and the parameter type is the object type to fire. Then when you want to notify observers
you simply call the method fire of the event object passing as a parameter the event itself
(your own class!).
On the other side you annotated a method parameter
@Observes and the parameter type is the sent type
by the notifier.
Note: of course you can add qualifiers to be more precise on your events.
The example is pretty simple: an ejb uses the
@Schedule annotation to get a notification each second.
The each second this EJB will send the current date through CDI events.
This is our "business" code. It is a simple behavior (nothing).
In our test (which is considered as an extension) we created an observer (
which simply store and log each received date.
The test itself (
EventTest) simply verifies the dates were received.
CDI let's implement very easily plugins through this event mecanism.
If you go further and look at CDI plugin API you'll realize it is simply the same kind of events. CDI events is really the basis of CDI.
svn co http://svn.apache.org/repos/asf/tomee/tomee//examples/cdi-events cd cdi-events mvn clean install
All edits are reviewed before going live, so feel free to do much more than fix typos or links. If you see a page that could benefit from an entire rewrite, we'd be thrilled to review it. Don't be surprised if we like it so much we ask you for help with other pages :)NOTICE: unless indicated otherwise on the pages in question, all editable content available from apache.org is presumed to be licensed under the Apache License (AL) version 2.0 and hence all submissions to apache.org treated as formal Contributions under the license terms.