Java 如何使用相同的连接将一台RMI服务器替换为另一台RMI服务器?
我正在从事一个项目,其中有n个客户端连接到RMI服务器(主RMI服务器)。此服务器有另一个连接到它的服务器,称为备份RMI服务器。当备份服务器出现故障/关闭时(例如CTRL+C),备份服务器应承担主RMI服务器的角色,成为主RMI服务器 为了保留客户端和服务器之间的连接,而不必再次连接所有内容或再次对服务器进行客户端查找(命名.查找),是否有方法将第一台服务器之间的连接传输到另一台服务器 我已经尝试过在备份服务器上使用LocateRegistry.getRegistry(PORT),在创建“RMI连接”时,使用LocateRegistry.createRegistry(PORT).rebind(rmname,this)(我知道它不像套接字那样是一个连接,但只是为了简单起见)。不起作用,或者至少我不确定它是否被正确使用 RMI服务器类Java 如何使用相同的连接将一台RMI服务器替换为另一台RMI服务器?,java,rmi,Java,Rmi,我正在从事一个项目,其中有n个客户端连接到RMI服务器(主RMI服务器)。此服务器有另一个连接到它的服务器,称为备份RMI服务器。当备份服务器出现故障/关闭时(例如CTRL+C),备份服务器应承担主RMI服务器的角色,成为主RMI服务器 为了保留客户端和服务器之间的连接,而不必再次连接所有内容或再次对服务器进行客户端查找(命名.查找),是否有方法将第一台服务器之间的连接传输到另一台服务器 我已经尝试过在备份服务器上使用LocateRegistry.getRegistry(PORT),在创建“RM
try {
// First try to connect to Primary server (the case where this is
//supposed to be Backup server)
ci = (RMIInterface) Naming.lookup("RMIConnection");
// This function is what makes the Backup server aware that the Primary
// server failed. It's a while(true) loop with a Thread.sleep(2000)
// that invokes a simple RMI method to test the connection
checkPrimaryServerStatus();
}
catch (java.rmi.ConnectException e) {
System.out.println("No primary RMIServer yet. Connecting...");
} finally {
// If it isn't backup server, creates connection
if (!isBackup) {
try {
LocateRegistry.createRegistry(RMIPORT).rebind(RMINAME, this);
} catch (Exception err) {
System.out.println("\nERROR: Something went wrong. Aborting program...");
err.printStackTrace();
System.exit(-1);
}
}
}
try {
ci = (RMIInterface) Naming.lookup("RMIConnection");
} catch (java.rmi.ConnectException e) {
System.out.println("\nConnect the server first.");
System.exit(-1);
}
RMI客户端类
try {
// First try to connect to Primary server (the case where this is
//supposed to be Backup server)
ci = (RMIInterface) Naming.lookup("RMIConnection");
// This function is what makes the Backup server aware that the Primary
// server failed. It's a while(true) loop with a Thread.sleep(2000)
// that invokes a simple RMI method to test the connection
checkPrimaryServerStatus();
}
catch (java.rmi.ConnectException e) {
System.out.println("No primary RMIServer yet. Connecting...");
} finally {
// If it isn't backup server, creates connection
if (!isBackup) {
try {
LocateRegistry.createRegistry(RMIPORT).rebind(RMINAME, this);
} catch (Exception err) {
System.out.println("\nERROR: Something went wrong. Aborting program...");
err.printStackTrace();
System.exit(-1);
}
}
}
try {
ci = (RMIInterface) Naming.lookup("RMIConnection");
} catch (java.rmi.ConnectException e) {
System.out.println("\nConnect the server first.");
System.exit(-1);
}
这将正确地创建主服务器和备份服务器,但是如果主服务器关闭并且备份接管,用户将无法像与主服务器一样与备份通信。
问题是将连接从一个连接“转移”到另一个连接。没有RMI连接这样的事情,因此也没有丢失或转移这样的事情。只是拥有一个存根而已。如果存根停止工作,或者它来自的远程对象未被报告,调用方将得到某种类型的
RemoteException
。此时,它应该重新获取存根,这意味着在您的场景中有一个新的Naming.lookup()
。如果您已经恢复了该名称以指向它,或者用新绑定的新注册表完全替换了该注册表,那么它将指向另一个服务器。我感到困惑。在新服务器(以前的备份服务器)中,要获取以前的主服务器创建的存根,我该怎么做?如果我进行查找,服务器不会成为客户机吗?必须重新获取存根的是客户机。