Cod*_*ind 9 java networking multithreading rmi
总而言之,在我的应用程序不再需要RMI之后,我无法让几个Java RMI的非守护程序线程关闭.这可以防止JVM在main()完成时退出.
我知道导出UnicastRemoteObjects会导致RMI在成功调用之前保持线程处于打开状态UnicastRemoteObject.unexportObject(Object o,boolean force).这是一个示例(无需修改即可运行,JVM将正常退出 - 删除对unexportObject的调用,JVM永远不会退出):
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
public class TestUnicastRemoteObject{
private static UnicastRemoteObject obj;
private static Registry registry;
public static void main(String[] args) throws Exception{
obj = new UnicastRemoteObject(){
private static final long serialVersionUID = 1L;
};
System.err.println("created UnicastRemoteObject");
System.err.println("creating registry ...");
registry = LocateRegistry.createRegistry(9999);
System.err.println("registry created.");
System.err.println("binding obj to registry ...");
registry.bind("Test", obj);
System.err.println("bound");
UnicastRemoteObject.unexportObject(obj, true);
System.err.println("unexported obj");
}
}
Run Code Online (Sandbox Code Playgroud)
此外,您是否创建注册表和/或将远程对象绑定到它似乎并不重要 - 在此示例中唯一似乎重要的是,无论何时创建UnicastRemoteObject,您都需要调用unexportObject以便完成后防止任何线程遗留.
在我的应用程序中,我已经确保我在我创建的每个UnicastRemoteObject上调用了unexportObject,但是RMI的"reaper"线程和"连接接受"线程仍然存在,当我的应用程序使用RMI资源完成时,阻止了我的JVM退出.
除了忘记取消导致UnicastRemoteObjects之外,还有什么东西可以导致RMI留下线程吗?
果然,我在代码中遇到了一个错误,导致我的(很多)UnicastRemoteObjects中的一个在调用应用程序完成后不会自动导出它.所以答案是:
在运行的JVM中取消导出所有UnicastRemoteObjects足以关闭所有RMI非守护程序线程.
听起来你解决了你的问题@Ben,但对于后代,我想我会将我的评论推广到一个答案.每当我有一个注册/取消注册类型的模式时,我都要确保通过单例对象来管理它们.这样你就有一个地方可以找出哪个对象没有注册.在JMX中公开这也是一个胜利.
像下面这样的代码会很好.它将允许您进行日志记录或JMX查询,以查看哪些对象已绑定到注册表但尚未解除绑定.
public class UnicastRegistry {
private static Registry registry;
private static UnicastRegistry singleton;
// private to force the singleton
private UnicastRegistry() throws RemoteException {
registry = LocateRegistry.createRegistry(9977);
}
public static UnicastRegistry createSingleton() throws RemoteException {
if (singleton == null) {
singleton = new UnicastRegistry();
}
return singleton;
}
public void register(String label, Remote obj) throws Exception {
registry.bind(label, obj);
}
public void unregister(String label) throws Exception {
Remote remote = registry.lookup(label);
registry.unbind(label);
if (remote instanceof UnicastRemoteObject) {
UnicastRemoteObject.unexportObject(remote, true);
}
}
public void unregisterAll() throws Exception {
for (String label : registry.list()) {
unregister(label);
}
}
public void printStillBound() throws Exception {
String[] stillBound = registry.list();
if (stillBound.length > 0) {
System.out.println("Still bound = " + Arrays.toString(stillBound));
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4690 次 |
| 最近记录: |