2017-09-01 139 views
1

我使用的Apache点燃缓存目的和正在运行的服务器在单个节点上只使用此代码初始化点燃客户端: -在服务器restartup阿帕奇点燃客户端重新连接

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

回答

1

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

+0

这将阻止我的猜测,不会允许其他操作,除非在单独的线程中完成......按照我的理解,OP希望不断尝试重新连接每个缓存命中,直到服务器启动并且应用程序继续工作....检查服务器是否正常工作20秒做实际的重新连接...应该有更好的方式进行检查,直到点燃错误得到解决https://issues.apache.org/jira/browse/IGNITE-2766 – user2572801

+0

Will IgniteClientDisconnectedException。 reconnectFuture()。get()保持应用程序挂起直到我连接到服务器?如果是,那么这不是必需的,即使服务器关闭,应用程序也应该运行。如果可能的话,请提供其他的选择。谢谢。 –

+0

你不需要阻止这个未来,它只是一个等待重新连接的便捷方式。如果您不等待,任何Ignite操作都会在客户端重新连接之前抛出异常,因此您只需以满足您的要求的方式处理此异常。 –