gpt4 book ai didi

java - 如何捕获 "NotesException: Notes error: Remote system no longer responding"并重试?

转载 作者:行者123 更新时间:2023-11-30 07:11:51 24 4
gpt4 key购买 nike

我有这个 Java 代理程序,它可以处理大量文档,并且可以在一夜之间运行。问题是如果网络突然短暂断开,我需要代理重试。重试次数可能有上限。

int numberOfRetries = 0;
try {
while(nextdoc != null) {
// process documents
numberOfRetries = 0;
}
} catch (NotesException e) {
numberOfRetries++;
if (numberOfRetries > 4) {
// go back and reprocess current document
} else {
// message reached max number of retries. did not successfully finished
}
}

此外,我当然不想重试整个过程。基本上我需要继续处理它正在处理的文档并继续下一个循环

最佳答案

您应该围绕获取文档的每段代码执行重试循环。由于 Notes 类通常需要 getFirst 和 getNext 范例,这意味着您需要两个单独的重试循环。例如,

   numberOfRetries = 0;
maxRetries = 4;

// get first document, with retries

needToRetry = false;
while (needToRetry)
{
try
{
while (needToRetry)
{
nextDoc = myView.getFirstDocument();
needToRetry=false;
}
}
catch (NotesException e)
{
numberOfRetries++;
if (numberOfRetries < maxRetries) {
// you might want to sleep here to wait for the network to recover
// you could use numberOfRetries as a factor to sleep longer on
// each failure
needToRetry = true;
} else {
// write "Max retries have been exceeded getting first document" to log
nextDoc = null; // we won't go into the processing loop
}
}
}

// process all documents

while(nextdoc != null)
{

// process nextDoc
// insert your code here


// now get next document, with retries

while (needToRetry)
{
try
{
nextDoc = myView.getNextDocument();
needToRetry=false;
}
catch (NotesException e)
{
numberOfRetries++;
if (numberOfRetries < maxRetries) {
// you might want to sleep here to wait for the network to recover
// you could use numberOfRetries as a factor to sleep longer on
// each failure
needToRetry = true;
} else {
// write "Max retries have been exceeded getting first document" to log
nextDoc = false; // we'lll be exiting the processing loop without finishing all docs
}
}
}
}

请注意,我将 maxRetries 视为数据集中所有文档的最大总重试次数,而不是每个文档的最大重试次数。

另请注意,将其分解一点可能更干净。例如

   numberOfRetries = 0;
maxRetries = 4;

nextDoc = getFirstDocWithRetries(view); // this contains while loop and try-catch

while (nextDoc != null)
{
processOneDoc(nextDoc);
nextDoc = getNextDocWithRetries(view,nextDoc); // and so does this
}

关于java - 如何捕获 "NotesException: Notes error: Remote system no longer responding"并重试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20750767/

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