Reputation: 285
I'm struggling a little on CDI Events. I have a class that is implemented as a CDI Singleton almost like this:
import import javax.inject.Singleton;
@Singleton
public class MyClass{
@Inject
Event<StatusUpdateEvent> events;
public MyClass(){};
public void myMethod(){
events.fire(new StatusUpdateEvent());
}
}
Then I have my consumer class implemented as EJB Singleton
import javax.ejb.Singleton;
@Singleton
public class MyObserver(){
public MyObserver(){};
public onStatusUpdateEvent(@Observes StatusUpdateEvent event){
...do something...
}
}
The problem is that when myMethod
is invoked no events is received from myObserver.
MyClass
is included in a library jar of my EAR project (the jar has beans.xml) and MyObserver
is an EJB of the same EAR.
What am I doing wrong? Thanks a lot for your help!
Upvotes: 2
Views: 1700
Reputation: 3443
CDI injection does not work across class-loader boundaries. Since your project is an EAR, the ejb-jar is most likely on a separate class-loader. For example, if your project structure is:
--EAR
|--EAR/lib
|--|--EAR/lib/CDIBeans.jar
|--EJBArchive.jar
then any beans from CDIBean.jar
won't be available for injection into your EJBArchive.jar
.
Upvotes: 3