Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RMI对象静态和非静态字段?_Java_Static_Rmi - Fatal编程技术网

Java RMI对象静态和非静态字段?

Java RMI对象静态和非静态字段?,java,static,rmi,Java,Static,Rmi,所以我有这个代码: public class RemoteImpl extends UnicastRemoteObject implements TestRemote { private static final long serialVersionUID = 1L; private static int counter = 0; private int localizedCounter = 0; protected RemoteImpl() throws R

所以我有这个代码:

public class RemoteImpl extends UnicastRemoteObject implements TestRemote {

    private static final long serialVersionUID = 1L;
    private static int counter = 0;
    private int localizedCounter = 0;

    protected RemoteImpl() throws RemoteException {
        super();
    }

    @Override
    public int getMeGlobalCounter() throws RemoteException {
        counter++;
        return counter;
    }

    @Override
    public int getMeLocalizedCounter() throws RemoteException {
        localizedCounter++;
        return localizedCounter;
    }
}
对于我的客户,我正在尝试:

public class TestClient {

    public static void main(String[] args) throws Exception {
        Registry registry = LocateRegistry.getRegistry("localhost", Constant.RMI_PORT);
        TestRemote remote = (TestRemote) registry.lookup(Constant.RMI_ID);
        System.out.println("Global counter:" + remote.getMeGlobalCounter());
        System.out.println("Localized counter:" + remote.getMeLocalizedCounter());
    }

}
运行此代码两次后,我希望看到:

Global counter:3
Localized counter:1
但是我看到

Localized counter:3
那么,为什么每次我调用这个方法时都不重置本地化计数器呢?我不是每次都有新的东西吗

我不是每次都有新的东西吗


不,你不是。您得到的实例与绑定到注册表的实例相同。RMI不只是随意创建远程对象。

我如何才能实现我正在尝试的目标?我不知道你在尝试什么。如果您希望每次查找都获得一个新的远程对象,则需要修改您的期望。我希望每次运行客户端时都获得一个新对象。然后您必须自己编写代码。注册表中需要一个单例远程对象,该对象导出一个方法,该方法创建另一个远程对象的新实例,该远程对象实现另一个远程接口。有点像远程工厂模式。非常浪费资源。我会自己尝试改变这个问题。