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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
jezhilvalan
  • 331
  • 1
  • 7
  • 14
  • Check this answer: [How to close rmiregistry running on particular port?](https://stackoverflow.com/questions/8386001/how-to-close-rmiregistry-running-on-particular-port) – canislatrans May 04 '19 at 18:59

3 Answers3

1

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
  • 16,501
  • 2
  • 28
  • 35
0

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!

Taryn
  • 242,637
  • 56
  • 362
  • 405
Vivek
  • 9
  • 1
  • 1
    The *result* of a *prior* `createRegistry()` can be unexported. To ensure it's running, try `createRegistry()`: if you get the `BindException`, call `getRegistry()`. Of course you can only unexport in the former case, but then in the latter case it's someone else's Registry so they are responsible for unexporting it. – user207421 Dec 23 '11 at 03:50
-1

I have found a way of shutting down the registry from any process (and for that matter shutting down any bound processes which are bound in the registry)

Any of the Interfaces which extends remote and which you eventually want to kill off should also extend the following Interface:

public interface PIDSupplierInterface extends Remote {
  String getPID() throws RemoteException;
}

every server class you create with this as part of its interface must then implement getPID(). The thing you then have to do is return the process ID. Google "getpids" for Windows, or go here: www.jroller.com/santhosh/entry/get_current_java_process_id. For Linux as I understand it getting the PID is more straightforward. Then (in Windows) you want to go

String PID = myServer.getPID();
String[] a_command = { "taskkill", "/pid", PID };
Runtime.getRuntime().exec(a_command, envp, dir);

to kill off the PID of the registry itself, first, when starting the registry (programatically), simply go

PIDSupplierInterface stub = PIDSupplierInterface)UnicastRemoteObject.exportObject( 
 new PIDSupplierServer(), 0);
reg.bind( "regKiller",  stub );

where PIDSupplierServer is a class which implements only PIDSupplierInterface.

Then, when you want to kill off the RMI registry from any Process just go

PIDSupplierInterface regProcess = (PIDSupplierInterface)reg.lookup( "regKiller" );
String regPID = regProcess.getPID();
String[] a_command = { "taskkill", "/pid", regPID };
Runtime.getRuntime().exec(a_command, envp, dir);

the reg has disappeared from your system. Or is your question more complicated for some reason? Any comments welcome.

mike rodent
  • 14,126
  • 11
  • 103
  • 157
  • How exactly are you going to persuade the RMI Registry to implement `PIDSupplierInterface`? and embody `PIDSupplierServer`? – user207421 Dec 23 '11 at 03:52