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

时间:2016-09-19 13:27:58

标签: java sleep intervals

我的函数中有以下代码,我试图从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;                  
                    }
                }

            }

1 个答案:

答案 0 :(得分: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;                  
    }
}