Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to pass by reference in RMI?

Tags:

java

rmi

I have read various articles about passing variables around using RMI.

Some of them say that it is impossible to pass variables by references in RMI. e.g.: this one and this one

While others says that it is possible. e.g.: this one, this one and this one

can anyone clear this up please? :)

like image 528
MBZ Avatar asked Jun 25 '12 10:06

MBZ


People also ask

Can Java be pass by reference?

Java is always Pass by Value and not pass by reference, we can prove it with a simple example. Let's say we have a class Balloon like below. And we have a simple program with a generic method to swap two objects, the class looks like below.

How are references passed in Java?

Object references are passed by value The reason is that Java object variables are simply references that point to real objects in the memory heap. Therefore, even though Java passes parameters to methods by value, if the variable points to an object reference, the real object will also be changed.

What does passing by reference mean in Java?

When a parameter is pass-by-reference, the caller and the callee operate on the same object. It means that when a variable is pass-by-reference, the unique identifier of the object is sent to the method. Any changes to the parameter's instance members will result in that change being made to the original value.


2 Answers

Important note: if pass by reference means modifying argument value inside the method and change original variable in caller, you can't. If what you want to do is passing a copy of a reference to an object, to allow the method interact with some object of yours... yes you can. The answer explores that second option.

Yes. But it has to be an RMI object. In that case a RMI stub will be passed by copy.

RMI passes arguments and return values two ways:

  1. copy the full object
  2. send a remote reference (it has to be remote if its a reference!).

Sample

Guess we have a Service. It's a RMI object, published through the RMI registry, so it's accesible to clients. Client can call a method on it (to create something) and the service wants to return a reference to that newly created object. Not a serialized copy but a reference to the created object in server memory space.

import java.rmi.Remote;
import java.rmi.RemoteException;

// normally published object
public interface MyService extends Remote
{
    // creates something and return a "handle" to it
    public MyHandle createX(SomeSerializableObj param1) throws RemoteException;
}

// interface for object that the service will return a reference to...
public interface MyHandle extends Remote
{
    void doOne();
    void doTwo();
}

In this example you could:

Create an implementation of MyService and publish it

Registry registry = LocateRegistry.createRegistry(port);
MyService stub = (MyService) UnicastRemoteObject.exportObject(server, 0);
registry.bind("myService", stub);`

and then, some RMI client could get a reference to it

Registry registry = LocateRegistry.getRegistry(server, port);
MyService serv1 = (MyService) registry.lookup("myService");

and with a reference to the service object could obtain a reference to other RMI object.

MyHandle handle1 = serv1.createX(...);
handle1.doOne()

In this example the method argument is serialized (it should be a Serializable class) while the returned object is a RMI reference to an object created in the server.

The implementation of createX(...) could be:

public MyHandle createX(...) {
   MyHandle handle = new MyHandleImpl(); // create an implementation
   createdHandlers.add(handle); // add it to some structure (just a sample)
   return handle; // return the implementation. RMI will send to client a RMI reference to this very instance
}
like image 157
helios Avatar answered Sep 30 '22 02:09

helios


Of your references:

(1) says RMI does not support pass by reference. I agree.

(2) says RMI does not support in-out parameters. I agree.

(3) just exhibits the usual confusion between pass by reference and passing references by value. RMI does the latter, just like the rest of Java.

(4) is wrong.

(5) is just plain incoherent. There is no pass-by-reference in RMI.

RMI's remote 'references-by-value' is essentially no different semantically from Java's local 'references-by-value', apart from the additional possible failure modes.

The meaning of what you have read is that for non-remote references, RMI object arguments and results are passed by value, via Object Serialization, rather than by Java's argument passing method of reference-by-value.

like image 35
user207421 Avatar answered Sep 30 '22 04:09

user207421