Tuesday, July 22, 2008

"Shutting down" the RMI Registry

This blog is a quick tip to shut down the RMI registry. Something I encountered just now while writing the tearDown() method for a test case.

When a RMI registry has already been created (via LocateRegistry.createRegistry()), re-creating it will give you an ExportException:
java.rmi.server.ExportException: internal error: ObjID already in use
at sun.rmi.transport.ObjectTable.putTarget(ObjectTable.java:169)
at sun.rmi.transport.Transport.exportObject(Transport.java:74)
at sun.rmi.transport.tcp.TCPTransport.exportObject(TCPTransport.java:229)
at sun.rmi.transport.tcp.TCPEndpoint.exportObject(TCPEndpoint.java:393)
at sun.rmi.transport.LiveRef.exportObject(LiveRef.java:129)
at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:190)
at sun.rmi.registry.RegistryImpl.setup(RegistryImpl.java:92)
at sun.rmi.registry.RegistryImpl.<init>(RegistryImpl.java:78)
at java.rmi.registry.LocateRegistry.createRegistry(LocateRegistry.java:186)
at org.mule.transport.rmi.RmiMessageReceiverTestCase.registerRmi(RmiMessageReceiverTestCase.java:126)

Yes, you can't create the same registry twice, therefore you may want to dispose the previous one after every test (not actually necessary, as you could just share the same instance for all tests). An RMI registry can be disposed with the UnicastRemoteObject.unexportObject() method, by passing the Registry object to the method argument. Take note that LocateRegistry.createRegistry() and LocateRegistry.getRegistry() give you a stub to the remote object as a different object, so please use the same reference returned from the createRegistry() method.

- yc

2 comments:

Scott Conger said...

Thanks, I was having trouble figuring out how to do that myself.

Neelambaran said...

Thanks, it was really helpful...