如果RMI服务器崩溃,该服务器的名称何时/如何从其注册的Java RMI注册表中删除?
答案 0 :(得分:1)
这是一个简单而强大的示例,用于演示此问题:
public class RMITEst {
static class Registry {
public static void main(String[] args) throws RemoteException, InterruptedException {
java.rmi.registry.Registry registry = LocateRegistry.createRegistry(1099);
Thread.sleep(Long.MAX_VALUE);
}
}
static class Server {
public static void main(String[] args) throws RemoteException {
java.rmi.registry.Registry registry = LocateRegistry.getRegistry();
TestService service = new TestServiceImpl();
Remote stub = UnicastRemoteObject.exportObject(service, 0);
registry.rebind("test", stub);
}
}
static class Client {
public static void main(String[] args) throws AccessException, RemoteException, NotBoundException {
System.out.println(Arrays.asList(LocateRegistry.getRegistry().list()));
TestService stub = (TestService) LocateRegistry.getRegistry().lookup("test");
stub.test();
}
}
}
interface TestService extends Remote {
public void test() throws RemoteException;
}
class TestServiceImpl implements TestService {
@Override
public void test() {
System.out.println("I am alive!");
}
}
如何使用它(一切都可以从Eclipse完成):
test
位于查找列表test
仍在查找列表中! - 连接拒绝异常将被抛出。因此,即使远程对象消失,RMI注册表也不会自动取消绑定绑定,例如由于JVM崩溃。
答案 1 :(得分:1)
如果RMI服务器崩溃,该服务器的名称何时/如何从其注册的Java RMI注册表中删除?
从不。注册表没有被告知这一点,也没有检查。