user1413793
user1413793

Reputation: 9347

Java RMI - Understanding the Oracle Tutorial

I am trying to understand Oracle's RMI Tutorial for java and I had some questions. For reference the tutorial is linked here:

1) In the server example, the last line of the main function is: System.out.println("ComputeEngine bound");. Normal execution would say that at this point, the main function exits and the program terminates. However, something is causing the program to block. Does anyone know why the program blocks (rather than exiting after printing compute engine bound) when you run the server?

2) In relation to the previous question, it would seem the server is blocking and listening on a port. What port is it listening on? How does the RMIRegistry know what port the server is listening on?

3) My other question is when the client gets the stub from the RMIRegistry (using registry.lookup) and then calls executeTask on this stub, does the computation happen on the machine running the RMIRegistry, or the machine running the server code? i.e. does the stub tell the RMIRegistry to run executeTask on a computeEngine instance living in the registry or within the server's main function?

Upvotes: 0

Views: 679

Answers (2)

user207421
user207421

Reputation: 310840

  1. RMI creates an accepting thread for every port it is listening on. Those threads only exit when all the remote objects exported on the corresponding ports have been unexported, either explicitly or via DGC.
  2. It is listening on whatever port you specified when constructing or exporting the remote object, or a system-allocated port if you didn't specify one, or specified zero. The Registry doesn't 'know' what port that is, but the stub does.
  3. The computation happens on the host the remote object was exported from, but normally that's the same host as the Registry, as it is difficult (but not impossible) to bind a remote object to a Registry in a different host.

Upvotes: 0

Rocky Pulley
Rocky Pulley

Reputation: 23301

RMI creates background listening threads. The program will not terminate until those threads die.

It specifies 0 as the port, which means it uses a random port which the OS defines the range for.

The stub encapsulates a TCP/IP call to notify the server to call that method. So that actual implementation runs on the server hosting the code. If you put a println on the server code you will see it print out when that function is called.

Upvotes: 2

Related Questions