aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes rmiregistry - LocalRegistry.getRegistry() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "rmiregistry - LocalRegistry.getRegistry()" Watch "rmiregistry - LocalRegistry.getRegistry()" New topic
Author

rmiregistry - LocalRegistry.getRegistry()

John Deal
Greenhorn

Joined: Sep 07, 2006
Posts: 20
I am at the beginning of getting RMI implemented on my server and ran into the following problem. I use LocalRegistry.getRegistry(Registry.REGISTRY_PORT) to check if rmiregistry is already running assuming I will get a RemoteExecption() if it is not (unable to create a reference to it). If I get a RemoteException() I then do LocateRegistry.createRegistry(Registry.REGISTRY_PORT) to start rmiregistry.

So far it seems getRegistry() is "successful" if rmiregistry is running or not. If I manually start rmiregistry then start the server, getRegistry() does invoke an exception and my later Naming.rebind() method fails. If rmiregistry is not running getRegistry() still does not fail and of course my rebind() fails. However, if rmiregistry is not running and I do a createRegistry(), the creatRegistry() is "successful" and my rebind() works. However, if I start rmiregistry manually then invoke createRegistry(), it gets the expected RemoteException.

I am a bit confused by this. I could just do createRegistry() and if I get a RemoteException assume that rmiregistry is already running. This does not seem to be a very clean approach assuming that there are other reasons that createRegistry() can invoke RemoteException.

Any ideas? Thanks!


SCJP 1.4, SCJD 1.5
Liviu Carausu
Ranch Hand

Joined: Oct 07, 2004
Posts: 157
Hi
This is from javadoc spec of java.rmi.registry.LocateRegistry
<p> Note that a <code>getRegistry</code> call does not actually make a
* connection to the remote host. It simply creates a local reference to
* the remote registry and will succeed even if no registry is running on
* the remote host. Therefore, a subsequent method invocation to a remote
* registry returned as a result of this method may fail.


I'm doing like this : to be sure that a rmi registry runs on a host , on the registry obtained by the call , I call a convenience method, for example .
If I have no exception, I assume that the registry is running. If there is no registry running, invoking this method will throw an exception.

I'm not very happy with this solution. Does anybody has some better idea ?


Oracle Certified Master Java SE6 Developer(SCJD),
OCE JEE 6 JSP and Servlets Developer.
John Deal
Greenhorn

Joined: Sep 07, 2006
Posts: 20
Hello Liviu. I implemented your suggestion of using list() to test the registry stub but now I am having some rmic and ClassNotFound issues with my stub that maybe influencing this problem. After I solve that I will try to resolve this! Thanks.
Mark Smyth
Ranch Hand

Joined: Feb 04, 2004
Posts: 288
Originally posted by John Deal:
Hello Liviu. I implemented your suggestion of using list() to test the registry stub but now I am having some rmic and ClassNotFound issues with my stub that maybe influencing this problem. After I solve that I will try to resolve this! Thanks.





This is the code I used createRegistry to programatically start the rmi registry as this was a requirement of my project. There is no need to call UnicastRemoteObject.exportObject when you call this method.

I don't bother to verify that it starts because if it doesn't then the getRegistry will throw an exception, give an error message to the client and then exit the application.

Mark.


SCJP<br />SCJD
Liviu Carausu
Ranch Hand

Joined: Oct 07, 2004
Posts: 157


I don't bother to verify that it starts because if it doesn't then the getRegistry will throw an exception, give an error message to the client and then exit the application.



If the rmi registry already runs at the specified port, I think it must be used. Or ? Should we just fail because we cannot start our own ?
Mark Smyth
Ranch Hand

Joined: Feb 04, 2004
Posts: 288
Originally posted by Liviu Carausu:


If the rmi registry already runs at the specified port, I think it must be used. Or ? Should we just fail because we cannot start our own ?


Well I don't think that you can rely on the examiner starting the registry for the purposes of this exam although the instructions are not clear in this regard, it is probably best to start it yourself. If the port is already in use we can always select another one to try!

Mark.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: rmiregistry - LocalRegistry.getRegistry()