【问题标题】:Access remote objects with an RMI client by creating an initial context and performing a lookup通过创建初始上下文并执行查找,使用 RMI 客户端访问远程对象
【发布时间】:2011-04-13 18:15:40
【问题描述】:

我正在尝试从 Weblogic 10 服务器上的 EJB 中查找 PublicRepository 类。这是一段代码:

/**
     * RMI/IIOP clients should use this narrow function
     */
private static Object narrow(Object ref, Class c) {
    return PortableRemoteObject.narrow(ref, c);
}

/**
 * Lookup the EJBs home in the JNDI tree
 */
private static PublicRepository lookupHome() throws NamingException {
    // Lookup the beans home using JNDI
    Context ctx = getInitialContext();

    try {

        Object home = ctx.lookup("cea");
        return (PublicRepository) narrow(home, PublicRepository.class);

    } catch(NamingException ne) {
        System.out.println("The client was unable to lookup the EJBHome.  Please make sure ");
        System.out.println("that you have deployed the ejb with the JNDI name "
        + "cea" + " on the WebLogic server at " + "iiop://localhost:7001");
        throw ne;
    }
}


private static Context getInitialContext() throws NamingException {

    try {
        // Get an InitialContext
        Properties h = new Properties();
        h.put(Context.INITIAL_CONTEXT_FACTORY,
        "weblogic.jndi.WLInitialContextFactory");
        h.put(Context.PROVIDER_URL, "iiop://localhost:7001");
        return new InitialContext(h);

    } catch(NamingException ne) {
        System.out.println("We were unable to get a connection to the WebLogic server at " + "iiop://localhost:7001");
        System.out.println("Please make sure that the server is running.");
        throw ne;
    }
}

然而,我得到了 Cast Exception:

Exception in thread "main" java.lang.ClassCastException
    at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(Unknown Source)
    at javax.rmi.PortableRemoteObject.narrow(Unknown Source)
    at vrd.narrow(vrd.java:67)
    at vrd.lookupHome(vrd.java:80)
    at vrd.main(vrd.java:34)
Caused by: java.lang.ClassCastException: weblogic.corba.j2ee.naming.ContextImpl
    ... 5 more

当我使用上述代码检索要在我的客户端应用程序中使用的某个类时,我是否正确?我怎样才能摆脱演员表异常?

【问题讨论】:

  • 您能否提供该类的完整代码以及您的客户端应用程序的 CLASSPATH?异常中引用的包看起来很可疑。
  • 您要查找哪个版本的 EJB?它是 EJB 3 吗? EJB2?同样使用 weblogic 10.x,您可以考虑使用注释来获取对 EJB 的引用。您能否详细说明您的问题?

标签: weblogic rmi jndi weblogic-10.x


【解决方案1】:

简单的做法是将“narrow”的结果存储在 java.lang.Object 中,然后查看它是什么类型...

【讨论】:

  • 嗯我试过了,我仍然得到演员表异常!?顺便说一句,PublicRepository 扩展了 EJBObject 类型。
  • 当您尝试将方法的结果分配给“对象”时收到 ClassCastException,我很惊讶...
  • 您应该将 Object.class 传递给 narrow(),而不是 PublicRepository.class。实际的 ClassCastException 是在narrow() 内部生成的,而不是在方法返回时生成的。但首先,你为什么不直接打印 home.getClass()?
【解决方案2】:

该错误表示您查找的是 Context 而不是绑定对象。换句话说,您查找的是“cea”而不是“cea/Bean”。这类似于在目录上使用 FileInputStream。

【讨论】:

    【解决方案3】:

    我使用了错误的 JNDI 名称,因此它无法检索对象。感谢大家观看。

    【讨论】:

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