我有这个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
}
}
另外,我当然不想重试整个过程。基本上我需要继续处理正在处理的文档并继续下一个循环
答案 0 :(得分:3)
您应该围绕获取文档的每段代码执行重试循环。由于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视为数据集中所有文档的最大总重试次数,而不是每个文档的最大重试次数。
另请注意,稍微分解一下可能更干净。 E.g。
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
}
答案 1 :(得分:1)
我不会推荐你在做什么。
NotesException可能由于多种原因而触发,并且无法保证您将返回到安全状态。
此外,代理需要运行这么长时间的事实意味着您需要更改服务器“最大执行超时”以允许它正确运行。将其设置为非常高的值会使服务器更容易出现性能/死锁问题。
更好的解决方案是批处理工作负载,让代理在批处理上运行一段时间。随时更新,以便当代理回来时,它知道要处理下一批。