如何在 RetryListener 的 onError 方法中从 RetryContext 中检索实现类名的名称
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
public abstract class ServiceBase {
@Retryable(value = ServiceException.class, maxAttempts = 3, backoff = @Backoff(delay = 5000), listeners = {"myRetryListener"})
public void start() throws ServiceException {
fireServiceStarted("Service started");
}
}
public class MyRetryListener implements RetryListener {
@Override
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
// Log when retry operation starts
return true; // proceed with retry
}
@Override
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
// Log when retry operation ends
}
@Override
public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
// Log each error during retry
}
}
我试图从 RetryListener 实现的 onError 方法中的 RetryContext 获取实现类名称的名称。但是,我得到的是抽象类名称。
如果您要重试,下面将给出方法和类名称 抽象方法
private static <T, E extends Throwable> String getMethodName(MethodInvocationRetryCallback callback) {
return callback.getInvocation().getMethod().getName();
}
private static <T, E extends Throwable> String getClassName(MethodInvocationRetryCallback callback) {
return callback.getInvocation().getThis().getClass().toString();
}