gpt4 book ai didi

java - 在Java中以一定的时间间隔提供 sleep 间隔

转载 作者:行者123 更新时间:2023-12-01 09:29:21 28 4
gpt4 key购买 nike

我的函数中有以下代码,我试图从数据库中获取参数MAX_FAILED_ATTEMPT,并且基于此,如果检查失败,我将发送警报。当前代码将尝试从 MAX_FIELD_ATTEMPT 获取值并立即依次进行检查。现在我只想在每次尝试后让 sleep 持续 5 分钟。例如,如果 MAX_FAILED_ATTEMPT 为 3,那么第一次尝试时,它将尝试立即检查,再次尝试时,它将 hibernate 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;
}
}

关于java - 在Java中以一定的时间间隔提供 sleep 间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39574417/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com