有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何捕获“Notes异常:Notes错误:远程系统不再响应”并重试?

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

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


共 (2) 个答案

  1. # 1 楼答案

    我根本不推荐你在做什么

    NotesException可能会触发的原因有很多,而且不能保证你会回到安全状态

    此外,代理需要运行如此长的时间,这意味着您需要更改服务器的“最大执行超时”以允许其正确运行。将其设置为非常高的值会使服务器更容易出现性能/死锁问题

    更好的解决方案是批处理工作负载,并让代理在批处理上运行一段设定的时间。边走边更新,以便代理返回时知道下一批工作

  2. # 2 楼答案

    您应该围绕获取文档的每段代码进行重试循环。由于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
       }