Search code examples
javaobserver-patterncdi

CDI - is the caller notified when an observer observes an event?


I am using CDI and want to know how the caller is notified that the observer has observed an event or didn't. If no observes act on that event, then I want to do something. I don't see this being documented anywhere in the documentation other than there was a hint that the caller is notified.

Thanks,

Walter


Solution

  • I don't think the caller is notified (it's not what the observer pattern is about actually). But you can try to work this around by:

    • you can have a field one the event object - private boolean consumed and set it to true if it is consumed. Then, (the events are handled synchronously) in the event-producer check that variable.

    • firing a new event from the observers that is observed by the event producer

    • If you want to drop the benefit of the loose coupling, and you know exactly which the event producer is is, you can @Inject it into all listeners, and let them invoke a method on it