EJB - 查找“ejb/BookRequestBean”失败

7

我刚接触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: 在SerialContext中查找“ejb/BookRequestBean”失败[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。

完整的堆栈跟踪会有所帮助。确保您也查看了这个链接:http://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#POJOLocalEJB - LMeyer
4个回答

8

可能有多种原因:

1) 你的EJB没有映射到JNDI名称。你需要检查你的EJB是否成功部署,并且是否映射到JNDI名称。你可以检查服务器GUI服务器启动日志或使用通用测试客户端来查看EJB是否正确映射。注意,UTC只会显示远程暴露的 EJBs。

2) 你的EJB仅对本地应用程序公开。在这种情况下,对你的EJB进行远程调用跨应用程序调用(不同的EAR、WAR...)将会失败。在这种情况下,创建远程接口并将其公开。本地接口只公开EJB以供本地调用使用。而远程接口公开EJB以供远程跨应用程序调用使用。

3) 你的RMI/IIOP 端口可能不正确。你可以检查Glassfish GUI服务器启动日志来查看RMI/IIOP分配的端口是什么。

注意: 要诊断确切的问题,请发布完整堆栈跟踪。


2

除了@Ravi Trivedi和@Miljen Mikic提供的信息,如果你使用的是Glassfish,你应该检查你的EJB在JNDI中的注册方式。例如,在Glassfish中,输入以下命令:

 asadmin list-jndi-entries

2

除了@RaviTrivedi的好答案外,这里还有一些想法:

  • @Named注释不应该这样使用
  • 不要同时使用namemappedName,对于Glassfish来说,只使用mappedName就足够了
  • 你的EJB应该实现远程接口

稍作插话。name 用于标识 EJB,而 mappedName 是 EJB 的 JNDI 名称。此外,mappedName 是供应商特定的。例如:Glassfish 支持,Websphere 不支持。 - Ravi Trivedi
@RaviTrivedi 是的,这就是为什么我指出它对于Glassfish来说已经足够了。如果你想要完全可移植的应用程序,就不应该使用它。 - Miljen Mikic

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接