通过创建初始上下文并执行查找,使用RMI客户端访问远程对象

时间:2011-04-13 18:15:41

标签: weblogic rmi jndi weblogic-10.x

我正在尝试从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

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

错误意味着您已查找了Context而不是绑定对象。换句话说,你查找“cea”而不是像“cea / Bean”这样的东西。这类似于在目录上使用FileInputStream。

答案 2 :(得分:0)

我使用的是错误的JNDI名称,因此无法检索该对象。谢谢大家的关注。