Are CDI event monitoring methods compatible with EJB?

I have a Singleton EJB (version of javax.ejb.Singleton. Sigh.) On which there is a CDI observer method. When I try to deploy this to Glassfish 3.1, the server will not be able to deploy the EAR file without any real explanation - just saying that during the deployment there was an exception without any details.

SEVERE: Exception while loading the app SEVERE: Exception while shutting down application container .... SEVERE: Exception while shutting down application container : java.lang.NullPointerException 

This is the CDI event listener:

 public void updateFromGranule(@Observes @CloudMask GranuleAvailableEvent granuleEvent) { LOG.info("updating cloud map"); update(granuleEvent.getGranule(), CloudMask.class); fireUpdate(); } 

If I changed the Singleton bean to just be an @ApplicationScoped bean, the application deploys perfectly. Similarly, if I remove the CDI event observer method, the application will deploy fine. I really need a class that will be singleton EJB because I want transactions, thread safety, etc. EJB, so just leaving this as @ApplicationScoped POJO is not much for me. The problem doesn't seem to be limited to Singleton beans, though - I experimented by changing the annotation to @Stateless and @Stateful, and I get the same problem.

It seems to me that this may be a mistake in Weld, perhaps Weld and EJB are fighting about how they proxy this method - presumably EJB should add an interceptor class and wrap this method to ensure thread safety, and Weld is trying to do something else, to get an event listener to work?

I don’t understand something, and if CDI event handlers just won’t be used in EJB (in this case there should be better error messages from glass fish) - or is it just a mistake in implementing CDI or EJB?

+7
source share
2 answers

I think this is the answer:

CDI watch methods must be either static or declared in the local bean if the bean declares the local bean. Usually, if you try to declare an observer method that is not in the local interface, you get an exception from Weld as follows:

 org.jboss.weld.exceptions.DefinitionException: WELD-000088 Observer method must be static or local business method: [method] public org.stain.ObserverBean.testMethod(EventClass) on public@Singleton class org.stain.ObserverBean 

For some reason, Glassfish does not report this exception properly when loading my EAR file and just says Exception while loading the app .

Adding a method to the local interface (or removing the interface declaration in the class) fixes the problem and allows the application to load normally.

+5
source

I noticed the same problem with the latest version of the weld. But if you add the @LocalBean annotation, it will work with @Singleton and @Singleton @Startup.

+3
source

All Articles