Search code examples
javarmi

what does the constructor of UnicastRemoteObject class do


I have some questions about the UnicastRemoteObject class. take the following code:

public class MaxImpl extends UnicastRemoteObject implements Max{
public MaxImpl () throws RemoteException {}
    @Override
    public int getMax(int[] A) throws RemoteException {
     // ...
        return max;
    }
public static void main(String[] args) {
        try {
            LocateRegistry.createRegistry(1099);
            MaxImpl max  = new MaxImpl();
            Naming.rebind("maximum", max);
        } catch (RemoteException ex) {
            System.out.println(ex.getMessage());
        } catch (MalformedURLException ex) {
            System.out.println(ex.getMessage());
        }
    }

}

what does the following statement do:

MaxImpl max  = new MaxImpl();
  1. generate a stub.
  2. generate a stub and create a remote object so that it can receive invocations of its remote methods from remote clients.

the above code is executed indefinitely, Why? I suppose there's a loop:

while(true){  ServerSocket server = ...; }

Solution

  • generate a stub.

    No.

    generate a stub and create a remote object so that it can receive invocations of its remote methods from remote clients.

    No.

    It exports the remote object, which consists of:

    1. Open a ServerSocket
    2. Start a thread listening at that socket.
    3. Create a stub object containing the IP address, the port used at (1), and a remote object identifier.

    Note that (1( and (2) Can be shared between remote objects using the same port so it may not happen precisely as above.

    Now the existence of the thread at (2) will prevent the JVM from exiting.