2016-09-19 48 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之后,都可以突破外观 –