This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Architect Certification (SCEA/OCMJEA) and the fly likes RMI question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Architect Certification (SCEA/OCMJEA)
Bookmark "RMI question" Watch "RMI question" New topic
Author

RMI question

Dhar Desai
Greenhorn

Joined: Nov 01, 2004
Posts: 11
I took the exam and passed but there's some confusion regarding one of the questions.

If an object implements the Remote and Serializable interfaces and it's passed as an argument to a remote object, does the object itself get passed or does the stub?

The self-test question answers say the stub is passed.

Is this right?

I think it depends. If the object is exported (is this assumed by the question?) then yes, obviously, the ObjectOutputStream (or a subclass of it) replaces the object with its stub.

But if the object is NOT exported, then it's passed by value.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1175
Dhar,

http://java.sun.com/developer/onlineTraining/EJBIntro/EJBIntro.html#DistributedBeans says:

Actually, the stub is just a dumb network object that sends the requests across the network to the skeleton, which in turn invokes the method on the actual instance. The instance does all the work, the stub and skeleton just pass the method identity and arguments back and forth across the network.


So, I don't understand how the stub can be passed as an argument.

Regards,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Dhar Desai
Greenhorn

Joined: Nov 01, 2004
Posts: 11
Well, from the point of view of the client, the stub is the remote object.

If I am a client a have a remote stub for remote object A and a remote stub for remote object B, I can pass B to one of A's methods.

B b = getB(); //returns a stub with remote interface B
A a = getA(); //returns a stub with remote interface A.
a.someMethod(b); //see, here the class of object referenced
// by b is a stub implementation: the stub is what
//is passed and the stub is what is sent.

On the other hand we could do this:

B b = new SomeClassThatImpelentsRemoteInterfaceB();
export(b); //somehow exports b. This instantiates the stub and makes
//it available to the rmi runtime. Note, b is not a reference
//to the stub; it is a reference to the actual remote object.

a.someMethod(b); //the object referenced by b (the actual remote object)
//is replaced by a stub for the remote object and then sent
//over the wire after serialization.


BUT, if b is not exported and SomeClassThatImpelentsRemoteInterfaceB also implements Serializable, the the object referenced by b not be replaced by a stub: it itself will be serialized and sent. At least, that's how I think it works.
I Roberts
Ranch Hand

Joined: Dec 16, 2004
Posts: 66
The Stub and Skeleton are both references between the client and server [i.e. remote] objects. They are not passed between networks, as the above thread suggests, but are simply used to distribute messages to the correct remote object or returning to the correct invoking client object.

I am assuming that the question related to RMI-IIOP and not RMI-JRMP. The differences are that IIOP can support pass-by-reference and pass by pass-by-value while JRMP supports pass-by-value. JRMP was the original Sun venture into distributed objects but CORBA became an industry standard and Sun decided to extend RMI to use the IIOP extension.

With pass-by-value; a new copy of the remote object is created on the remote server and it modifies the object as requested.

With pass-by-reference; the remote server receives the reference to the original object on the remote server (i.e. not a copy as in pass-by-value)so any modifications are made to the original not a copy of the object.

When using RMI-IIOP and using primary types as arguments they are passed-by-value but when using an object (e.g. DTO or VO) this is passed-by-reference - so long as it is serialised.

Hope this helps!?


Ian Roberts<br />Application Architect<br />SCJP, SCJD, SCEA, OCUP Fundamental
Dhar Desai
Greenhorn

Joined: Nov 01, 2004
Posts: 11
I did some more research on the topic.

Here's a reference. See esp. section 2.6.5

http://java.sun.com/j2se/1.4.2/docs/guide/rmi/spec/rmi-objmodel7.html. This refers to RMI-JRMP.

I assume the question refers to IIOP since this is what the EJB containers use.

But as I recall, JRMP is pass-by-value and pass-by-reference. RMI-IIOP also supports both.

Stubs must be passed over the network. What happens when you use JNDI to look up a home interface? You, the client, are getting a stub. Also, the client can have a reference to a stub. You can pass this stub around just like anyother object.

Now that I think about it there are several cases to consider. Assume you have a remote method "void doSomething(Object z) throws RemoteException"

You, the client, calls doSomething and pass in an object z. I'll take an educated guess at the result: I haven't actually tried out all these scenarios. Let me know if something is inaccurate.

1) The class of z does not implement Serializable or Remote. Result: runtime exception.

2) The class of z does implement Serializable but not Remote. Result: z is packaged into a stream of bytes and sent over the wire to the remote object.

3) The class of z implements Remote, but is not Serializable. Futhermore, z has been exported as a remote object in this JVM. Result: a stub is created by the rmi runtime and sent over the wire.

4) The class of z implements Remote, but is not Serializable. Futhermore, z has NOT been exported as a remote object in this JVM. Result: a runtime error?

5) The class of z implements RemoteStub. Result: z is Serialzed and sent over the wire. If it implements RemoteStub then it is a stub object, which is Serializable.

The following scenario is why the original question is misleading:

6) The class of z implements both Serializable and Remote.
6a) z has been exported by to the rmi runtime. Result:
Send stub over wire.
6b) z has NOT been exported to the rmi runtime. Result: z is
itself serialized and sent over the wire.
Dhar Desai
Greenhorn

Joined: Nov 01, 2004
Posts: 11
"When using RMI-IIOP and using primary types as arguments they are passed-by-value but when using an object (e.g. DTO or VO) this is passed-by-reference - so long as it is serialised."

Not sure I understand this quote. A DTO is passed by reference? If it's serialzable shouldn't that be "pass-by-value"?
I Roberts
Ranch Hand

Joined: Dec 16, 2004
Posts: 66
There is very good book called "Mastering Enterprise JavaBeans" that I would recommend anyone wanting to take the SCEA to reference.

Good luck.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RMI question