我正在使用Apache Ignite进行缓存,并在单节点上运行服务器,仅使用以下代码初始化Ignite客户端: -
公共类IgniteUtil {private static final Log logger = LogFactory.getLog(IgniteUtil.class.getName());
public static boolean initializeIgniteClient() {
try{
String hostName="localhost:47500..47509";
logger.info("Connecting to Apache ignite Server with host:port=" + hostName);
TcpDiscoverySpi spi = new TcpDiscoverySpi();
IgniteConfiguration cfg = new IgniteConfiguration();
TcpDiscoveryVmIpFinder finder =new TcpDiscoveryVmIpFinder();
List<String>addressList=new ArrayList<>();
addressList.add(hostName);
finder.setAddresses(addressList);
spi.setIpFinder(finder);
spi.setJoinTimeout(600);
cfg.setDiscoverySpi(spi);
cfg.setPeerClassLoadingEnabled(true);
Ignition.setClientMode(true);
URL xml = U.resolveIgniteUrl("log4j2.xml", false);
IgniteLogger log = new Log4J2Logger(xml);
cfg.setGridLogger(log);
Ignition.start(cfg);
if(!Ignition.ignite().cluster().forServers().nodes().isEmpty())
{
logger.info("Connecting to Apache ignite Server SUCCESS with hostName="+hostName);
return true;
}else{
logger.error("Connecting to Apache ignite Server FAILED with hostName="+hostName);
return false;
}
}catch(Exception e)
{
logger.error("Connection to Apache ignite Server failed...",e);
e.printStackTrace();
return false;
}
}
假设当点燃服务器关闭时,抛出异常并使用下面的代码尝试重新连接客户端。 每次尝试重新连接服务器都会产生大约20秒的延迟,直到服务器关闭。我怎样才能以最佳方式克服这个问题?
catch(Exception e)
{
if(e instanceof CacheException) {
if (e.getCause() instanceof
IgniteClientDisconnectedException)
{
Ignition.stop(true);
IgniteUtil.initializeIgniteClient();
logger.info("Reattempt failed");
}else if (e.getCause() instanceof
IgniteClientDisconnectedCheckedException) {
Ignition.stop(true);
IgniteUtil.initializeIgniteClient();
logger.info("Reattempt failed");
}else if (e.getCause() instanceof
NodeStoppingException) {
Ignition.stop(true);
IgniteUtil.initializeIgniteClient();
logger.info("Reattempt failed");
}else{
// nothing to do as not related to ignite server shutdown
e.printStackTrace();
}
} else if(e instanceof IllegalStateException) {
Ignition.stop(true);
IgniteUtil.initializeIgniteClient();
logger.info("Reattempt failed");
}else{
// nothing to do as not related to ignite server shutdown }
e.printStackTrace();
}
}
答案 0 :(得分:1)
如果服务器出现故障,客户端将抛出IgniteClientDisconnectedException
,它有一个未来,将在客户端再次重新连接到服务器时完成:
IgniteClientDisconnectedException.reconnectFuture().get()
。