关闭RMI远程对象

时间:2014-08-15 12:32:04

标签: rmi

我有一个绑定服务的RMI类。当我想要关闭时,我取消绑定服务,但即使客户端没有连接,程序也不会退出。我是否应该采取其他措施来释放资源?

public void run() {
    try {
...
        GraphDataInterface gs = new GraphServer(config, dob, "file:./server.policy", "GraphServer");
        gs.close();
    } catch (RemoteException e) {
        System.err.println("GraphServer exception:" + e.toString());
    } catch (Exception e) {
        System.err.println("GraphServer exception:" + e.toString());
    }
}

这是构造函数调用的代码和close();

private void bindService() throws RemoteException {
    BaseRMIInterface stub = (BaseRMIInterface) UnicastRemoteObject.exportObject(this, 0);
    Registry registry = LocateRegistry.getRegistry();
    registry.rebind(name, stub);
    System.out.println(name + " bound");
}

private void unbindService() throws RemoteException, NotBoundException {
    Registry registry = LocateRegistry.getRegistry();
    registry.unbind(name);
    System.out.println(name + " unbound");
}

代码的输出是,

  

GraphServer绑定

     

GraphServer未绑定

但程序没有退出。

1 个答案:

答案 0 :(得分:2)

您已从注册表中取消绑定引用,但您还需要使用

取消导出对象本身
UnicastRemoteObject.unexportObject(this, true);