我有一个 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/