closing rmi registry

余生长醉 提交于 2019-12-06 05:07:40

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 :-(

Vivek

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!

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!