Is it possible to observe a CDI Event inside a WAR when packaged as EAR

为君一笑 提交于 2020-01-12 07:47:10

问题


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) {
..
}

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?

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

  • JMS
  • Guava EventBus
  • ...

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

----------- EDIT:

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.

Packaging is like this:

  • EAR
    • WAR ( event should be observed here )
    • WAR
    • EJB ( event gets fired in here )

回答1:


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

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 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



来源:https://stackoverflow.com/questions/14709475/is-it-possible-to-observe-a-cdi-event-inside-a-war-when-packaged-as-ear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!