Search code examples
javaresilience4j

Java + Resilience4J - Time limiter on my own handler?


Small question regarding Resilience4j please.

Currently, Resilience4J propose a very nice TimeLimiter pattern.

With it, one can very easily configure the time limit on an outbound http call. For instance, my service A is using an http client to call a service B. The time limit is to wait for service B for 1 second.

When properly configured, we can see when service B takes more than 1 second, to return a fallback instead, some kind of "no need to wait for service B anymore".

I was wondering, is it possible to configure similar, but for my own service/handler?

By that, I mean I would like to set a TimeLimiter on my own service A. Since I have an SLA defined by a contract of 5 seconds, instead of telling all my clients ("don't wait for me if I take more than 5 seconds") and to let them configure some kind of TimeLimiter on their end.

Very naive, I put the Resilience4J time limiter annotation on my own handler, and put very long computation in it, but not working. I think the Resilience4J time limiter is more for outbound calls.

Would it be possible to have the same mechanism on my own service A please?

Thank you


Solution

  • Yes, it also possible for your own service A. But your service must return a CompletableFuture, Mono or Flux so that the TimeLimiter can throw an exception.