Java RMI - Understanding the Oracle Tutorial

北城余情 提交于 2019-12-13 07:49:13

问题


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

  • server code: http://docs.oracle.com/javase/tutorial/rmi/implementing.html
  • client code: http://docs.oracle.com/javase/tutorial/rmi/client.html

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?


回答1:


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.




回答2:


  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.


来源:https://stackoverflow.com/questions/26169097/java-rmi-understanding-the-oracle-tutorial

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!