首页 文章

服务器重启时Apache Ignite客户端重新连接

提问于
浏览
1

I am using Apache Ignite for caching purpose and running server on single node only initializing Ignite client using this code:-

公共类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();
            }
        }

1 回答

  • 1

    如果服务器出现故障,客户端将抛出 IgniteClientDisconnectedException ,它有一个未来,将在客户端再次重新连接到服务器时完成: IgniteClientDisconnectedException.reconnectFuture().get() .

相关问题