Java 服务器重启时Apache Ignite客户端重新连接
我使用ApacheIgnite进行缓存,仅在单个节点上运行服务器,使用以下代码初始化Ignite客户端:-Java 服务器重启时Apache Ignite客户端重新连接,java,client,ignite,reconnect,Java,Client,Ignite,Reconnect,我使用ApacheIgnite进行缓存,仅在单个节点上运行服务器,使用以下代码初始化Ignite客户端:- public class IgniteUtil { private static final Log logger = LogFactory.getLog(IgniteUtil.class.getName()); public static boolean initializeIgniteClient() { try{
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;
}
}
如果服务器宕机,客户端将抛出
IgniteClientDisconnectedException
,它有一个未来,将在客户端重新连接到服务器时完成:
IgniteClientDisconnectedException.reconnectFuture().get()
我想这将被阻塞,将不允许其他操作,除非在单独的线程中完成…根据我的理解,OP希望继续尝试与每个缓存命中重新连接,直到服务器启动并且应用程序继续工作…检查服务器是否启动需要20秒,就像执行实际重新连接一样…在ignite bug出现之前,应该有更好的方法进行此检查已解决将IgniteClientDisconnectedException.reconnectFuture().get()保持应用程序挂起,直到我连接到服务器?若是,那个么这不是要求,即使服务器关闭,应用程序也应该运行。如果可能,请提供任何其他替代方案。谢谢。你不需要阻止这个未来,这只是一种等待重新连接的方便方式。如果您不等待,任何Ignite操作都会抛出异常,直到客户端重新连接,所以您只需要以满足需求的方式处理此异常。
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();
}
}