• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

RMI pass by value or by reference ?

 
author
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 284
Netbeans IDE Firefox Browser Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Francesco.

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


regards
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Posts: 194
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    reply
      Bookmark Topic Watch Topic
    • New Topic