In CDI 2.0 one can fire an event asynchronously by calling Event.fireAsync()
and then listen to this event by with a @ObservesAsync
annotated listener.
Why do we need both Event.firesAsync()
and @ObservesAsync
?
Event.fire()
and caught with @ObservesAsync
?Event.fireAsync()
and caught with @Observes
?A very good question indeed, here is a bit of insight.
CDI EG (expert group) decided against mixing those two for several reasons:
Event.fireAsync()
gives you a CompletionStage
to which you can chain next steps with exceptionally()
or thenApply()
etc. This naturally fits into asynchronous programming model.Event.fire()
only gives you void
, to which you cannot react at all - not good for asyncCompletionStage
, you can easily react to that.Event.fire()
(if it were for async as well)RequestScoped
needs to be re-activated, by Weld, in the async observer thread)Other advantages of the current model I can think of:
fireAsync()
allows you to fire event with additional options
NotificationOptions
which allow you to specify an executor for your notificationsfireAsync()
you have matching @ObservesAsync