Search code examples
javarmi

How to move objects by RMI?


Is there an easy, elegant and clever way to move objects between different RMI instances?

At the moment I would perform this task by cloning the object in the receiving RMI server and then destroying the original in the sending RMI client (or other way around when moving from RMI server A to RMI server B).

I guess, I will also have to clone and destroy all objects of the object?


Solution

  • If your object implements "Remote" interface, it is passed by reference.

    If your object doesn't implements "Remote" interface, it is passed by value.

    You have to take a decision depending on type of the object.

    Java documentation

    Passing Objects in RMI

    Arguments to or return values from remote methods can be of almost any type, including local objects, remote objects, and primitive data types.

    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 fields that are marked static or transient. Default serialization behavior can be overridden on a class-by-class basis.