Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we pass this object on wire?

 
Ram Dhan Yadav K
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If an object implements java.rmi.Remote, can it be passed on the wire? or does it have to implement Serializable interface also?
thanks,
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It must implement Serializable.
 
Matjaz Juric
Author
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ram Dhan Yadav K
Ranch Hand
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 321
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Matjaz Juric
Author
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic