使用 @Retryable 打印重试次数

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

Print retry count with @Retryable

问题

我正在一个方法上使用 @Retryable,代码如下:

@Retryable(value = SQLException.class, 
      maxAttempts = 5, backoff = @Backoff(delay = 100))
void testMethod(String abc) throws SQLException{
//可能会抛出 SQL 异常的一些方法体
};

我想要打印重试次数,带有以下消息:

重试次数1
重试次数2
...
重试次数5

我该如何实现这个功能?

英文:

I am using @Retryable on a method like this :-

@Retryable( value = SQLException.class, 
      maxAttempts = 5, backoff = @Backoff(delay = 100))
void testMethod(String abc) throws SQLException{
//some method body that could throw sql exception
};

And i want to print the count of retry , with a message like :

Retry Number : 1
Retry Number : 2
...
Retry Number : 5

How can i achieve this ?

答案1

得分: 21

你可以添加以下代码:

    @Retryable( value = SQLException.class, 
          maxAttempts = 5, backoff = @Backoff(delay = 100))
    void testMethod(String abc) throws SQLException{
        log.info("Retry Number : {}", RetrySynchronizationManager.getContext().getRetryCount());
    };

RetrySynchronizationManager.getContext().getRetryCount() 将会给你流程中的重试次数。

英文:

you can add below code:

@Retryable( value = SQLException.class, 
      maxAttempts = 5, backoff = @Backoff(delay = 100))
void testMethod(String abc) throws SQLException{
log.info("Retry Number : {}",RetrySynchronizationManager.getContext().getRetryCount());
};

RetrySynchronizationManager.getContext().getRetryCount() will give you the retry count on flow.

答案2

得分: 13

你可以添加retryListener

@Retryable(value = SQLException.class, maxAttempts = 5, 
            backoff = @Backoff(delay = 100), listeners = {"retryListener"})
void testMethod(String abc) throws SQLException{
    // 可能会抛出 SQL 异常的方法体
};

retryListener 应该如下所示,你可以在错误时打印重试次数。

@Slf4j
@Component
class RetryListener extends RetryListenerSupport {

    @Override
    public <T, E extends Throwable> void close(RetryContext context,
                                               RetryCallback<T, E> callback, Throwable throwable) {

        log.error("无法从异常中恢复");
        log.error("错误", throwable);
        super.close(context, callback, throwable);
    }

    @Override
    public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
        log.error("发生异常,重试次数 {}", context.getRetryCount());
        super.onError(context, callback, throwable);
    }

    @Override
    public <T, E extends Throwable> boolean open(RetryContext context,
                                                 RetryCallback<T, E> callback) {
        log.error("发生异常,重试会话已启动");
        return super.open(context, callback);
    }
}
英文:

you can add retryListener

    @Retryable( value = SQLException.class, maxAttempts = 5, 
                backoff = @Backoff(delay = 100), listeners = {&quot;retryListener&quot;})
    void testMethod(String abc) throws SQLException{
    //some method body that could throw sql exception
    };

retryListener should like below, you can print retry count on error.

@Slf4j
@Component
class RetryListener extends RetryListenerSupport {

    @Override
    public &lt;T, E extends Throwable&gt; void close(RetryContext context,
                                               RetryCallback&lt;T, E&gt; callback, Throwable throwable) {

        log.error(&quot;Unable to recover from  Exception&quot;);
        log.error(&quot;Error &quot;, throwable);
        super.close(context, callback, throwable);
    }

    @Override
    public &lt;T, E extends Throwable&gt; void onError(RetryContext context, RetryCallback&lt;T, E&gt; callback, Throwable throwable) {
        log.error(&quot;Exception Occurred, Retry Count {} &quot;, context.getRetryCount());
        super.onError(context, callback, throwable);
    }

    @Override
    public &lt;T, E extends Throwable&gt; boolean open(RetryContext context,
                                                 RetryCallback&lt;T, E&gt; callback) {
        log.error(&quot;Exception Occurred, Retry Session Started &quot;);
        return super.open(context, callback);
    }
}

huangapple
  • 本文由 发表于 2020年10月12日 05:40:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64309290.html
匿名

发表评论

匿名网友

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

确定