我想以编程方式停止RMI注册表。我认为这对于在类stop()
或shutdown()
上调用某些LocateRegistry
或Naming
方法一样微不足道,但是没有这样的方法。
如果我有一个只创建注册表并且不做任何其他操作的简单程序,它会成功结束。
// This is the only code in main()
Registry registry = LocateRegistry.createRegistry(1099);
// reaches the end of main() and stops
一旦注册了服务,Java虚拟机就不会停止 - 大概是因为注册表有一个在后台运行的线程。如果有服务注册,这将是有意义的,但如果没有注册,则没有那么多(至少给出了上面#1中描述的行为):
// This is the only code in main()
Registry registry = LocateRegistry.createRegistry(1099);
Naming.rebind("///name", new SomeRemoteServer());
Naming.unbind("name");
// never stops - must be killed with Ctrl-C
此时,如果我尝试取消导出注册表,它没有任何不同。注册表已停止(不能再向其注册/注销服务)但程序将继续运行。
// This is the only code in main()
Registry registry = LocateRegistry.createRegistry(1099);
Naming.rebind("///name", new EchoServer());
Naming.unbind("name");
if (registry != null) {
UnicastRemoteObject.unexportObject(registry, true);
}
//
// If executed, next line throws NoSuchObjectException
// Naming.rebind("///name", new EchoServer());
//
// main program never stops - must be killed with Ctrl-C
简而言之,问题是是否有可能以编程方式停止RMI Registry 一次(a)它是否已经创建并且(b)服务已在其上注册?提前致谢。