【发布时间】:2023-10-14 09:58:02
【问题描述】:
我正在开发一个具有许多服务器的 Java RMI 应用程序。其中一个服务器是类 LDAP 服务器,它跟踪其他服务器的 IP 地址(和其他一些信息):每次创建新服务器时,它都会立即连接到存储其 IP 的类 LDAP 服务器,方法是调用函数connectToLDAP():
ArrayList<ServerInfo> computingServers = new ArrayList<ServerInfo>();
/**
* Adds a server to the LDAP to store its IP address
*/
@Override
public void connectToLDAP(int port, int q) throws RemoteException, Exception {
computingServers.add(new ServerInfo(RemoteServer.getClientHost(), port, q));
}
这样一来,服务器只需要连接到类似 LDAP 的服务器即可获取所有服务器 IP 的列表。
我的问题如下:当我运行一个新服务器 X 并连接到类似 LDAP 的服务器,然后我使用 Eclipse“终止”或在终端上使用 Ctrl + C 停止运行 X 的进程时,如何服务器 X 能否在退出前调用下面的函数,以便类 LDAP 服务器保持最新状态?
/**
* Removes a server from the LDAP
*/
@Override
public void removeServer(int port, int q) throws RemoteException, Exception {
computingServers.remove(new ServerInfo(RemoteServer.getClientHost(), port, q));
}
我知道这不是最佳选择,但优化或遵循惯例并不是我的重点。但是,如果有完全其他的方式来做我想做的事,我愿意接受建议。
【问题讨论】:
-
您可以查看关闭挂钩,但它们确实不应该用于阻塞操作。
-
但我看不出 RMI 服务器的 IP 地址和端口号对任何人有什么用处。除非他们是注册表。
-
单个服务器使用IP地址一次连接到所有服务器(使用IP),而无需事先知道它们。存储端口号是因为我正在处理的网络只允许我使用某些端口,而不是默认的 1099。
-
我不知道关闭挂钩会是什么样子,因为据我所知,RMI 线程无法访问?
-
这与它有什么关系?您无需访问 RMI 线程即可安装关闭挂钩。
标签: java rmi exit termination