在 Java 中的特定时间间隔提供睡眠间隔

provide sleep interval at certain interval of time in Java

我的函数中有以下代码,我试图从 MAX_FAILED_ATTEMPT 的数据库中获取参数,并且基于此,如果检查失败,我将发送警报。当前代码将尝试从 MAX_FIELD_ATTEMPT 中获取值并立即一个接一个地检查。现在我只想在每次尝试后放置 sleep 5 分钟。因此,例如,如果 MAX_FAILED_ATTEMPT 为 3,那么对于第一次尝试,它会尝试立即检查,它会再次休眠 5 分钟并尝试检查,以这种方式,它会根据间隔尝试检查MAX_FAILED_ATTEMPT.

的次数
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;                  
                    }
                }

            }

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;                  
    }
}