wood burning stoves*
The moose likes EJB and other Java EE Technologies and the fly likes Can we pass this object on wire? 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 » Java » EJB and other Java EE Technologies
Bookmark "Can we pass this object on wire?" Watch "Can we pass this object on wire?" New topic
Author

Can we pass this object on wire?

Ram Dhan Yadav K
Ranch Hand

Joined: Aug 13, 2001
Posts: 321
If an object implements java.rmi.Remote, can it be passed on the wire? or does it have to implement Serializable interface also?
thanks,


Ram Dhan Yadav (SCJP, SCWCD, SCJA-I, IBM EC(483))
"We are what we repeatedly do. Excellence, then, is not an act, but a habit."
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
It must implement Serializable.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Matjaz Juric
Author
Ranch Hand

Joined: Aug 02, 2001
Posts: 65
Hi Ram,
Originally posted by Ram Dhan Yadav K:
If an object implements java.rmi.Remote, can it be passed on the wire? or does it have to implement Serializable interface also?

If a class implements Remote interface, objects cannot be sent over the wire by value. Rather references are sent. To send the object by value, the class has to implement the Serializable interface.
However, a single class cannot implement both interfaces.
Cheers,
Matjaz


Matjaz Juric<br />Author of <a href="http://www.amazon.com/exec/obidos/ASIN/186100544X/qid%3D1008676221/sr%3D8-1/ref%3Dsr%5F8%5F5%5F1/103-4928879-8274265" target="_blank" rel="nofollow">Professional J2EE EAI</a> and <a href="http://www.amazon.com/exec/obidos/ASIN/1861005083/ref=ase_electricporkchop" target="_blank" rel="nofollow">Professional EJB</a>
Ram Dhan Yadav K
Ranch Hand

Joined: Aug 13, 2001
Posts: 321
Hi Matjaz,
However, a single class cannot implement both interfaces.
Why can't a single class cannot implement both interfaces? Its perfectly legal to implement more than one interface in java right? What are the problems faced with this?
thanks,
Matjaz Juric
Author
Ranch Hand

Joined: Aug 02, 2001
Posts: 65
Originally posted by Ram Dhan Yadav K:
Why can't a single class cannot implement both interfaces? Its perfectly legal to implement more than one interface in java right? What are the problems faced with this?

A class which implements the Remote interface (actually implements an interface which inherits from Remote) is a distributed RMI object which provides services in the network. Serializing such an object is not possible and doesn't make sense.
A class which implements Serializable is basis for a "normal" Java object, just that the persistent state can be serialized and thus can be transferred over the wire to a different JVM. Such object does not provide remote services to other objects.
Usually a RMI (Remote) object will provide methods which will accept or return serializable objects.
Cheers,
Matjaz
Ram Dhan Yadav K
Ranch Hand

Joined: Aug 13, 2001
Posts: 321
Hi Matjaz,
Thanks for the information. One more doubt, may be silly
Serializing such an object is not possible and doesn't make sense.
Is it not possible or doesn't it make sense?
thanks,
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
If a class implements Remote interface, objects cannot be sent over the wire by value. Rather references are sent. To send the object by value, the class has to implement the Serializable interface.
However, a single class cannot implement both interfaces.

This is incorrect. Look at the RemoteObject class which is in the package java.rmi.server:
http://java.sun.com/j2se/1.3/docs/api/java/rmi/server/RemoteObject.html
It implements both Remote and Serializable.
[ February 06, 2002: Message edited by: Thomas Paul ]
Matjaz Juric
Author
Ranch Hand

Joined: Aug 02, 2001
Posts: 65
Hi Thomas/Ram,
Originally posted by Thomas Paul:

This is incorrect. Look at the RemoteObject class which is in the package java.rmi.server:
http://java.sun.com/j2se/1.3/docs/api/java/rmi/server/RemoteObject.html
It implements both Remote and Serializable.

It is true, that a class can implement both, the Remote and the Serializable interface. However such object CANNOT be passed over the wire (this was the original question).
Try the following:
public interface Hello extends Remote {
public String sayHello() throws RemoteException;
}
public class HelloImpl extends PortableRemoteObject implements Hello, Serializable {
...
}
public interface Wire extends Remote {
public HelloImpl returnHelloObj() throws RemoteException;
}
public class WireImpl extends PortableRemoteObject implements Wire {
public WireImpl () throws RemoteException {
super();
}
public HelloImpl returnHelloObj() {
HelloImpl h = null;
try {
h = new HelloImpl();
} catch (Exception e) {
System.out.println(e);
}
return h;
}
}
After compilation with javac we have to generate stubs and skeletons using rmic:
rmic -iiop HelloImpl
rmic -iiop WireImpl
Here we get the following error message:
error: HelloImpl is a remote implementation class and cannot be used as a method argument or return type in Wire.
error: Class Wire contains an invalid return type.
2 errors
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
I got a clean compile and a clean rmic on this:

In fact, in your example if you remove the Wire interface and replace all references of it to the Remote interface, you get a clean rmic.This compiles and rmic's cleanly:

[ February 07, 2002: Message edited by: Thomas Paul ]
Doug Wang
Ranch Hand

Joined: Oct 05, 2001
Posts: 445
Originally posted by Matjaz Juric:
If a class implements Remote interface, objects cannot be sent over the wire by value. Rather references are sent. To send the object by value, the class has to implement the Serializable interface.
However, a single class cannot implement both interfaces.

Hi Matjaz,
What if a Remote object itself acts as a parameter to pass across machines?


Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep
Matjaz Juric
Author
Ranch Hand

Joined: Aug 02, 2001
Posts: 65
Hi Thomas,
Originally posted by Thomas Paul:
I got a clean compile and a clean rmic on this:

I think our discussion is getting very interesting. I’ve tried what you’ve recommended, however if I remove the Wire interface, how can the Client (which can be located on a remote computer, and therefore does not have access to WireImpl – it should only have access to the interface of the remote object, not its implementation) get the Hello object?
Cheers,
Matjaz
Matjaz Juric
Author
Ranch Hand

Joined: Aug 02, 2001
Posts: 65
Hi Doug,
Originally posted by Doug Wang:
What if a Remote object itself acts as a parameter to pass across machines?

If you use a Remote object as a method parameter or a return value, then a remote reference is sent. If a Serializable object is used as a method parameter or a return value, then the object is serialized and sent over the wire to the remote JVM, where it is instantiated.
Cheers,
Matjaz
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Matjaz Juric:
I think our discussion is getting very interesting. I’ve tried what you’ve recommended, however if I remove the Wire interface, how can the Client (which can be located on a remote computer, and therefore does not have access to WireImpl – it should only have access to the interface of the remote object, not its implementation) get the Hello object?
It would appear that this can only work if the client has access to the remote object class itself and not just the remote interface. Perhaps that is why Serializable/Remote is used in the java.rmi.server package?
Rajeev Gupta
Greenhorn

Joined: Nov 28, 2001
Posts: 16
Hello Friends
There seems a great discussion on implemening the Remote and Serializable interface. I also like to participate somthing here. If a class implements Serializable then the state of that Object is sent accross the network and a new Instance is created in another JVM,which is called passing by value. All method calles are made on this copy of object, which inturns are copied back to the original Object. But if a class implements the Remote interface then the Stub of that object is sent accross the network and all method call is made on that Stub, which inturns are transffered to the original object,That is why this is called call by Reference. But infact if u see all the stub as well as skeletons are themself Seriazable, that is how they are transffered across the network. This way we can say that Serialization is the BASIC technique of java for networking.
As far as it concern with implementing both the interface, The Virtual machine will have a problem as to transffer an object with what techinque(Serialization or Remote). So it will depend upon the implementation of Virtual Machine as to which technique is preffered over the other. But implemeting both interfaces can not be a trouble as it conforms to the Java Spacifications.
Thanks
Raajeev
Matjaz Juric
Author
Ranch Hand

Joined: Aug 02, 2001
Posts: 65
Hi Thomas,
Originally posted by Thomas Paul:
It would appear that this can only work if the client has access to the remote object class itself and not just the remote interface. Perhaps that is why Serializable/Remote is used in the java.rmi.server package?

I believe that java.rmi.server.RemoteObject implements both, Remote and Serializable to provide a way to generate stubs for remote objects.
Cheers,
Matjaz
Matjaz Juric
Author
Ranch Hand

Joined: Aug 02, 2001
Posts: 65
Hi Rajeev,
I agree with your explanation.
Originally posted by Rajeev Gupta:
As far as it concern with implementing both the interface, The Virtual machine will have a problem as to transffer an object with what techinque(Serialization or Remote).

As I've mentioned previously, the rmic compiler will detect some situations where this occurs and refuse to generate stubs. It would be really nice to hear whether the JVM spec says something about this – if it is really up to the JVM implementation, this may cause incompatibilities between different JVMs.
Cheers,
Matjaz
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we pass this object on wire?