Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java RMI - UnicastRemoteObject: what is the difference between UnicastRemoteObject.exportObject() and extends UnicastRemoteObject?

i'm preparing for an exam and I'm having a question that I hope someone here could answer me.

It's about RMI and remote objects. I wonder why there is so much difference between these two implementations. one is extending the UnicastRemoteObject whereas the other is exporting the object as an UnicastRemoteObject.

I don't really get the difference

Interface:

public interface EchoI extends Remote {
   public String echo() throws RemoteException
}

This is the server code (version 1):

public class EchoImpl extends UnicastRemoteObject implements EchoI {
    public EchoImpl {
        super();
    }

    public static void main (String[] args) {
        try {
            LocateRegistry.createRegistry(Registry.REGISTRY_PORT);
            StoreHouse storehouseImpl = new StorehouseImpl();
            Naming.rebind("//localhost/StoreHouse.SERVICE_NAME", storehouseImpl);
            System.out.println("Server ready");
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    public String echo() {
        return "echo";
    }
}

and this would be version 2:

public class EchoImpl implements EchoI {
    public static void main (String[] args) {
        EchoI echoService = new EchoImpl();
        EchoI stub = (EchoI) UnicastRemoteObject.exportObject(echoService, 0);
        Registry registry = LocateRegistry.getRegistry();
        registry.bind("echoService", stub);
        ...
    }
}

My question is: what is the difference between these two?

In thefirst version the registry is explicitly created, furthermore the remote object is created within a rebind?

I'm really curious, why in the first I need to create the registry myself but do not need to export the object explicitly and just rebind it using Naming. Is that object already bound to the registry before, or could I use bind instead? And what happens, if the object was not previously bound and a rebind is excecuted?

In the second version, the registry seems to be already created. Why is binding to naming the same as binding to an registry directly?

This is, what I think:

  • the first class direclty implements the interface UnicastRemoteObject which means, that at runtime the registry is created and the object is automatically exported to the RMI registry.
  • as the object is already bound to the registry, a rebind instead of a normal bind must take place.
  • the latter does all this explicitly.
like image 638
CatholicEvangelist Avatar asked Feb 03 '10 19:02

CatholicEvangelist


People also ask

What is UnicastRemoteObject in RMI Java?

The UnicastRemoteObject class defines a non-replicated remote object whose references are valid only while the server process is alive. The UnicastRemoteObject class provides support for point-to-point active object references (invocations, parameters, and results) using TCP streams.

What is UnicastRemoteObject in Java?

public class UnicastRemoteObject extends RemoteServer. Used for exporting a remote object with JRMP and obtaining a stub that communicates to the remote object. Stubs are either generated at runtime using dynamic proxy objects, or they are generated statically at build time, typically using the rmic tool.

What is Rmiregistry in Java?

rmiregistry & A remote object registry is a bootstrap naming service that is used by RMI servers on the same host to bind remote objects to names. Clients on local and remote hosts can then look up remote objects and make remote method invocations.

What is naming rebind?

rebind(String name, Remote obj) Rebinds the specified name to a new remote object. static void. unbind(String name) Destroys the binding for the specified name that is associated with a remote object.


2 Answers

There are two questions here.

  1. You can either extend UnicastRemoteObject or call UnicastRemoteObject.exportObject(). Which you do is up to you. The first is simple and automatic; the second means you can extend another class.

  2. You can either use an external RMI Registry or create it yourself inside your server JVM. Again which you do is up to you, there are advantages both ways.

    These two questions have no interaction.

  3. If you extend UnicastRemoteObject you also get the benefit of 'remote semantics' for the hashCode() and equals() methods, such that all stubs appear to be identical to the remote object that exported them, but this is of no practical use on the client side, and is really only there to support the RMI implementation itself.

like image 99
user207421 Avatar answered Sep 29 '22 23:09

user207421


java.rmi.server.UnicastRemoteObject is used for exporting a remote object with Java Remote Method Protocol (JRMP) and obtaining a stub that communicates to the remote object.

For the constructors and static exportObject methods below, the stub for a remote object being exported is obtained ...

There you should follow the Javadoc

like image 37
stacker Avatar answered Sep 29 '22 22:09

stacker