【问题标题】:EJB - Lookup failed for 'ejb/BookRequestBean'EJB - 查找“ejb/BookRequestBean”失败
【发布时间】:2013-05-10 11:27:22
【问题描述】:

我是 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 中查找“ejb/BookRequestBean”失败, org.omg.CORBA.ORBInitialHost=localhost, java

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

【问题讨论】:

标签: java jakarta-ee ejb ejb-3.0 glassfish-3


【解决方案1】:

这可能有几个原因:

1)您的EJB 未映射到JNDI 名称。您需要检查您的EJB 是否部署成功并映射到JNDI 名称。您可以检查Server GUIServer Log on startup 或使用Universal Test Client 来查看EJB 是否映射正确。注意,UTC 只会显示远程暴露的 EJB。

2) 您的EJB 只暴露给Local 应用程序。在这种情况下,对您的EJB远程调用跨应用程序调用(不同的EAR、WAR...)将失败。在这种情况下,创建 Remote interface 并公开它。 本地接口 仅将 EJB 公开给本地调用。 远程接口远程跨应用程序调用公开EJB。

3) 您的RMI/IIOP 端口 可能不正确。您可以查看Glassfish GUIServer startup log 以查看RMI/IIOP 分配给的端口

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 轻微感叹。 name 用于标识 EJB,而 mappedName 是 EJB 的 JNDI 名称。 mappedName 也是特定于供应商的。即:Glassfish 支持,Websphere 不支持。
    • @RaviTrivedi 是的,这就是为什么我注意到它对 Glassfish 来说已经足够了。如果您想要完全可移植的应用程序,则不应使用它。
    【解决方案3】:

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

     asadmin list-jndi-entries
    

    【讨论】:

      【解决方案4】:
      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多