我是EJB新手,正在尝试" Hello World" EJB Java程序的类型。这是我的EJB:
package dukesbookstore.ejb;
@Stateless(name="BookRequestBean", mappedName="ejb/BookRequestBean")
@Named
public class BookRequestBean {
//Other codes here
}
这是我的客户:
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
prop.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
prop.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
try {
InitialContext ctx = new InitialContext(prop);
ctx.lookup("ejb/BookRequestBean");
System.out.println("EJB Look-up successfull!!");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
但每当我尝试跑步时,我都会遇到异常:
javax.naming.NamingException:' ejb / BookRequestBean'查找失败在SerialContext中[myEnv = {org.omg.CORBA.ORBInitialPort = 3700,java.naming.factory.initial = com.sun.enterprise.naming.SerialInitContextFactory,org.omg.CORBA.ORBInitialHost = localhost,java
我添加了appserv-rt.jar
,gf-client.jar
,javaee.jar
,但仍然没有运气。任何人都可以帮助我,我在这里缺少什么?我是Glassfish 3.1
答案 0 :(得分:8)
这有几个原因:
1)您的EJB
未映射到 JNDI 名称。您需要检查是否已成功部署EJB
并将其映射到 JNDI 名称。您可以查看Server GUI
,Server Log on startup
或使用Universal Test Client
查看EJB
是否已正确映射。请注意,UTC
仅显示远程公开的 EJB。
2)您的EJB
仅向本地应用程序公开。在这种情况下,远程调用或跨应用程序调用(不同的EAR,WAR ...)将导致EJB
失败。在此方案中,创建远程接口并公开它。 本地接口将EJB暴露给本地调用。 远程接口将EJB暴露给远程或跨应用程序调用。
3)您的RMI/IIOP
端口可能不正确。您可以查看Glassfish GUI
或Server startup log
,了解分配了哪些端口 RMI/IIOP
。
注意:要诊断确切的问题,请发布完整的堆栈跟踪。
答案 1 :(得分:2)
除了很好的@RaviTrivedi回答,这里有一些想法:
@Named
注释name
和mappedName
,对于Glassfish,仅使用mappedName
答案 2 :(得分:2)
添加到@Ravi Trivedi和@Miljen Mikic,如果您使用的是Glassfish,您应该检查您的EJB在JNDI中的注册方式。例如,在Glassfish中键入以下命令:
asadmin list-jndi-entries
答案 3 :(得分:0)
1. your above code works perfectly on glassfish only missing was the remote interface. 2. As suggested above mappedname[vendor specific] and name ....yada yada..... 3. copy below code and run you should be good to go 4. only ensure the *client*.jar is on your path and redeploy the application to glassfish server and run main.
**This Remote interface (the only addition to your above code);**
import javax.ejb.Remote;
@Remote
public interface BookRequestI {
//Other codes here
String getISBN();
}
**your existing implementation spiced with my getISBN() to prove the point :)**
import javax.ejb.Stateless;
@Stateless(name="BookRequestBean", mappedName="ejb/BookRequestBean")
public class BookRequest implements BookRequestI {
//Other codes here
@Override
public String getISBN(){
return "ISBN 87 - 11 - 07559 - 7";
}
}
**your test as is with my getISBN and typing to interface Type.**
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import java.util.Properties;
public class BookRequestT {
public static void main(String[] args) {
Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
prop.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
prop.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
try {
Context ctx = new InitialContext(prop);
BookRequestI bookRequest = (BookRequestI) ctx.lookup("ejb/BookRequestBean");
System.out.println("EJB Look-up successfull!!" + bookRequest.getISBN());
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
output:
EJB Look-up successfull!!ISBN 87 - 11 - 07559 - 7
Process finished with exit code 0