3

What does the @TimeLimiter annotation exactly?

Example

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

Could be equal to:

    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();
    }

The scheduler required in the non-blocking variant of the code, is that somehow involved in the annotation?

Research

I've mainly read:

  1. Resilience4J's Guide on TimeLimiter
  2. Reflectoring's blog post Implementing Timeouts with Resilience4j

Is there some other place where I can understand what the annotation does?

hc_dev
  • 8,389
  • 1
  • 26
  • 38
Adam
  • 2,845
  • 2
  • 32
  • 46

1 Answers1

1

The annotation is collected by an annotation-processor like in resilience4j-spring the TimeLimiterAspect.

Here Aspect Oriented Programming (AOP) extension AspectJ is used to create an Advice for the time-limiting resilience Aspect around the JointPoint of the annotated method.

You can look at its code, e.g. line 90 to figure out, how the evaluated annotation and method/class meta-information is used to weave (Advice) the TimeLimiter decoration (Aspect) around the annotated method's execution (JointPoint).

Further reading

For an introduction to AOP with AspectJ you can read Baeldung's Intro to AspectJ .

How Resilience4J leverages AOP can be read in official Resilience4J Guides, Spring Boot 2, Getting started with resilience4j-spring-boot2, Annotations:

The Spring Boot2 starter provides annotations and AOP Aspects which are auto-configured.

hc_dev
  • 8,389
  • 1
  • 26
  • 38