Java RMI代理问题 [英] Java RMI Proxy issue

查看:149
本文介绍了Java RMI代理问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:

 java.lang.ClassCastException: $Proxy0 cannot be cast to rmi.engine.Call
        at Main.main(Main.java:39)

我的 Abstract 调用类都扩展远程

致电:

public class Call extends UnicastRemoteObject implements rmi.engine.Abstract {

    public Call() throws Exception {
        super(Store.PORT, new RClient(), new RServer());
    }

    public String getHello() {
        System.out.println("CONN");
        return "HEY";
    }
} 

摘要:

public interface Abstract extends Remote {

    String getHello() throws RemoteException;

}

这是我的主要:

public static void main(String[] args) {
    if (args.length == 0) {
        try {
            System.out.println("We are slave ");
            InetAddress ip = InetAddress.getLocalHost();
            Registry rr = LocateRegistry.
                getRegistry(ip.getHostAddress(), Store.PORT, new RClient());

            Object ss = rr.lookup("FILLER");

            System.out.println(ss.getClass().getCanonicalName());
            System.out.println(((Call)ss).getHello());

        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        if (args[0].equals("master")) {
            // Start Master
            try {
                RMIServer.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

Netbeans说问题出在第39行,即

Netbeans says the problem is on line 39 which is

    System.out.println(((Call)ss).getHello());

输出如下:

运行:

We are slave 
Connecting 10.0.0.212:5225
$Proxy0
java.lang.ClassCastException: $Proxy0 cannot be cast to rmi.engine.Call
        at Main.main(Main.java:39)

BUILD SUCCESSFUL (total time: 1 second)

我正在cmd上监听端口5225上运行一个主人。

I am running a master in cmd listening on port 5225.

推荐答案

$ Proxy0 动态代理,即java在运行时生成一个类。

The class $Proxy0 is a dynamic proxy, that is, a class is generate by java at run-time.

此类应实现接口 Abstract ,但不扩展调用。所以你不能将它转发到调用(这会给出一个类强制转换),但是将它向下转换为 Abstract 应该是好的。

This class should implement the interface Abstract, but does not extend Call. So you can't downcast it to Call (this gives a class cast), but downcasting it to Abstract should be ok.

如果你使用RMI,你应该只通过接口与远程对象通信。

If you use RMI, you should only communicate with remote object through interfaces.

PS:抽象是一个令人困惑的名字。我真的以为你说的是​​抽象课程第一。如果可能的话我会重命名。对于 rmi.engine ,更喜欢 org.myproject ,以避免与JDK的内部类混淆。

PS: Abstract is kind of a confusing name. I really thought that you were speaking of abstract class first. I would rename that if possible. Same for rmi.engine, prefer something like org.myproject, to avoid confusion with internal class of the JDK.

这篇关于Java RMI代理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆