2

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!

fabpicca
  • 285
  • 1
  • 3
  • 16
  • Commenting my on question. One of the things I've noticed si that the afore mentioned ejb and the other singleton are on different thread pools (admin-thread-pool and thread-pool-1). Does this means something? – fabpicca Mar 08 '13 at 21:09

1 Answers1

3

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.

rdcrng
  • 3,415
  • 2
  • 18
  • 36
  • Thanks for the answer, just want to remark one thing. I'm actually able to inject dependencies from, following your example, CDIBean.jar. For instance I can inject (going back to my example) the whole MyClass in MyObserver with no problems at all, but when it comes to events the problem appears. As I commented on my first post I'm worried about the thread pools, any ideas? – fabpicca Mar 09 '13 at 06:34
  • The spec mentions scopes being bound to the thread context as in http://docs.jboss.org/cdi/spec/1.0/html_single/#normalscope, but I can't seem to find anything related to events so far. I'll try to replicate your problem and see if I come up with anything. – rdcrng Mar 09 '13 at 19:32
  • In the meanwhile I've decided to switch to the dear old JMS. As far as I understand, CDI events are fine in "easier" contexts rather than heavily structured applications. Thanks a lot for your advice anyway. – fabpicca Mar 13 '13 at 09:25
  • @fabpicca Sorry I didn't get back to you earlier. The only additional info I could find was http://stackoverflow.com/questions/6002885/are-cdi-event-observer-methods-compatible-with-ejbs. Which sheds some light on it, but it still didn't allow me to get it to work. Maybe it will help you. – rdcrng Mar 13 '13 at 13:27