Glassfish v3/JNDI 入口无法发现问题!

发布于 2024-08-17 21:30:18 字数 3374 浏览 5 评论 0原文

我在尝试从 Java 应用程序客户端调用 EJB 的方法时遇到问题。 这是代码。

EJB 远程接口

package com.test;

import javax.ejb.Remote;

@Remote
public interface HelloBeanRemote {

    public String sayHello();

}

EJB

package com.test;

import javax.ejb.Stateless;

@Stateless (name="HelloBeanExample" , mappedName="ejb/HelloBean")
public class HelloBean implements HelloBeanRemote {

    @Override
    public String sayHello(){

        return "hola";

    }

}

主类(另一个项目)

import com.test.HelloBeanRemote;
import javax.naming.Context;
import javax.naming.InitialContext;

public class Main {


    public void runTest()throws Exception{

        Context ctx = new InitialContext();
        HelloBeanRemote  bean = (HelloBeanRemote)ctx.lookup("java:global/Test/HelloBeanExample!com.test.HelloBeanRemote");
        System.out.println(bean.sayHello());

    }


    public static void main(String[] args)throws Exception {

        Main main = new Main();
        main.runTest();

    }

}

那么,我的问题是什么?找不到此 EJB 的 JNDI 条目!

java.lang.NullPointerException
        at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:297)
        at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
        at javax.naming.InitialContext.lookup(InitialContext.java:392)
        at testdesktop.Main.runTest(Main.java:22)
        at testdesktop.Main.main(Main.java:31) Exception in thread "main" javax.naming.NamingException: Lookup failed for 'java:global/Test/HelloBeanExample!com.test.HelloBeanRemote' in SerialContext  [Root exception is javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext  [Root exception is java.lang.NullPointerException]]
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:442)
        at javax.naming.InitialContext.lookup(InitialContext.java:392)
        at testdesktop.Main.runTest(Main.java:22)
        at testdesktop.Main.main(Main.java:31) Caused by: javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext  [Root exception is java.lang.NullPointerException]
        at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:276)
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
        ... 3 more Caused by: java.lang.NullPointerException
        at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:297)
        at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
        ... 4 more Java Result: 1

我尝试使用不同的 JNDI 条目,但没有任何效果(我从 NetBeans 控制台获取此条目):

INFO: EJB HelloBeanExample 的可移植 JNDI 名称 : [java:global/Test/HelloBeanExample, java:global/Test/HelloBeanExample!com.test .HelloBeanRemote]

INFO:EJB HelloBeanExample 的 Glassfish 特定(不可移植)JNDI 名称:[ejb/HelloBean, ejb/HelloBean#com.test.HelloBeanRemote]

所以我尝试了以下条目,但得到了相同的异常:

  1. java: global/Test/HelloBeanExample
  2. java:global/Test/HelloBeanExample!com.test.HelloBeanRemote
  3. ejb/HelloBean
  4. ejb/HelloBean#com.test.HelloBeanRemote

我正在使用 Netbeans 6.8 和 Glassfish v3!

I've been having problems trying to call an EJB's method from a Java Application Client.
Here is the code.

EJB Remote Interface

package com.test;

import javax.ejb.Remote;

@Remote
public interface HelloBeanRemote {

    public String sayHello();

}

EJB

package com.test;

import javax.ejb.Stateless;

@Stateless (name="HelloBeanExample" , mappedName="ejb/HelloBean")
public class HelloBean implements HelloBeanRemote {

    @Override
    public String sayHello(){

        return "hola";

    }

}

Main class (another project)

import com.test.HelloBeanRemote;
import javax.naming.Context;
import javax.naming.InitialContext;

public class Main {


    public void runTest()throws Exception{

        Context ctx = new InitialContext();
        HelloBeanRemote  bean = (HelloBeanRemote)ctx.lookup("java:global/Test/HelloBeanExample!com.test.HelloBeanRemote");
        System.out.println(bean.sayHello());

    }


    public static void main(String[] args)throws Exception {

        Main main = new Main();
        main.runTest();

    }

}

Well, what is my problem? JNDI entry for this EJB cannot be found!

java.lang.NullPointerException
        at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:297)
        at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
        at javax.naming.InitialContext.lookup(InitialContext.java:392)
        at testdesktop.Main.runTest(Main.java:22)
        at testdesktop.Main.main(Main.java:31) Exception in thread "main" javax.naming.NamingException: Lookup failed for 'java:global/Test/HelloBeanExample!com.test.HelloBeanRemote' in SerialContext  [Root exception is javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext  [Root exception is java.lang.NullPointerException]]
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:442)
        at javax.naming.InitialContext.lookup(InitialContext.java:392)
        at testdesktop.Main.runTest(Main.java:22)
        at testdesktop.Main.main(Main.java:31) Caused by: javax.naming.NamingException: Unable to acquire SerialContextProvider for SerialContext  [Root exception is java.lang.NullPointerException]
        at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:276)
        at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:430)
        ... 3 more Caused by: java.lang.NullPointerException
        at com.sun.enterprise.naming.impl.SerialContext.getRemoteProvider(SerialContext.java:297)
        at com.sun.enterprise.naming.impl.SerialContext.getProvider(SerialContext.java:271)
        ... 4 more Java Result: 1

I've trying with different JNDI entries but nothing works (I got this entries from NetBeans console):

INFO: Portable JNDI names for EJB HelloBeanExample : [java:global/Test/HelloBeanExample, java:global/Test/HelloBeanExample!com.test.HelloBeanRemote]

INFO: Glassfish-specific (Non-portable) JNDI names for EJB HelloBeanExample : [ejb/HelloBean, ejb/HelloBean#com.test.HelloBeanRemote]

So I tried with the following entries but I got the same exception :

  1. java:global/Test/HelloBeanExample
  2. java:global/Test/HelloBeanExample!com.test.HelloBeanRemote
  3. ejb/HelloBean
  4. ejb/HelloBean#com.test.HelloBeanRemote

I'm using Netbeans 6.8 and Glassfish v3!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怎樣才叫好 2024-08-24 21:30:18

实际上,您的问题不是查找您的 bean 的 JNDI 引用,否则您会得到类似的结果:

Caused by: javax.naming.NameNotFoundException: ejb/HelloBean not found

不,这里,我怀疑是一个简单的类路径问题,您只是在您的客户端项目的类路径。对于 GlassFish v3,添加 $GF_HOME/modules/gf-client.jar 应该足够了,如 如何从独立的 java 客户端访问远程 EJB 组件?(我的理解是这个 jar 应该替换 $ GF_HOME/lib/appserv-rt.jar 出于与 GFv2 的兼容性原因而存在)。然而,从 GlassFish 安装目录引用 gf-client.jar 非常重要,否则将找不到其清单中声明的​​ jar。

gf-client.jar 引用 GlassFish 安装目录中的许多其他 .jar,因此最好从安装目录本身引用它,而不是复制它(以及所有其他 .jar)。 jars)到另一个位置。

修复此问题后,您应该能够使用 GlassFish 在日志中输出的 JNDI 名称来查找您的 bean。我建议使用 Java EE 6 中新的可移植全局 JNDI 名称。

以防万一,EJB 3.1 中可移植全局 JNDI 名称的语法是什么? GlassFish EJB FAQ 中的条目提供了这一新约定的精彩总结。如果您想了解更多信息,请查看:http://blogs.oracle.com/MaheshKannan/entry /portable_global_jndi_names

Actually, your problem is not the lookup of the JNDI reference of your bean or you would get something like that:

Caused by: javax.naming.NameNotFoundException: ejb/HelloBean not found

No, here, I suspect a simple classpath problem, you're simply missing some jar on the classpath of your client project. With GlassFish v3, adding $GF_HOME/modules/gf-client.jar should be enough as mentioned in How do I access a Remote EJB component from a stand-alone java client? in GlassFish's EJB FAQ (my understanding is that this jar is supposed to replace $GF_HOME/lib/appserv-rt.jar which is there for compatibility reasons with GFv2). It is however important to refer the gf-client.jar from the GlassFish installation directory or the jars declared in its manifest won't be found.

gf-client.jar refers to many other .jars from the GlassFish installation directory so it is best to refer to it from within the installation directory itself rather than copying it (and all the other .jars) to another location.

Once you'll have this fixed, you should be able to lookup your bean using the JNDI names that GlassFish outputs in the logs. I'd suggest to use the new portable global JNDI names from Java EE 6.

Just in case, the What is the syntax for portable global JNDI names in EJB 3.1? entry from the GlassFish EJB FAQ provides a nice summary of this new convention. And if you want more information, check out: http://blogs.oracle.com/MaheshKannan/entry/portable_global_jndi_names.

最好是你 2024-08-24 21:30:18

我发现了一些东西!我认为有一种“特殊方法”可以使用 NetBeans 添加和配置组件!我一直在网页上阅读 netbeans 的教程,显然我做错了!所以,这不是这个问题的真正解决方案,但也许如果您阅读其中之一 http ://netbeans.org/kb/trails/java-ee.html 你会找到你的答案!

问候

I have found something! I think there is a "special way" to add and configure components using NetBeans! I've been reading the netbeans's tutorials from the web pages and apparently I was doing the things wrong! So, this is not a real solution for this problem but maybe if you read one of those http://netbeans.org/kb/trails/java-ee.html you will find your aswers!

Regards

风流物 2024-08-24 21:30:18

我也遇到过同样的问题。我用 Google 搜索了网络,并逐行按照 Oracle 的教程设置了一个独立的 Java 客户端,但仍然没有任何效果。我遇到了 http://glassfish.java.net/javaee5/ejb/EJB_FAQ.html< /a> 并且它包含我已经尝试解决该问题的信息。

我只需重新启动 IDE、清理并构建项目即可;也可以重新创建项目。谁知道,当满月时,我的代码可能运行良好:)所以可能也想尝试一下

I have encountered the same problem. I googled the web and followed tutorials from Oracle line by line to set up a standalone java client but still nothing works. I came across http://glassfish.java.net/javaee5/ejb/EJB_FAQ.html and it has an information that I already tried to resolve the issue.

I'll just restart my IDE, clean and build the project; may be also create the project afresh. And who knows, when the moon is full, my code might run fine :) So might wanna try that too

酒绊 2024-08-24 21:30:18

我的独立客户端终于运行了!诀窍是,只需重新启动 Netbeans IDE、取消部署应用程序并重新部署即可。这应该可以解决您的问题(假设您的代码和配置中没有其他错误)。

My stand alone client finally run! The trick was, just restarting Netbeans IDE, undeploying the application and redeploying it. This should fix your problem(assuming there is no other error in your code and configuration).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文