【问题标题】:Translate resilience4j @TimeLimiter annotation to code将resilience4j @TimeLimiter 注释转换为代码
【发布时间】:2021-06-23 02:59:50
【问题描述】:

@TimeLimiter 注释到底是什么?

示例

    @TimeLimiter(name = "abc123")
    public <T> CompletableFuture<T> execute(Supplier<T> supplier) {
        return CompletableFuture.supplyAsync(supplier);
    }

可能等于:

    public <T> CompletableFuture<T> execute(Supplier<T> supplier) {
        TimeLimiter timeLimiter = timeLimiterRegistry.timeLimiter("abc123");

        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3); // This scheduler must somehow exist with the annotation as well right?
        return timeLimiter.executeCompletionStage(
            scheduler, () -> CompletableFuture.supplyAsync(supplier)).toCompletableFuture();
    }

代码的非阻塞变体中所需的调度程序,是否以某种方式包含在注释中?

研究

我主要阅读:

  1. Resilience4J 的Guide on TimeLimiter
  2. Reflectoring 的博文Implementing Timeouts with Resilience4j

还有其他地方我可以理解注释的作用吗?

【问题讨论】:

    标签: annotations resilience4j time-limiting


    【解决方案1】:

    注释由注释处理器收集,例如resilience4j-spring TimeLimiterAspect

    Here Aspect Oriented Programming (AOP) 扩展 AspectJ 用于为 JointPoint周围的限时弹性Aspect 创建 Advice > 注释的方法。

    您可以查看它的code, e.g. line 90 以了解评估的注释和方法/类元信息如何用于编织(建议)TimeLimiter 装饰(方面)围绕注释方法的执行(JointPoint)。

    进一步阅读

    关于 AOP 与 AspectJ 的介绍,您可以阅读 Baeldung 的 Intro to AspectJ

    Resilience4J 如何利用 AOP 可以阅读官方 Resilience4J 指南,Spring Boot 2Getting started with resilience4j-spring-boot2, Annotations

    Spring Boot2 启动器提供自动配置的注解和 AOP 方面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 2010-12-03
      • 1970-01-01
      • 1970-01-01
      • 2011-10-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多