I am working on an internal Java RMI exercise by looking at the Sun RMI tutorial and I only understand what is going on. There are a few things that seem strange to me, and I would like to know what happens before I give feedback on this tutorial.
In the tutorial, I wrote the following components:
- An interface, say RemoteInterface, with the methods we want to access remotely.
- A class, say, RemoteServer that implements RemoteInterface, which in its constructor:
- Creates a registry object by calling
LocateRegistry.createRegistry(PORTNO) - Creates a RemoteInterface stub object by calling
UnicastRemoteObject.exportObject(this, PORTNO) and UnicastRemoteObject.exportObject(this, PORTNO) result. - Binds a stub to the registry using
registry.rebind(BINDNAME, stub)
- A class, say ServerDemo, that has a main method that simply creates an instance of RemoteServer.
- The client class that calls the server using BINDNAME and everything works fine.
Firstly, I noticed that:
- The server continues to work after executing the main method. I have to finish it explicitly. In my opinion, it should just create an object and then finish, but the program does not end, and the client can still connect.
- If I call
registry.unbind(BINDNAME) in the RemoteServer constructor, the client stops working, but the server still does not shut down. - If I call
UnicastRemoteObject.unexportObject(this, true); in the RemoteServer constructor, the server immediately exits.
I would like to know why this is happening - although if it weren’t, the server would be useless since it stopped immediately. (In my reviews, I'm going to suggest using the RemoteServer binding methods and disabling the ServerDemo methods for calling, rather than doing all the binding in the constructor, never disconnecting and relying on the server performing the launch for some reason - but first, I’d like to understand why this is happening.)
Secondly, I noticed that I can call UnicastRemoteObject.exportObject(this, PORTNO) before calling LocateRegistry.createRegistry(PORTNO) , and it still works. I suggested that the registry would need to be set up before we could export it, but obviously it does not work the way I thought.
I would like some information on how exportObject works. (I think this is due to the previous point - why does an object exception occur to stop program termination?)
Also, if I export and bind the stub in one way, ready to disconnect and untie it with another method, I would like to know whether to explicitly specify a link to the stub between. (Only BINDNAME is needed to unleash.) One of my colleagues reported problems with the GC, and although I myself did not notice, I cannot exclude them because I really do not understand what is happening.
source share