当打包为EAR时,是否可以在WAR内观察CDI事件 [英] Is it possible to observe a CDI Event inside a WAR when packaged as EAR

查看:125
本文介绍了当打包为EAR时,是否可以在WAR内观察CDI事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个企业应用程序归档(EAR),其中包含多个后端模块(EJB)以及一些Web模块(WAR)。

I have an Enterprise Application Archive ( EAR ) containing multiple backend modules ( EJB ) as well as some web modules ( WAR ).

事件在一个内部被触发后端模块:

The Event gets fired inside one of the backend modules:

@Inject private Event<MyEvent> myEvent;
...
public void fireEvent() {
  myEvent.fire(new MyEvent());
}
...

任何其他的都可以观察到后端模块,代码如下:

It can be observed in any of the other backend modules with code like this:

public void listener(@Observes MyEvent myEvent) {
..
}

但我无法在WAR中检索事件。这是因为类加载器可见性(来自WAR的类对EJB不可见)还是CDI应该处理这个?

But I can't retrieve the event inside the WARs. Is this because of classloader visibility (classes from WAR are not visible to EJBs) or should CDI handle this?

如果CDI不能用于应用程序范围的事件,那么是替代方案吗?

If CDI can't be used for application wide events, what are the alternatives?


  • JMS

  • 番石榴EventBus

  • ...

有什么与CDI有效吗?也许某些CDI扩展将事件连接到WAR?

Is there anything that works with CDI? Maybe some CDI extension that bridges the events into the WARs?

-----------编辑:

-----------

如果在同一个WAR中触发事件,我能够观察到该事件。
我还尝试使用@Stateless bean作为事件监听器但没有成功。

I am able to observe the event if it is fired inside the same WAR. Also I tried to use a @Stateless bean as event listener without success.

包装是这样的:


  • EAR

    • WAR(此处应观察事件)

    • WAR

    • EJB(此处触发事件)

    推荐答案

    经过一些更多的研究后,似乎它的预期行为是因为EJB不能看到WAR类。

    After some more research it seems that its expected behaviour because WAR classes are not visible to EJBs.

    更多地考虑它这是一件好事 - 在集群环境中,CDI事件只能由与触发事件的EJB模块在同一节点上运行的WAR接收。但为了可靠地更新用户视图,我们需要在每个实例上接收它。

    Thinking more about it this is a good thing - in a clustered environment the CDI Event would be received only by the WAR running on the same node as the EJB module firing the event. But to reliable update the users view, we need to receive it on every instance.

    在这种情况下,JMS或其他Messaging系统显然是可行的方法。
    还有一个CDI扩展可用于CDI< - > JMS桥接: Seam3 JMS

    JMS or another Messaging system is clearly the way to go in this case. There is also a CDI Extension available for CDI <-> JMS bridging: Seam3 JMS

    这篇关于当打包为EAR时,是否可以在WAR内观察CDI事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆