Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 )
like image 960
psartini Avatar asked Feb 05 '13 14:02

psartini


1 Answers

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

like image 195
psartini Avatar answered Sep 28 '22 11:09

psartini