CDI - 当观察者观察到事件时是否通知调用者?
我正在使用 CDI,想知道如何通知调用者观察者是否观察到事件。如果没有观察者对该事件采取行动,那么我想做点什么。除了通知调用者的提示之外,我没有看到文档中的任何地方记录了这一点。
谢谢,
沃尔特
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为调用者不会收到通知(这不是观察者模式实际上的目的)。但您可以尝试通过以下方式解决此问题:
您可以在事件对象上设置一个字段 -
private boolean Consumer
并在使用时将其设置为true
。然后,(事件同步处理)在事件生成器中检查该变量。从事件生产者观察到的观察者激发一个新事件
如果您想放弃松散耦合的好处,并且您确切地知道事件生产者是谁,您可以将其
@Inject
到所有侦听器中,并让它们调用其上的方法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 totrue
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