CDI事件观察者方法是否与EJB兼容? [英] Are CDI event observer methods compatible with EJBs?

查看:144
本文介绍了CDI事件观察者方法是否与EJB兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Singleton EJB(javax.ejb.Singleton版本。叹气),它有一个CDI观察者方法。当我尝试将其部署到glassfish 3.1时,服务器无法在没有任何真正解释的情况下部署EAR文件 - 只是说在部署过程中出现了一个异常而没有任何更多细节。



<$ p SEVERE:加载应用程序时出现异常
SEVERE:关闭应用程序容器时出现异常
....
SEVERE:关闭应用程序容器时出现异常:java .lang.NullPointerException

这是CDI事件监听器:

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



$ b如果我将Singleton bean更改为@ApplicationScoped bean,精细。同样,如果我删除了CDI事件观察者方法,那么应用程序部署就会很好。
我实际上需要这个类作为一个EJB单例,因为我想要EJB的事务,线程安全等等,所以把它作为@ApplicationScoped POJO留给我的用处不大。这个问题似乎并不局限于Singleton bean,尽管我已经通过将注释更改为@Stateless和@Stateful进行了尝试,并且我也遇到了同样的问题。



在我看来,这可能是Weld中的一个错误,或许Weld和EJB正在争论他们如何代理该方法 - 可能EJB需要添加一个拦截器类并包装该方法以确保线程安全,Weld正在尝试做一些事情否则,使事件监听器工作?



我在这里误解了一些东西,如果CDI事件处理程序不能用于EJB(在这种情况下,应该有更好的错误消息从glassfish) - 或者这实际上只是CDI或EJB实现中的一个错误? 解决方案

我认为这是答案:

如果EJB声明本地接口,则CDI观察器方法必须显然为静态或在EJB的本地接口中声明。通常情况下,如果你尝试声明一个不在本地接口中的观察者方法,你会得到如下的Weld异常:

  org.jboss.weld.exceptions.DefinitionException:WELD-000088 Observer方法必须是静态或本地业务方法:[method] public org.stain.ObserverBean.testMethod(EventClass)public @ Singleton类org.stain.ObserverBean 

由于某些原因,glassfish在加载我的EAR文件时不会正确报告此异常,并且只是说加载应用程序时出现异常



将方法添加到本地接口(或删除类的接口声明)可以解决问题并允许应用程序正常加载。


I have a Singleton EJB (javax.ejb.Singleton version. sigh.) which has a CDI observer method on it. When I try to deploy this to glassfish 3.1 the server fails to deploy the EAR file without any real explanation - simply saying there was an exception during deployment without any more 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 change the Singleton bean to just be an @ApplicationScoped bean the app deploys fine. Similarly, if I remove the CDI event observer method the application deploys fine. I actually need the class to be an EJB singleton because I want the transaction, thread safety etc. of EJBs, so just leaving this as a @ApplicationScoped POJO isn't much use to me. The problem doesn't seem to be limited to Singleton beans though - I've experimented by changing the annotation to @Stateless and @Stateful and I get the same issue.

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

Am I misunderstanding something here, and should CDI event handlers simply not be used on EJBs (in which case there should be better error messages from glassfish) - or is this actually just a bug in the CDI or EJB implementation?

解决方案

I think this is the answer :

CDI observer methods must apparently either be static or declared in the local interface of an EJB if the EJB declares a local interface. Normally if you try to declare an observer method that isn't in the local interface you get an exception from Weld like this :

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 simply says Exception while loading the app.

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

这篇关于CDI事件观察者方法是否与EJB兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆