This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RMI pass by value or by reference ?

 
Francesco Marchioni
author
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
In view of the exam I did a little refresh about rmi.
As per definition "In RMI objects that implement the Remote interface are passed by reference. All other parameters are passed by copy "

So I started to code a minimal server which passes an object that implements java.rmi.Remote



Unfortunately the output isn't that I expected.

On the client I can see printed "0" and not 1000 like if
I have passed a reference of class "A" to the Server.

Can anybody shed some light on it ?
thanks
Francesco

[Andrew: tidied the code a little bit to make it easier to read]
[ November 28, 2005: Message edited by: Andrew Monkhouse ]
 
Oricio Ocle
Ranch Hand
Posts: 284
Debian Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Francesco.

Exports the remote object to make it available to receive incoming calls using an anonymous port:


regards
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11854
185
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Francesco,

I could not find the definition you mention:
As per definition "In RMI objects that implement the Remote interface are passed by reference. All other parameters are passed by copy "
However I did find in the specification:
when an exported object of type UnicastRemoteObject is passed as a parameter or return value in an RMI call, the object is replaced by the remote object's stub. An exported remote object implementation remains in the virtual machine in which it was created and does not move (even by value) from that virtual machine. In other words, an exported remote object is passed by reference in an RMI call
Or, from the RMI Tutorial:
The rules governing how arguments and return values are passed are as follows.
  • Remote objects are essentially passed by reference. A remote object reference is a stub, which is a client-side proxy that implements the complete set of remote interfaces that the remote object implements.
  • Local objects are passed by copy, using object serialization. By default all fields are copied, except those that are marked static or transient. Default serialization behavior can be overridden on a class-by-class basis.
  • That last explanation is probably the simplest, and explains the problem you are seeing.

    Just stating that class A implements Remote does not mean that it will be passed as an exported Remote object - since you have not exported it (as indicated by Oricio) Java will revert to sending a copy of the local A instance.

    So lets convert your code to export A, which means that it will pass a remote object reference:Now when we test this, we can see that we get the same value on both client and server:




    Regards, Andrew
     
    Francesco Marchioni
    author
    Ranch Hand
    Posts: 194
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for your replies.
    Actually I have read some documentation from a not official
    Java site (http://www.eli.sdsu.edu/courses/spring98/cs696/notes/rmiParameters/rmiParameters.html)

    Now it's quite clear.
    Regards
    Francesco
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic