Search code examples
javarmiiiop

How to pass the remote reference of an exported RMI-IIOP object


Let's say I want to pass the remote reference of the following RMI-IIOP exported object to a receiver (which is another remote object):

public interface MyInterface extends Remote
{
    public void send(Receiver receiver);

    public String sayHello();

}

public class MyObject implements MyInterface
{
    public MyObject()
    {
        PortableRemoteObject.exportObject(this); // I know I could extend PortableRemoteObject instead
    }

    public void send(Receiver receiver)
    {
        // which one is correct?

        /* 1. */ receiver.receive(this);
        /* 2. */ receiver.receive(this.toStub());
        /* 3. */ // other things, such as narrow...
    }

    public String sayHello() { return "hello"; }
}

and this is the implementation of receive method:

public void receive(Remote remote)
{
    MyInterface myObjectRef = (MyInterface) PortableRemoteObject.narrow(remote, MyInterface.class);
    System.out.println(myObjectRef.sayHello());
}

the target is the correct remote invocation of the sayHello() method.

Thanks.


Solution

  • They are equivalent. The semantics of RMI provide that exported remote objects are passed as their own stubs.