[英]Java RMI object static and not static field?
So I have this code: 所以我有这段代码:
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;
}
}
And with my Client I am trying: 与我的客户一起,我正在尝试:
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());
}
}
After running this code for the 2 times I am expecting to see: 在两次运行此代码后,我希望看到:
Global counter:3
Localized counter:1
however I see that 但是我看到
Localized counter:3
So why is the localized counter not reset everytime I invoke this method? 那么,为什么每次调用此方法时都不会重置本地化计数器? Am I not getting a new object everytime?
我不是每次都得到一个新物体吗?
Am I not getting a new object every time?
我不是每次都得到一个新物体吗?
No you aren't. 不,你不是。 You're getting the same instance that was bound into the Registry.
您将获得绑定到注册表中的同一实例。 RMI doesn't just create remote objects willy-nilly.
RMI不仅会随意创建远程对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.