首页 > 解决方案 > GlassFish 5 上的 JNDI 查找失败

问题描述

我是 Java EE 的新手,我的问题可能看起来很愚蠢。

当我javax.naming.NameNotFoundException尝试在 Glassfish 上部署我的应用程序时,我有。当我在 DeviceStorage 类中实现 java.util.Observer 时会发生这种情况。当我不这样做时,它会毫无错误地部署。有人知道为什么会发生吗?

Exception while deploying the app [Test-1.0-SNAPSHOT] : JNDI lookup 
failed for the resource: Name: 
[java:module/env/com.view.servlet.TestServlet/deviceStorage], Lookup: 
[com.core.DeviceStorage#com.core.DeviceStorage], Type: [Session].

Caused by: 
javax.naming.NameNotFoundException: 
com.core.DeviceStorage#com.core.DeviceStorage not found
@Singleton
public class ServersStateListener extends Observable {
...
}
@Singleton
public class DeviceStorage implements Observer{
...
}
@WebServlet("/test")
public class TestServlet extends HttpServlet {

    @EJB
    private ServersStateListener serversStateListener;

    @EJB
    private DeviceStorage deviceStorage;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("--== Servlet ==--");

        System.out.printf("ServerStateListener: %s\n", serversStateListener);
        System.out.printf("DeviceStorage: %s\n", deviceStorage);

    }
}

标签: javaejbjndienterprise

解决方案


GlassFish 中有一个错误会导致类似的错误消息。该漏洞于 2018 年 2 月在 Payara(源自 GlassFish)中发现并修复,并包含在 5.182 版本中。我不知道它是否已在 GlassFish 中修复。Payara 错误报告可在此处找到,并且已在此处提交修复。

错误报告提到可以使用以下属性(JVM 选项)作为解决方法:

-Ddeployment.resource.validation=false

我不确定该设置是否也适用于 GlassFish,或者它是否特定于 Payara。但这当然值得一试。


推荐阅读