我在一个springbean中指定了一个@ Retryable注释,如下所示.

@EnableRetry
@Import({GrpcClientRetryConfig.class})
@Retryable(interceptor = "grpcClientRetryInterceptor", label = "ProfileGrpcClient")
public class ProfileGrpcClient {
  public RateLimitConfig getRateLimitConfig()

@Component
@AllArgsConstructor
public class RateLimits {
  private final ProfileGrpcClient client;

  // Some method calls client.getRateLimitConfig()
}

拦截器定义为GrpcClientRetryConfig

@EnableRetry
public class GrpcClientRetryConfig {
 @Bean
 public RetryOperationsInterceptor grpcClientRetryInterceptor() {
    val template = new RetryTemplate();
    val backOffPolicy = new ExponentialBackOffPolicy();
    // Set the exponential backoff parameter
    val interceptor = new RetryOperationsInterceptor();
    template.setRetryPolicy(new SimpleRetryPolicy());
    template.setBackOffPolicy(backOffPolicy);
    template.setListeners(new GrpcClientRetryListener[] {new GrpcClientRetryListener()});
    interceptor.setRetryOperations(template);
    return interceptor;
 }
}

GrpcClientRetryListener中,我试图获得@Retryable中指定的标签

@Slf4j
@Component
public class GrpcClientRetryListener extends MethodInvocationRetryListenerSupport {

  @Override
  public <T, E extends Throwable> void onSuccess(
      RetryContext context, RetryCallback<T, E> callback, T result) {
    if (callback instanceof MethodInvocationRetryCallback<T, E> methodInvocationRetryCallback) {
      log.info(
          "Retry Succeeded: {}, Count: {}",
          getMethodName(methodInvocationRetryCallback),
          context.getRetryCount());
    }
    super.onSuccess(context, callback, result);
  }

  @Override
  public <T, E extends Throwable> void onError(
      RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
    if (callback instanceof MethodInvocationRetryCallback<T, E> methodInvocationRetryCallback) {
      log.info(
          "Retry Failing: {}, Count: {}",
          getMethodName(methodInvocationRetryCallback),
          context.getRetryCount());
    }
    super.onError(context, callback, throwable);
  }

  private <T, E extends Throwable> String getMethodName(
      MethodInvocationRetryCallback<T, E> methodInvocationRetryCallback) {
    return methodInvocationRetryCallback.getLabel();
  }
}

但在重试期间,我看到得到的标签像public com.abc.proto.v2.common.ratelimit.RateLimits com.abc.grpcclients.ProfileGrpcClient.getRateLimitConfig().完整日志(log)如下

Retry Failing: public com.abc.proto.v2.common.ratelimit.RateLimits com.abc.grpcclients.ProfileGrpcClient.getRateLimitConfig(), Count: 5

有没有办法让我在MethodInvocationRetryListenerSupport中得到@Retryable中指定的label

推荐答案

参见@Retryable(interceptor) Javadoc:

/**
 * Retry interceptor bean name to be applied for retryable method. Is mutually
 * exclusive with other attributes.
 * @return the retry interceptor bean name
 */
String interceptor() default "";

因此,如果指定interceptor,则忽略所有其他注释属性.

由于您提供了自己的RetryOperationsInterceptor,请查看其各自的setLabel(String label)属性.

Java相关问答推荐

当涉及到泛型时,类型推理在Java中是如何工作的?

将带有js文件的 bootstrap 程序导入maven项目时出错

使用GridBagLayout正确渲染

如何在Java中从XML中获取特定的 node ,然后将其删除?

GSON期间的Java类型擦除

在Spring终结点中,是否可以同时以大写和小写形式指定枚举常量?

OpenGL ES 3.0-纹理黑色

使用Jolt将字段转换为列表

声明MessageChannel Bean的首选方式

带有Health Check的Spring Boot BuildPack打破了本机映像构建过程

如何在 spring 数据的MongoDB派生查询方法中使用$EXISTS

项目react 堆中doOnComplete()和Subscribe()的第三个参数之间的差异

在JDK Flight Recorder中只记录单个线程

Spring Validator批注不起作用

何时调用密封层次 struct 的switch 中的默认情况

如果执行@BeForeEach#repository.save(),则测试中的UnitTest最终UUID会发生更改

在ECLIPSE上的M1 Pro上运行JavaFX的问题

Java类型推断:为什么要编译它?

在具有Quarkus Panache的PostgreSQL中将JSON数据存储为JSONB时,会将其存储为转义字符串

如何使用Java ZoneID的区域设置?