Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RMI-How does passing a remote object through a remote method work?

Tags:

java

rmi

As I understand it, once I've set up an RMI communications link between two systems, I can pass an object that implements "Remote" into one of the remote methods that takes an object of that type and the far-end will just get the remote interface for the new object (in other words, it will become a new remote connection as opposed to just serializing the object over.)

Is this correct?

If so, I assume this has something to do with the method signatures--but I'd like to know exactly how it determines that it should create a new remote object instead of simply serializing the entire object.

This is really hard to put into words. Let me try this:

Let's say I have a client and a server system. On the server system I create and publish an RMI object, on the Client system I retrieve the interface and can interact with the Server system.

Client                            Server
Object1 RemoteIface     ----   Object1 Implementation

So far so good. On Client, Object1.remoteMethod() will execute on Server (after serializing over the parameters).

Now, here's the question, on the client I execute code like this:

Object2 object2=new object2(); // Also a remote object
object1.send(object2); 

As I understand it, at that point, my system will have a new communications mechanism:

Client                           Server
Object1 RemoteIface    -----    Object1 Implementation
Object2 Implementation -----    Object2 RemoteIface    

At this point, if the server calls a method on Object2, that method will actually be executed on the client.

I'm wondering at what point the system decides to do this rather than just serializing it (as it would with any non-remote object).

Or am I completely wrong and it just serializes it over and I need some kind of "getRemoteInterface()" method call to actually create the remote call?

like image 241
Bill K Avatar asked Apr 21 '09 22:04

Bill K


1 Answers

It's what's called the Proxy Pattern. The thing instantiated at the remote end has generated code to transfer values and activate methods.

java.rmi.Remote itself is just a "tagging interface". You need an implementation to do the actual work. Have a look at this JavaCamp tutorial for more.

Update: okay, going the other direction, yes, you need to serialize the object and pass if over the wire. Probably the best thing is to work through the Java Tutorial on RMI first. But, basically, Serializable is another tagging interface that tells Java the object is prepared to be turned into an internal string format, kind of like XML or YAML. That format can be "rehydrated" into a matching object at the receiving end so long as the class files for that object are available.

like image 139
Charlie Martin Avatar answered Oct 19 '22 23:10

Charlie Martin