EJB3 Glassfish JNDI 查找

发布于 2024-11-01 23:03:47 字数 2897 浏览 1 评论 0原文

我正在使用 Glassfish 捆绑的 Eclipse IDE。我编写了一个简单的 EJB 应用程序。但它不起作用。

@Stateless
@Remote(CalculatorRemote.class) 
@Local(CalculatorLocal.class) 
public class Calculator implements CalculatorRemote, CalculatorLocal {
    @Override
    public String sayHello(String name) {
        System.out.println("ejb:"+name);
        return null;
    }
}

----

@Remote
public interface CalculatorRemote {
    public String sayHello(String name);

}
-------

@Local
public interface CalculatorLocal {
    public String sayHello(String name);
}

我写了一个独立的客户端来测试,但失败了。找不到 JNDI。

public class Main {

    public static void main(String[] args) throws Exception {        
        InitialContext ctx = new InitialContext();      
        CalculatorRemote bean = (CalculatorRemote) ctx.lookup("java:global/TestEAR/TEjb/Calculator!com.CalculatorRemote");
        bean.sayHello("Billy Bob");

    }

}

在服务器日志中,它说

INFO: Portable JNDI names for EJB Calculator : [java:global/TestEAR/TEjb/Calculator!com.CalculatorRemote, java:global/TestEAR/TEjb/Calculator!com.CalculatorLocal]
INFO: Glassfish-specific (Non-portable) JNDI names for EJB Calculator : [com.CalculatorRemote, com.CalculatorRemote#com.CalculatorRemote]

另外,我已经尝试过

ctx.lookup("com.CalculatorRemote")

仍然不起作用。

错误消息是

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 com.Main.main(Main.java:9)
Exception in thread "main" javax.naming.NamingException: Lookup failed for 'java:global/TestEAR/TEjb/Calculator!com.CalculatorRemote' 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 com.Main.main(Main.java:9)
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)
    ... 2 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)
    ... 3 more

请帮助我。

I am using Glassfish-bundled Eclipse IDE. I wrote a simple EJB application. but it doesn't work.

@Stateless
@Remote(CalculatorRemote.class) 
@Local(CalculatorLocal.class) 
public class Calculator implements CalculatorRemote, CalculatorLocal {
    @Override
    public String sayHello(String name) {
        System.out.println("ejb:"+name);
        return null;
    }
}

----

@Remote
public interface CalculatorRemote {
    public String sayHello(String name);

}
-------

@Local
public interface CalculatorLocal {
    public String sayHello(String name);
}

I wrote a standalone client to test, but failed. can't find JNDI.

public class Main {

    public static void main(String[] args) throws Exception {        
        InitialContext ctx = new InitialContext();      
        CalculatorRemote bean = (CalculatorRemote) ctx.lookup("java:global/TestEAR/TEjb/Calculator!com.CalculatorRemote");
        bean.sayHello("Billy Bob");

    }

}

In the server log, it said

INFO: Portable JNDI names for EJB Calculator : [java:global/TestEAR/TEjb/Calculator!com.CalculatorRemote, java:global/TestEAR/TEjb/Calculator!com.CalculatorLocal]
INFO: Glassfish-specific (Non-portable) JNDI names for EJB Calculator : [com.CalculatorRemote, com.CalculatorRemote#com.CalculatorRemote]

Also, I have tried

ctx.lookup("com.CalculatorRemote")

still doesn't work.

error message is

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 com.Main.main(Main.java:9)
Exception in thread "main" javax.naming.NamingException: Lookup failed for 'java:global/TestEAR/TEjb/Calculator!com.CalculatorRemote' 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 com.Main.main(Main.java:9)
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)
    ... 2 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)
    ... 3 more

Please help me.

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

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

发布评论

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

评论(5

海拔太高太耀眼 2024-11-08 23:03:47

您可以添加以上几行吗:

  Properties props = new Properties();
 props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
 props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
 // glassfish default port value will be 3700,
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext ctx = new InitialContext(props);

 CalculatorRemote bean = (CalculatorRemote) ctx.lookup("java:global/TestEAR/TEjb/Calculator!    com.CalculatorRemote");

我在这里使用 EJB3x 和 Glassfish v3 创建了一篇博客文章。
http://anirbanchowdhury.wordpress.com/ 2012/06/07/ejb-3-application-in-glassfish-3x/

Can you just add the above lines:

  Properties props = new Properties();
 props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
 props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
 // glassfish default port value will be 3700,
props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
InitialContext ctx = new InitialContext(props);

 CalculatorRemote bean = (CalculatorRemote) ctx.lookup("java:global/TestEAR/TEjb/Calculator!    com.CalculatorRemote");

I have created a blog post here using EJB3x with Glassfish v3.
http://anirbanchowdhury.wordpress.com/2012/06/07/ejb-3-application-in-glassfish-3x/

寄居人 2024-11-08 23:03:47

我认为抛出异常是因为您没有正确配置初始上下文。创建一个 jndi.properties 文件,或者创建一个包含属性的哈希表,并将其发送到 IntialContext 的构造函数。

使用 Netbeans 和 Glassfish 创建 EJB3

I think that exception is thrown because you didn't configure the initial context properly. Either create a jndi.properties file, or create a hash table with the properties and it send it to the constructor of IntialContext.

Creating EJB3 using Netbeans and Glassfish

晚雾 2024-11-08 23:03:47

就在今天,我遇到了一个问题。
您的独立客户端失败,但它将在 GF EJB 容器内工作。

对于客户端测试,您需要两件事才能使其工作:

  1. 从 GlassFish_install_folder\glassfish\lib 获取 javaee、gf-client 和 appserv-rt jar。最后一个包含 jndi.prop 所以你可以使用默认的 c-tor InitialContext();
  2. 从 GlassFish_install_folder\glassfish\modules 获取所有 jar。

这些 jar 需要位于您的类路径中。这很愚蠢,但我还不知道 2) 中的最小 jar 才能使其工作。

Just today I had an issue with this.
Your standalone client failed but it will work inside GF EJB container.

For client testing you need 2 things to make it work:

  1. from GlassFish_install_folder\glassfish\lib get javaee, gf-client and appserv-rt jars. the last one contains jndi.prop so you could use default c-tor InitialContext();
  2. from GlassFish_install_folder\glassfish\modules get all jars.

These jars need to be in your classpath. It is silly but I don`t know yet the minimum jars from 2) in order to make it work.

一个人的夜不怕黑 2024-11-08 23:03:47

解决方案如下。

在下面的代码中,您必须在另一个JVM中调用bean,对吗?例如,你的主要课程
JRE 中使用的是 BEAN,Glassfish JVM 中使用的是 BEAN。

public class Main {

    public static void main(String[] args) throws Exception {        
        InitialContext ctx = new InitialContext();      
        CalculatorRemote bean = (CalculatorRemote) ctx.lookup("java:global/TestEAR/TEjb/Calculator!com.CalculatorRemote");
        bean.sayHello("Billy Bob");
    }
}

Solution is as below.

In the code below, You have to call a bean in another JVM Right ? For ex, your main class
is in JRE and BEAN is in Glassfish JVM.

public class Main {

    public static void main(String[] args) throws Exception {        
        InitialContext ctx = new InitialContext();      
        CalculatorRemote bean = (CalculatorRemote) ctx.lookup("java:global/TestEAR/TEjb/Calculator!com.CalculatorRemote");
        bean.sayHello("Billy Bob");
    }
}
夕嗳→ 2024-11-08 23:03:47
  1. 您的大部分代码都是正确的,问题是客户端,因为您有本地和远程接口,我已在客户端中添加了我的注释,希望其不言自明。并且....
  2. 在 EJB 上添加了 @javax.ejb.Stateless(name = "CalculatorEJB ") //这是可移植的,没有锁定供应商。替代方案是mappedName,我相信它更以 weblogic 和 glassfish 为中心。
  3. 确保您的类路径中有 client.jar。
  4. 一旦ear部署成功,我就将EJB部署在glassfish容器中,运行客户端。
  5. 除了下面的代码之外,您不需要任何其他配置。这段代码是可移植的,应该适用于大多数容器,但是惊喜很有趣,请按原样运行并让我知道.....

======================= =======

  1. 远程接口

包 com.au.ejbs;

import javax.ejb.Local;
    
@Local
public interface CalculatorLocalI {
     String sayHello(String name);
}

package com.au.ejbs;

import javax.ejb.Remote;

@Remote
public interface CalculatorRemoteI {
    String sayHello(String name);
}


=================================
2. The Impl


package com.au.ejbs;

@javax.ejb.Stateless(name = "CalculatorEJB") //Added this is portable instead of using mappedName
public class Calculator implements CalculatorRemoteI, CalculatorLocalI {
    @Override
    public String sayHello(String name) {
        return  "ejb:"+name;
    }
}



======================================
3. The client


package com.au.clients;


import com.au.ejbs.CalculatorRemoteI;

import javax.naming.InitialContext;

public class CalculatorT {

    public static void main(String[] args) throws Exception {
        InitialContext ctx = new InitialContext();
        CalculatorRemoteI bean = (CalculatorRemoteI) ctx.lookup("java:global/ejb3_2_ear_exploded/ejb/CalculatorEJB!com.au.ejbs.CalculatorRemoteI");
        //portable syntax java:global/[ ear name]/[module name normally the jar name in my case ejb.jar within the ear, ejb3_2_ear_exploded]/name in ....javax.ejb.Stateless(name = "CalculatorEJB")/
        //Now since you have both local and remote interfaces so the extra after the bang ! symbol namely the explicit remote interface name.
        //if you had only 1 remote interface impl you will require only (CalculatorRemoteI) ctx.lookup("java:global/ejb3_2_ear_exploded/ejb/CalculatorEJB");
        System.out.println("===output==" +bean.sayHello("Billy Bob"));

    }

}

4. output
===output==ejb:Billy Bob
  1. Most of your code is correct the catch is the client, as you have local and remote interfaces I have added my comments in client hope its self explanatory.And....
  2. Added on EJB @javax.ejb.Stateless(name = "CalculatorEJB") //This is portable No Vendor locked in. Alternative is mappedName which i believe is more weblogic and glassfish centric.
  3. Ensure you have client.jar in your class path.
  4. I have deployed the EJB's in glassfish container once ear deployed successfully, run the client.
  5. Apart from the below code you wont need any other configurations. This code is portable and should work on most containers however Surprises is fun do run as is and let me know .....

==============================

  1. The Remote interfaces

package com.au.ejbs;

import javax.ejb.Local;
    
@Local
public interface CalculatorLocalI {
     String sayHello(String name);
}

package com.au.ejbs;

import javax.ejb.Remote;

@Remote
public interface CalculatorRemoteI {
    String sayHello(String name);
}


=================================
2. The Impl


package com.au.ejbs;

@javax.ejb.Stateless(name = "CalculatorEJB") //Added this is portable instead of using mappedName
public class Calculator implements CalculatorRemoteI, CalculatorLocalI {
    @Override
    public String sayHello(String name) {
        return  "ejb:"+name;
    }
}



======================================
3. The client


package com.au.clients;


import com.au.ejbs.CalculatorRemoteI;

import javax.naming.InitialContext;

public class CalculatorT {

    public static void main(String[] args) throws Exception {
        InitialContext ctx = new InitialContext();
        CalculatorRemoteI bean = (CalculatorRemoteI) ctx.lookup("java:global/ejb3_2_ear_exploded/ejb/CalculatorEJB!com.au.ejbs.CalculatorRemoteI");
        //portable syntax java:global/[ ear name]/[module name normally the jar name in my case ejb.jar within the ear, ejb3_2_ear_exploded]/name in ....javax.ejb.Stateless(name = "CalculatorEJB")/
        //Now since you have both local and remote interfaces so the extra after the bang ! symbol namely the explicit remote interface name.
        //if you had only 1 remote interface impl you will require only (CalculatorRemoteI) ctx.lookup("java:global/ejb3_2_ear_exploded/ejb/CalculatorEJB");
        System.out.println("===output==" +bean.sayHello("Billy Bob"));

    }

}

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