I am using Apache Ignite for caching purpose and running server on single node only initializing Ignite client using this code:-
public class 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;
}
}
Assuming when ignite server gets down an exception is thrown and client reconnection is attempted using below code. Every attempt to reconnect server gives latency of around 20 sec till server is down. How can I overcome this problem in best possible way?
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();
}
}
If server goes down, client will throw
IgniteClientDisconnectedException
, it has a future, that will be completed when client reconnects to server again:IgniteClientDisconnectedException.reconnectFuture().get()
.