RMI: LocateRegistry.getRegistry(int port)

G

Guest

Hi,

I wonder why the method "LocateRegistry.getRegistry(int port)" doesn't throw
an exception if no registry is available?

This seems a really bad design to me. I allways get a registry with this
method (even if no registry has been created) but when I try to bind some
object I get an exception, no wonder, there is no registry, so why is this
not notified by the getRegistry method???

See the code bellow for details (I'm using j2sdk1.4.2_02 / Windows XP):

[BEGIN CODE]

import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;

public class StupidRegistry extends UnicastRemoteObject implements
DummyRemoteInterface {

protected StupidRegistry() throws RemoteException {
super();
}

public static void main(String[] args) {
Registry reg = null;
try {
reg = LocateRegistry.getRegistry(12321);
} catch (RemoteException e) {
System.out.println("couldn't get the registry, nice to be
notified of this!");
}
// well, it seems we got the registry, let's bind something...
StupidRegistry sr = null;
try {
sr = new StupidRegistry();
} catch (RemoteException e) { }
try {
reg.bind("I guess I won't be bound", sr);
} catch (AccessException e) {
} catch (RemoteException e) {
System.out.println("Well, I guess there is no registry...");
System.exit(-1);
} catch (AlreadyBoundException e) {
}
}
}

interface DummyRemoteInterface extends Remote { }

class StupidRegistry_Stub extends RemoteStub implements
DummyRemoteInterface, Remote {
public StupidRegistry_Stub() {
super();
}
public StupidRegistry_Stub(RemoteRef ref) {
super(ref);
}
}

[END CODE]

This will allways prints "Well, I guess there is no registry...". (Unless
you've got a registry allready running and listening to the used
port...)

I really don't see why the getRegistry method doesn't throw any exception. I
do
actually expect that exception. Why do I get a non usable registry? What
is this good for?

Is there any way to check that the registry returned by the getRegistry
method is usable before trying to bind anything to it ?


Greetings,

Mike
 
E

E.J. Pitt

Mike said:
I wonder why the method "LocateRegistry.getRegistry(int port)" doesn't throw
an exception if no registry is available?

The reason is the bootstrap problem. A remote reference can only be
acquired as the result of a remote method, and a remot method can only
be called via a remote reference.

The Registry is only there to provide the necessary bootstrap to get out
of this loop, otherwise it wouldn't really be necessary.

This means that the stub returned by LocateRegistry.getRegistry() is
*not* the result of a remote method, it is synthesized locally using the
materials you provided: the host and the port. It is not guaranteed to
represent any real existing Registry, you have to suck it and see.
Is there any way to check that the registry returned by the getRegistry
method is usable before trying to bind anything to it ?

If you don't like bind(), you could try to list() it, but there's no
real point, you're better off just catching the exception from bind()
and saving the extra (RMI) call.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top