我正在使用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
请帮帮我。
答案 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的构造函数。
答案 2 :(得分:0)
就在今天,我遇到了一个问题。 您的独立客户端失败但它将在GF EJB容器内工作。
对于客户端测试,您需要两件事才能使其正常工作:
这些罐子必须在你的类路径中。这很愚蠢,但我还不知道来自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)
=============================
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