Reputation: 335
I'm studying java RMI. It's OK to register a Registry in localhost. But how can I register a Registry in a remote host with specified port?
It's OK to execute this code like
LocateRegistry.createRegistry(6600);
// Register communication route
Naming.rebind("rmi://0.0.0.0:6600/PersonService", personService);
But not like
LocateRegistry.getRegistry("202.114.70.31", 6600);
// Register communication route
Naming.rebind("rmi://202.114.70.31:6600/PersonService", personService);
Otherwise a ConnectionException
will be thrown.
Upvotes: 1
Views: 6049
Reputation: 310850
how can I register a Registry in a remote host with specified port?
You can't. You can only start a Registry in the localhost.
It's OK to execute this code like
LocateRegistry.createRegistry(6600);
// Register communication route
Naming.rebind("rmi://0.0.0.0:6600/PersonService", personService);
It's not OK. You can't connect to 0.0.0.0
. It should be "localhost"
.
But not like
LocateRegistry.getRegistry("202.114.70.31", 6600);
Now that is OK. But, LocateRegistry.getRegistry()
doesn't create a Registry. It gives you a stub to an existing Registry, or to nothing if that Registry doesn't actually exist. And note that the call won't fail if it doesn't, but using that stub will fail if it doesn't.
Naming.rebind("rmi://202.114.70.31:6600/PersonService", personService);
And this is not OK. You can only bind/rebind/unbind to a Registry running in the localhost. See the Javadoc.
Otherwise a
ConnectionException
will be thrown.
No, a java.rmi.ConnectException
is thrown, and by the rebind()
, not by the getRegistry().
Upvotes: 2