tags:

views:

472

answers:

3

Using RMI to pass String object from WebAppA to WebAppB.WebAppB is the RMIServer whereas WebAppA is RMIClient.I have added ContextListener in WebAppB, so that the rmi service starts right away when the context is initialized in tomcat.And in the contextDestroyed method of tomcat I am trying to close/shut down rmi using the following statements:

unexportObject(remoteObj,true);
LocateRegistry.getRegistry(3232).unbind("MessagePath"); //MessagePath - name of the remote reference

But even after the execution of the aforeseen statements, rmi is listening for incoming requests at port 3232.I saw that by using "netsat -ano" in command prompt.Please do help me to close RMI Service.

A: 

The code fragment you posted does not close the registry, it only unbinds a specific object from the registry.

I think you can do what you need by passing a reference to the registry to the UnicastRemoteObject.unexportObject method.

So create the registry in your context listnever like this:

LocateRegistry.createRegistry(3232);  // create the RMI registry

And during context shutdown, try this:

Registry registry = LocateRegistry.getRegistry(3232);
UnicastRemoteObject.unexportObject(registry, true);
skaffman
Thanks for answering.I did the same way you told me.But "java.rmi.NoSuchObjectException: object not exported" is thrown.Code given in ContextInitialized method."new RMIServer();"RMIServer is a class which extends UnicastRemoteObject and implements Remote interface.within RMIServer constructor "Registry r=LocateRegistry.createRegistry(3232);r.rebind("RemoteReference",this):" are given.From "contextDestroyed" method "unbind()" method of RMI server is invocated.Within "unbind()" method "Registry r = LocateRegistry.getRegistry(3232);UnicastRemoteObject.unexportObject(r,true);" are given.
jezhilvalan
A: 

getRegistry returns a stub only, so use the instance returned by createRegistry in unexportObject. However in my case this does not help either - the registry is not active anymore, but the sockets are still open and listening :-(

siddhadev
A: 

createRegistry won't work when you're trying to shutdown the registry.

Registry registry = LocateRegistry.createRegistry(3232);

This will throw a BindException when the registry is already running... So you cannot the create object to use it in

UnicastRemoteObject.unexportObject(registry, true);

However, even if you use

Registry registry = LocateRegistry.getRegistry(3232);

you just get the stub, which cannot be used as a parameter to unexport the object.

The reason its a cause of concern for me is because I only wish to start the registry if I could check it has not started yet.. and I haven't found a way to do that!

Vivek