EJB3 Glassfish JNDI查找

时间:2011-04-18 16:27:11

标签: glassfish ejb jndi

我正在使用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

请帮帮我。

5 个答案:

答案 0 :(得分:3)

你能添加以上几行:

  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");

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

答案 1 :(得分:0)

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

Creating EJB3 using Netbeans and Glassfish

答案 2 :(得分:0)

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

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

    来自GlassFish_install_folder \ glassfish \ lib的
  1. 获取javaee,gf-client和appserv-rt jars。最后一个包含jndi.prop所以你可以使用默认的c-tor InitialContext();
  2. 来自GlassFish_install_folder \ glassfish \ modules的
  3. 获取所有罐子。
  4. 这些罐子必须在你的类路径中。这很愚蠢,但我还不知道来自2)的最小罐子才能使它发挥作用。

答案 3 :(得分:0)

解决方法如下。

在下面的代码中,你必须在另一个JVM中调用bean吗?对于前,你的主要课程 在JRE中,BEAN在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");
    }
}

答案 4 :(得分:0)

  1. 您的大多数代码都是正确的,因为客户端具有本地和远程接口,因此我在客户端中添加了注释,希望它可以自我解释。....
  2. 在EJB上添加了@ javax.ejb.Stateless(name =“ CalculatorEJB”)//这是可移植的,没有供应商被锁定。替代项是mapedName,我相信它更像以weblogic和glassfish为中心。
  3. 确保您的类路径中有 client .jar。
  4. 一旦成功部署EJB,我就将其部署在glassfish容器中了,运行客户端。
  5. 除了下面的代码,您不需要任何其他配置。该代码是可移植的,并且可以在大多数容器上运行,但是Surprises很有趣,请按原样运行,并让我知道.....

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

  1. 远程接口

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