Spring Retry – 异常问题和重试

huangapple go评论77阅读模式
英文:

Spring Retry - Exception problem and retries

问题

@Retryable(value = {IOException.class}, maxAttempts = 3, backoff = @Backoff(delay = 3000))
public String getInfo() {
    try {
        // 在这里放置可能会引发 IOException 的执行代码
    } catch (IOException ex) {
        // 在这里捕获 IOException 并重新抛出
        throw ex;
    } catch (Exception ex) {
        // 在这里捕获其他 Exception
        throw new RuntimeException(ex);
    }
}
英文:

How can we catch two different exceptions (ex. from .lang and .io packages) in the same block of @Retryable method. One, we return an IOException and the other we retry the method.

@Retryable(value = {Exception.calss } ,maxAttempts = 3, backoff = @Backoff(delay = 3000))
public String getInfo() {
    try {
        //here we have an executive code that may have an IOException
    } catch(Exception ex) {
        //And here i would catch the Exception 
        throw new Exception();
    }   
}

答案1

得分: 9

你可以使用注解的 include 参数来处理多种不同的异常:

@Retryable(
    include = { IllegalAccessException.class, IOException.class }, 
    maxAttempts = 3, 
    backoff = @Backoff(delay = 3000))
public String getInfo() {
    // 一些代码
}
英文:

You can use the include parameter of the annotation to handle multiple various exceptions:

@Retryable(
    include = { IllegalAccessException.class, IOException.class }, 
    maxAttempts = 3, 
    backoff = @Backoff(delay = 3000))
public String getInfo() {
    // some code
}

huangapple
  • 本文由 发表于 2020年4月10日 21:35:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/61141431.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定