How to unload/kill rmiRegistry if app was terminated abnormally
Hello,
I use the following method in coding to [1] launch the RMIRegistry to listen on port PORT, [2] Exports the remote object to make it available to receive incoming calls, [3] Bind the service name to the Registry
int PORT = 1101;
String HOST_NAME ="myHost";
String SERVICE_NAME ="myRMIService";
LocateRegistry.createRegistry( PORT );
UnicastRemoteObject.exportObject( (MyRMIInterface)MyRMIObj );
Naming.rebind("//" + HOST_NAME +":" + Integer.toString( PORT ) +"/" + SERVICE_NAME, MyRMIObj);
However, If I the application was terminated abnormally and then restart the application, I always got the following exceptions separately (which I think the application will try to launch the RMIRegistry again and hence cause the following exceptions)
First Exception:
java.rmi.server.ExportException: internal error: ObjID already in use
at sun.rmi.transport.ObjectTable.putTarget(Unknown Source)
at sun.rmi.transport.Transport.exportObject(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.exportObject(Unknown Source)
at sun.rmi.transport.tcp.TCPEndpoint.exportObject(Unknown Source)
at sun.rmi.transport.LiveRef.exportObject(Unknown Source)
at sun.rmi.server.UnicastServerRef.exportObject(Unknown Source)
at sun.rmi.registry.RegistryImpl.setup(Unknown Source)
at sun.rmi.registry.RegistryImpl.<init>(Unknown Source)
at java.rmi.registry.LocateRegistry.createRegistry(Unknown Source)
....
Second Exception:
java.rmi.server.ExportException: Port already in use: 1101; nested exception is:
java.net.BindException: Address already in use: JVM_Bind
at sun.rmi.transport.tcp.TCPTransport.listen(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.exportObject(Unknown Source)
at sun.rmi.transport.tcp.TCPEndpoint.exportObject(Unknown Source)
at sun.rmi.transport.LiveRef.exportObject(Unknown Source)
at sun.rmi.server.UnicastServerRef.exportObject(Unknown Source)
at sun.rmi.registry.RegistryImpl.setup(Unknown Source)
at sun.rmi.registry.RegistryImpl.<init>(Unknown Source)
at java.rmi.registry.LocateRegistry.createRegistry(Unknown Source)
......
Caused by: java.net.BindException: Address already in use: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createServerSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createServerSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPEndpoint.newServerSocket(Unknown Source)
... 11 more
I don't know how to [1] free the RMIRegistry from listening on PORT and [2] also to terminate it from the background (it seems that RMIRegistry was executed in the Windows Task background).
Has anyone got any experiences on dealing with this issue?
Thanks in advance!

