Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Distributed Java and the fly likes Question about Stub and Skeleton 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 » Distributed Java
Bookmark "Question about Stub and Skeleton " Watch "Question about Stub and Skeleton " New topic
Author

Question about Stub and Skeleton

Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
I don't understand very good the purpose of Stub and Skeleton ,i want to be sure i understand or not, so i give you what here an example :

Remote Interface :

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RMIExample extends Remote {
public String getMessage() throws RemoteException;
}

Remote Object :

import java.rmi.*;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class RMIExampleImpl extends UnicastRemoteObject
implements RMIExample {

public RMIExampleImpl() throws RemoteException {
super();
}

public String getMessage() {
return "Hello from the RMI Server";
}
}

method main in another class to register my object in rmiregistry so that my object can be remotely accessible:

public static void main(String args[])
{
RMIExampleImpl server = new RMIExampleImpl ();
Naming.rebind("/Server", server);
}

1. When i do a lookup, i get an object reference. Then does it mean i get the 'new RMIExampleImpl' reference?
Or i'm getting the Stub ? but i don't understand , The Stub contains a reference to the 'new RMIExampleImpl' ?
I don't see the relation of my object reference that i get and the Stub ?

2.We don't care about the communication ,it means the creation of the socket is handled by the Stub and Skeleton ?

3.if we didn't have Stub and Skeleton, we must create a class with Socket for communication ? is right ?

Can you correct me please ?
Than you.
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
Engin Okucu:

I don't understand very good the purpose of Stub and Skeleton

The idea is to hide the fact that you're doing things remotely.

The stub is a local class that has the same interface as the remote class that actually does the work; this allows you to call the stub locally without having to worry about the fact that the actual work is being done remotely. The stub transparently converts its call to a message to the remote server.

The skeleton is the remote class that accepts the message and handles the corresponding call to the class, again on the remote machine, that actually does the work.

2.We don't care about the communication ,it means the creation of the socket is handled by the Stub and Skeleton ?

Socket or other communications method, yes. Depends on the specific stub & skeleton implementation you're using. I'm not familiar with the Java ones; I've only used the stub & skeleton model in a C++ project.

3.if we didn't have Stub and Skeleton, we must create a class with Socket for communication ? is right ?

Basically right, you'd have to handle the communication manually or use some other model.
Kalyan Mudumbai
Greenhorn

Joined: Apr 30, 2004
Posts: 12
Let's take your own example: RMIExample.

The communication ideally has to happen this way:

[talks to]
RMIClient ----------------> RMIExample (instance of RMIExampleImpl)

This might happens across the wire and across the JVM. So, some one has to send them from your client to the server. For this there might be a great deal of work like, connecting to the server, lookup the object, etc. etc. This work is common for most of such scenarios. Hence, the stubs and skeletons contain the common code that is required to obtain a reference to the remote object, serialize and deserialize arguments, etc. Stub acts like the messaging agent from the Client side and the Skeleton acts like the messaging agent from the Server side. So, when the client makes a call, it actually makes a call on the stub, which then sends the request to the skeleton on the server side, which in turn invokes the required method on the Server side object. So, now the above figure becomes:

RMIClient ----> RMIClientStub -----------> RMIServerSkeleton ---> RMIExample

The stub, skeleton and the RMIExample interface all will the have the same methods declared (read the disclaimer below). For example:

class RMIClientStub
{
public String getMessage()
{
//1. connect to the server
//2. look up the object (it's skeleton)
//3a. serialize the method arguments
//3b. invoke the method on the skeleton
skeleton.getMessage();
//4. deserialize the returned result
//5. return back to the client
}
}

And the skeleton looks like:

class RMIServerSkeleton
{
public String getMessage()
{
//1. listen for any stub requests
//2. deserialize any arguments
//3. invoke the method on the actual serverside object
serversideObject.getMessage();
//4. serialize the results
//5. send them back to the stub
}
}

class RMIExampleImpl implements RMIExample
{
public String getMessage()
{
//....
}
}

Disclaimer: The above implementation which I have given is just for your understanding. The actual implementation of the stubs and skeletons might be different (using reflection).

Hope this helps.

Thanks,
Kalyan.
Engin Okucu
Ranch Hand

Joined: Feb 09, 2002
Posts: 174
Warren and Kalyan, Thank you very much.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about Stub and Skeleton