我正在使用Java 6并尝试为telnet连接编写故障转移(即,如果它无法连接/拒绝,超时等,则地址1进行故障转移并再次尝试寻址2)。如果address1有效,那么一切都很好并返回我的结果,但是,如果第一个地址拒绝连接,则address2将始终失败(即使它在第一次测试中设置为工作地址)。 Telnet服务是一个只处理连接选项的便利类,并且从inputStream“获取”数据(如果有人需要更多细节:这是在GWT服务器端服务impl类中):
我正在使用commons-net-3.3
这里是无法“故障转移”的代码部分:
public List<Problem> searchForProblemRemote(final String searchStr) {
String msg = "ProblemService:searchForProblemRemote; searchString = " + searchStr;
if (!GWT.isProdMode()) {
log.info(" Remote Address : " + getThreadLocalRequest().getRemoteAddr() + " Session Id: "
+ getThreadLocalRequest().getSession().getId() + " User: "
+ getThreadLocalRequest().getSession().getAttribute("user") + " Info: " + msg);
}
List<Problem> clientProblemList = null;// new
try {
final TelnetService telnet = new TelnetService();
// HF - this is now using the imo host address from the config file
final String imoHostAddress = ConfigurationLoader.getConfiguration().getImoHostAddress();
final String imoAccountNumber = ConfigurationLoader.getConfiguration().getImoAccountNumber();
final int imoProblemItPort = ConfigurationLoader.getConfiguration().getImoProblemItPort();
String searchResults = "";
// here is where we try the timeout
try {
telnet.connect(imoHostAddress, imoProblemItPort);
searchResults = telnet.getResult("search^50^" + searchStr + "^" + imoAccountNumber);
} catch (Exception e1) {
// HF - if we fail to connect try the failover
final String imoHostFailoverAddress = ConfigurationLoader.getConfiguration().getImoHostFailoverAddress();
final String imoAccountFailoverNumber = ConfigurationLoader.getConfiguration().getImoAccountFailoverNumber();
log.fatal("IMO FAILOVER: on " + imoHostAddress + " with " + e1.getLocalizedMessage());
try {
telnet.disconnect();
log.fatal("IMO FAILOVER RECOVERY: on " + imoHostFailoverAddress);
telnet.connect(imoHostFailoverAddress, imoProblemItPort);
searchResults = telnet.getResult("search^50^" + searchStr + "^" + imoAccountFailoverNumber);
log.fatal("IMO FAILOVER SUCCESS: on " + imoHostFailoverAddress);
} catch (Exception e) {
log.fatal("IMO FAILOVER RECOVERY FAILED: on " + imoHostFailoverAddress + " with " + e.getLocalizedMessage());
throw new IOException("IMO SERVER FAILURE: Unable to connect to either primary \"" + imoHostAddress
+ "\" or failover \"" + imoHostFailoverAddress);
}
}
log.info("IMO Search searchResults, User: " + getThreadLocalRequest().getSession().getAttribute("user")
+ " Results:" + searchResults);
telnet.disconnect();
//CODE REMOVED HERE FOR CLARITY (lots of processing of results stuff)