案例
我在服务器进程中使用进程内* Java RMI 注册表。
我在注册表中绑定(bind)了 1 个对象。
我的客户端进程连接并查找绑定(bind)并传递远程对象。
现在我希望服务器进程干净地关闭,即不再有守护线程存在。使用 RMI,我在我的服务器进程中对一个对象调用“关闭”,问题是这个“关闭”方法实际上必须做什么。
问题
如何确保没有非守护线程在使用 RMI 导出对象的进程中运行?
据我所知,我必须取消导出**所有导出的对象才能关闭 JVM?由于许多对象已通过 RMI 传递,因此我必须维护这些对象的集合并取消导出**每个对象。
我读到任何导出的对象在垃圾收集时都应该取消导出自身(现在找不到引用),但我的经验告诉我,某些对象(尤其是分布式的?)可能需要很长时间才能被垃圾收集所以这不是一个真正的选择。
* 使用 LocateRegistry 创建
** 使用 UnicastRemoteObject 执行取消导出
最佳答案
How do I make sure that no non-daemon threads are running in a process using RMI exported objects?
全部取消导出,包括注册表。请注意,这需要保存 createRegistry() 的返回值。
As far as I can read, I must unexport** all exported objects for the JVM to shut down? Since many objects have been passed over RMI, I would have to maintain this set of these objects and unexport** each one.
正确,除非您使用Unreferenced
工具。
I have read that any exported object should unexport itself when garbage collected (can't find a reference right now) but my experience tells me that it can take a long time for some objects (especially distributed?) to be garbage collected so this is not really an option.
您可以控制所有 DGC 超时。这确实是一个选项,通过 Unreferenced
接口(interface)。
关于java - 使用进程内 Java RMI 注册表时如何正常关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35525224/