why java RMI can't get return value by reference
Asked Answered
S

3

1

In RMI, I can only get return value by

InetSocketAddress address = new InetSocketAddress(hostname, port);
Server server = Stub.create(Server.class, address);
int return = server.getValue();

But, I can't get it by

public class Return {
    int value;
}
InetSocketAddress address = new InetSocketAddress(hostname, port);
Server server = Stub.create(Server.class, address);
Return return = new Return();
server.getValue(return);

I know arguments will be serialized and deserialized, but that's not my question, my question is "why can't Java emulate a pass-by-reference as a pass by in-out, as was done in C with RPC?", I think it's related to java environment. By in-out I mean in C with RPC, you can get return value by

int return;
rpc.getValue(&return);

Hope now my question is clear.

Space answered 28/2, 2012 at 17:7 Comment(3)
What class are you calling these getValue methods on? RMI shouldn't require you to specifically get the return value as a separate step, you just call methods on the interface which return a result just as calling a method locally would. I'm afraid I don't really understand your question as it stands.Bianca
@Andrzej Doyle I have modified code, please check again, sorry for the confusion.Space
"I know arguments will be serialized and deserialized" ... and return values; in both cases unless the object concerned is an exported remote object. This is documented.Mccowyn
S
4

Returning additional object proxies would pose an implementation challenge. As things stand, there's only one way to create a remote object. if methods are allowed to spawn more remote objects just by returning regular objects, then all those calls need to be intercepted, objects properly catalogued, etc.

In other words, the Java people - unlike , e. g. DCOM people - decided not to do the extra plumbing. And that will always be the answer to the "why is system A unlike system B" quesions.

Salo answered 28/2, 2012 at 17:31 Comment(0)
N
1

A reference is a memory address on your computer. With RMI, you are trying to pass that memory address to (potentially) another machine, where that memory address won't make sense in that context. The other machine will not now how to interpret that address. This is only why values can be passed.

Nostoc answered 28/2, 2012 at 17:9 Comment(2)
Thanks, but as I said, in RPC, it can be achieved, after invoking the function, values will be "write back" to the reference variable as far as I know.Space
I disagree. A reference is a way to refer to an object. It can be implemented straightforwardly using the object's memory address (in e.g. C++ heap), but may just as wel be implemented as a path /machineX/objectstore/socketxyz or an opaque integer object-ID. All of these references can be passed to other machines.London
S
0

When you pass arguments to a method by "pass by reference" you are passing the reference, which is not possible in remote calls. And remember that Java only supports pass by values (even object references).

You may say the RMI runtime can deserialize all arguments back to the client, but this will be the problem:

Suppose you have a Long as an argument, so the java runtime will serialize it to server, then what? Shall it deserialize it? remember that Longs are immutable.

If Java runtime creates another instance of Long, then how can it update all instances referring to old Long (which was passed as an argument)?

Soughtafter answered 28/2, 2012 at 17:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.