2017-09-01 111 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操作都會在客戶端重新連接之前拋出異常,因此您只需以滿足您的要求的方式處理此異常。 –