如何以编程方式停止RMI注册表?

时间:2016-12-26 18:27:59

标签: java rmi

我想以编程方式停止RMI注册表。我认为这对于在类stop()shutdown()上调用某些LocateRegistryNaming方法一样微不足道,但是没有这样的方法。

我尝试过的事情

  • 我试图取消注册所有服务并等待注册表的线程自行结束,如某些网站所建议的那样,但它似乎不起作用(参见下面的#2)。
  • 我已经尝试了this similar question中提供的配方但是虽然它停止了注册表整个程序并没有停止(参见下面的#3)。
  • 我已经考虑了this other question中提供的答案,并确保我取消了创建注册表的实际结果而不是存根(请参阅下面的#3)。

代码示例

  1. 如果我有一个只创建注册表并且不做任何其他操作的简单程序,它会成功结束。

    // This is the only code in main() Registry registry = LocateRegistry.createRegistry(1099); // reaches the end of main() and stops

  2. 一旦注册了服务,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

  3. 此时,如果我尝试取消导出注册表,它没有任何不同。注册表已停止(不能再向其注册/注销服务)但程序将继续运行。

    // 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

  4. 简而言之,问题是是否有可能以编程方式停止RMI Registry 一次(a)它是否已经创建并且(b)服务已在其上注册?提前致谢。

0 个答案:

没有答案