문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top