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 [myEnv={org.omg.CORBA.ORBInitialPort=3700,java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory,org. omg.CORBA.ORBInitialHost=本地主机,java

我添加了appserv-rt.jar, gf-client.jar, javaee.jar,但仍然没有运气。谁能帮助我,我在这里缺少什么?我是使用 Glassfish 3.1

4

4 回答 4

8

这可能有几个原因:

1)EJB的未映射到JNDI名称。您需要检查您EJB的部署是否成功,并映射到JNDI名称。您可以检查Server GUIServer 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

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

于 2013-05-12T09:30:59.867 回答
2

除了不错的@RaviTrivedi 答案之外,还有以下几点想法:

  • @Named不应以这种方式使用注释
  • 不要同时使用nameand mappedName,对于 Glassfish 来说只使用就足够了mappedName
  • 你的 EJB 应该实现远程接口
于 2013-05-13T07:29:42.973 回答
2

添加到@Ravi Trivedi 和@Miljen Mikic,如果您使用的是Glassfish,您应该检查您的EJB 如何在JNDI 中注册。例如,在 Glassfish 中键入以下命令:

 asadmin list-jndi-entries
于 2014-03-14T01:29:59.213 回答
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
于 2020-07-18T11:52:06.700 回答