1

Without having any experience with Java RMI, I have a naive question, but am still not sure about the answer after searching the internet.

Question:

To me, there are two scenarios:

Scenario-1: Start a Java program from local, during its execution, it calls a method from a class stored on a remote machine, then the class of that method would be downloaded to the local machine, and resume executing.

Scenario-2: Start a Java program from local, during its execution, it calls a method from a class stored on a remote machine, then that method will be executed on the remote machine, and the results will be sent back to the local machine. (Does this require class/object transfer?)

Which one does Java RMI use? or neither?

From Wikipedia of RPC: "RPC allows a computer program to cause a subroutine or procedure to execute in another address space (commonly on another computer on a shared network)", it seems it is the 2nd scenario.

But according to this paper Reducing Data Transfer during Remote Classloading in Java RMI, it seems it is the 1st scenario.

JackWM
  • 10,085
  • 22
  • 65
  • 92
  • What part of the linked paper make you think it is Scenario #2? – Stephen C Oct 24 '13 at 17:03
  • @StephenCSThe linked paper make me think it is Scenario #1, as it mentions remote classloading (to me, loading a remote class to local). – JackWM Oct 24 '13 at 17:06
  • 1
    Remote class loading does not mean that. It means loading the classes (the code) from a location that is remote to the location where the code is going to be run. The code moves to the RMI target object ... not vice versa. – Stephen C Oct 24 '13 at 17:54
  • @StephenC By this "The code moves to the RMI target object", do you mean the code is moved to the remote machine in RMI case? – JackWM Oct 24 '13 at 22:35

2 Answers2

1

When using RMI, you basically have an interface which is shared between a client and the server. Server must have an implementation and the client should not have this implementation. So there is no class loading or any sort of client-to-server logic transfer, client is totally unaware of the server interface implementation.

RMI serializes and transports method parameters, then the server logic is being executed, the result is serialized and transported back to client.

Andrey Chaschev
  • 16,160
  • 5
  • 51
  • 68
  • Thanks, Andrey. So it does transfer objects, right? How about global variables / class members? – JackWM Oct 24 '13 at 17:11
  • Parameters of a remote method call must be `Serializable` and [visible to both server and client](http://stackoverflow.com/questions/11736347/how-to-pass-object-in-arguments-in-rmi-method). – Andrey Chaschev Oct 24 '13 at 17:17
  • 1
    The idea is as simple as 'pack my parameters, transfer, execute method, pack the result and transfer it back'. – Andrey Chaschev Oct 24 '13 at 17:21
  • What would happen if the method to transfer calls other user methods? – JackWM Oct 24 '13 at 20:52
  • Well, it's almost impossible as `Serializable` guarantees this. It means object can be written into `ObjectOutputStream` or `byte[]` and read from `ObjectInputStream` or `byte[]`. Anyway for remote calls they usually use so called "Data Transfer Objects". Typically these are the objects having a single default constructor, public fields or prperties and no other methods. This is done to make sure situation you described never happen. You may want to look into `Serializable` [Java Doc](http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html) for more details. – Andrey Chaschev Oct 24 '13 at 21:08
0

The method will be executed on the remote machine. For this to work, the local machine downloads a class that wraps the communication between the local machine and the remote machine.

user207421
  • 305,947
  • 44
  • 307
  • 483
Oswald
  • 31,254
  • 3
  • 43
  • 68