2016-09-19 68 views
0

我有我的功能在下面的代碼,我試圖從數據庫中的參數是MAX_FAILED_ATTEMPT並基於此我將發送警報,如果檢查失敗。當前的代碼將嘗試從MAX_FIELD_ATTEMPT中獲取值,並立即檢查另一個。現在我只想在每次嘗試5分鐘後放入sleep。因此,例如,如果MAX_FAILED_ATTEMPT爲3,那麼第一次嘗試時將嘗試立即檢查,再次將睡眠5分鐘並嘗試檢查,以這種方式基於間隔它將嘗試檢查MAX_FAILED_ATTEMPT的次數。在Java中提供一定的時間間隔的睡眠間隔

private String connect(KpiDefinition kpiDef) 
    { 
     FtpSftpServer ftpSftpServer = kpiDef.getFtpSftpServer(); 

     // FTP key 
     String serverName = ftpSftpServer.getServerName(); 

     // Retrieving ftp details from rator retail db 
     Map<String, String> serverDetails = getServerDetailsFromRator(kpiDef, 
       serverName); 

     if (serverDetails == null || serverDetails.isEmpty()) 
     { 
      errorMessage = "Error while retrieving FTP Details from Retail DB."; 
      logger.debug(errorMessage); 
     } else 

      { 
       boolean success = false; 
       // We would attempt to connect till the max failed attempts 
       // defined on the resource are reached. However if the 
       // connection is already successful or if the connection has 
       // failed due to Authentication Failure, then we will not try 
       // again and simply come out of the loop. 
       Long maxFailedAttempts = kpiDef.getFtpSftpServer() 
         .getMaxFailedAttempts(); 
       if (maxFailedAttempts == null || maxFailedAttempts == 0) 
       { 
        maxFailedAttempts = 1l; 
       } 

       for (int i = 0; i < maxFailedAttempts; i++) 
       { 
        try 
        { 
         success = connect(serverName, protocol, serverAddress, 
           serverPort, username, password); 
         if (!success) 
         { 
          String message = "Could not connect to " + protocol 
            + " server " + serverName 
            + " - Authorization failed."; 
          logger.debug(message); 
          errorMessage = message; 
          deactivateKPI(kpiDef, authenticateFailedMessage); 
          // do not attempt to try again if the KPI fails with 
          // authentication Exception. 
          break; 
         } 
         // Also come out of the loop if the connection was 
         // successful. We do not need to continue to attempt to 
         // connect. 
         break;     
        } 
       } 

      } 

回答

1

TimeUnit.MINUTES.sleep(5)的位置,你認爲合適的

編輯:

我會嘗試改變成功的條件在for循環只是一點點

for (int i = 0; i < maxFailedAttempts; i++) 
{ 
    try 
    { 
     success = connect(serverName, protocol, serverAddress, 
             serverPort, username, password); 
     if (!success) 
     { 
      String message = "Could not connect to " + protocol 
           + " server " + serverName 
           + " - Authorization failed."; 
      logger.debug(message); 
      errorMessage = message; 

      try 
      { 
       deactivateKPI(kpiDef, authenticateFailedMessage); 
       TimeUnit.MINUTES.sleep(5); 
      } 
      catch (AuthenticationException ae) 
      { 
       // do not attempt to try again if the KPI fails with 
       // authentication Exception. 
       ae.printStackTrace(); 
      } 

      break; 
     } 

     // Also come out of the loop if the connection was 
     // successful. We do not need to continue to attempt to 
     // connect. 
     break;     
    } 
} 
+0

可以請你告訴我在那裏我必須把這個睡眠,因爲正如我所提到的,我必須在每隔5分鐘的時間間隔之後提供睡眠,直到max_attempt_failed字段值被檢查爲止 – Andrew

+0

用一些代碼編輯。 idk什麼是你提到的認證異常,但大概是它被'deactivateKPI()'拋出,所以抓住它,除非它成功,否則不要進行睡眠。無論哪種方式,在try/catch之後,都可以突破外觀 –