resilience4j: Timelimiter Annotation not working (in SpringBoot Application)

Resilience4j version: 1.7.0

Java version: 16.0.1

I had used the timelimiter annotation with timeout duration as 5 sec in “application.yml”. I was basically testing and trying out all the patterns in resilience 4j , all of them worked fine except the timelimiter. To test it, I had called a thread.sleep for 10 sec and still the result was being printed and the fallBackMethod doesn’t get called.

Here’s the snippet where annotation was done.

  @TimeLimiter(name="Time",fallbackMethod="fallBackMethod")
    public CompletableFuture<ResponseEntity<?>> listAllAvailableBooks() {
    	
    	List<Book> books=BRepository.findByPersonId(null);
    	try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        if(books.size()>0) {
        	return CompletableFuture.completedFuture(new ResponseEntity<>(books,HttpStatus.OK));
        }
        else {
        	return CompletableFuture.completedFuture(new ResponseEntity<>("No Books Available Now",HttpStatus.OK));
        }
    }
public CompletableFuture<ResponseEntity<?>> fallBackMethod(Exception e) {
    	System.out.println("Here time limit exceed\n");
    	return CompletableFuture.completedFuture(new ResponseEntity<>(e.getMessage(),HttpStatus.OK));
    }

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (8 by maintainers)

Most upvoted comments

  @Bulkhead(name = BACKEND_A, type = Type.THREADPOOL)
  @TimeLimiter(name = BACKEND_A)
  @CircuitBreaker(name = BACKEND_A)
    public CompletableFuture<String> futureTimeout() {
        Try.run(() -> Thread.sleep(5000));
        return CompletableFuture.completedFuture("Hello World from backend A");
    }

The decoration is as follows:

CircuitBreaker ( TimeLimiter ( BulkHead ( method ( ) ) ) )

  1. Method is invoked in a Thread Pool.
  2. TimeLimiter limits the time of the thread, but the CompletableFuture is not/cannot be cancelled.
  3. CircuitBreaker records exceptions.