繁体   English   中英

Java RMI对象是静态字段还是非静态字段?

[英]Java RMI object static and not static field?

所以我有这段代码:

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不仅会随意创建远程对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM