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.
Domanda
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?
Soluzione
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.