Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing Objects to a Remote method

 
MadhuV sudan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am preparing for SCBCD certification using HeadFirst EJB. It was mentioned that if you pass an object to a remote method, the actual object gets passed instead of a copy of the reference. But I would like to know what is the difference between passing the entire object and passing the copy of the reference?
 
Jef Cumps
cowbird
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When calling a method on a Remote object, not your actual client-side object nor it's handle are passed. It's a (serialized) COPY of your client-side that is passed.

Remember that when you're working remote, the client and the server are running in different virtual machines, so the objects live on different heaps. That's why it's impossible to pass the handle or a copy of the handle. On the other side, in the other VM, there would simply be no object to reference to...

Hope this helps! Greetz
 
Jef Cumps
cowbird
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oops, typo..

I meant: It's a (serialized) COPY of your client-side ** OBJECT ** that is passed.
 
MadhuV sudan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jef..thanks for u'r reply..
What I wanted to know was the difference between passing the reference and the passing the object as a whole..In both the cases if we change the argument passed it is ultimately going to affect the object..
(Ex)..Assume..
public void modifyObject(Dog d) {
d.setColor("Brown");
}

What difference does it make if the argument "d" is a copy of the reference or is it a serializable object? Or is there really a difference?If we modify the attribute "color" of the object "Dog" in both the cases(local/remote method) the final object will be modified..right?

I had one more doubt..
Let us consider the same method both as local as well as remote..

public Dog modifyObject(Dog d) throws NotValidColorException {
d.setColor("Brown");
if (x)
throw new NotValidColorException("Invalid Color");
return d.
}

First let us assume this method is a local method..If the "NotValidColorException" exceptions gets thrown, the "Dog" object referenced by the client will have the color set to "Brown"..

If we assume the above as a remote one(i.e., present in bean implementation class) and the "NotValidColorException" exception is thrown the object "Dog" reference by the client will not hold the color as "Brown"..Why??
 
Jef Cumps
cowbird
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, assume you are the client calling the method modifyDog(Dog d) on a Remote server somewhere. You create your own client side Dog-object to use as argument. Your question is 'can't I just pass the reference instead of the (copied) object?"

The clue to this one is that you cannot pass the reference to the remote server because there is no Dog object living on the server's heap (remember, Remote means other VM) to reference to. There simply is no Dog object ready on your server to let the reference point to or anything like that... That's why you have to give your server the complete Dog object as a copy of your own client side object. You see?

When you've understood this, it will be clear that you have to return the modified Dog object to the client in order to get the changes made by the server... So in your second example, remote and local use of the modifyDog() method will behave different. When used remote, the Dog on the server will be made brown, but not the client side Dog... see it?

Hope it's all getting clearer ! ;-)
 
MadhuV sudan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey jef..thanks for the reply..it is crystal clear now..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic