使用@Retryable 打印重试次数
Print retry count with @Retryable
我在这样的方法上使用@Retryable :-
@Retryable( value = SQLException.class,
maxAttempts = 5, backoff = @Backoff(delay = 100))
void testMethod(String abc) throws SQLException{
//some method body that could throw sql exception
};
我想打印重试次数,并显示如下消息:
Retry Number : 1
Retry Number : 2
...
Retry Number : 5
我怎样才能做到这一点?
可以添加retryListener
@Retryable( value = SQLException.class, maxAttempts = 5,
backoff = @Backoff(delay = 100), listeners = {"retryListener"})
void testMethod(String abc) throws SQLException{
//some method body that could throw sql exception
};
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("Unable to recover from Exception");
log.error("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("Exception Occurred, Retry Count {} ", context.getRetryCount());
super.onError(context, callback, throwable);
}
@Override
public <T, E extends Throwable> boolean open(RetryContext context,
RetryCallback<T, E> callback) {
log.error("Exception Occurred, Retry Session Started ");
return super.open(context, callback);
}
}
您可以添加以下代码:
@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() 会给你流量的重试次数。
我在这样的方法上使用@Retryable :-
@Retryable( value = SQLException.class,
maxAttempts = 5, backoff = @Backoff(delay = 100))
void testMethod(String abc) throws SQLException{
//some method body that could throw sql exception
};
我想打印重试次数,并显示如下消息:
Retry Number : 1
Retry Number : 2
...
Retry Number : 5
我怎样才能做到这一点?
可以添加retryListener
@Retryable( value = SQLException.class, maxAttempts = 5,
backoff = @Backoff(delay = 100), listeners = {"retryListener"})
void testMethod(String abc) throws SQLException{
//some method body that could throw sql exception
};
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("Unable to recover from Exception");
log.error("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("Exception Occurred, Retry Count {} ", context.getRetryCount());
super.onError(context, callback, throwable);
}
@Override
public <T, E extends Throwable> boolean open(RetryContext context,
RetryCallback<T, E> callback) {
log.error("Exception Occurred, Retry Session Started ");
return super.open(context, callback);
}
}
您可以添加以下代码:
@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() 会给你流量的重试次数。