Why @Bulkhead method not working when I call it from the same class

66 views Asked by At

I have a @Bulkhead method like this

@GetMapping("/bulkhead")
@Bulkhead(name = USER_SERVICE, fallbackMethod = "bulkheadDefault")
public ResponseEntity<String> testBulkhead(){
    logger.info("call " + Thread.currentThread().getName());
    String response = restTemplate.getForObject(BASEURL_BULKHEAD, String.class);
    logger.info(LocalTime.now() + " Call processing finished = " + Thread.currentThread().getName());
    return ResponseEntity.ok(response);
}

When I call this method from another class by creating 10 threads it works fine

for (int i=0; i<10; i++)
        new Thread(()-> userServiceApplication.testBulkhead()).start();

But when I call it like above in the same class all threads call bulkhead method sequentially. Anyone can explain why this is happening?

1

There are 1 answers

0
Raghvendra Garg On BEST ANSWER

Resilience4j uses spring-aop to manage aspects which means when invoked from the same class will not result in invoking the aspect code. in your case code in @Bulkhead aspect does not comes into picture when invoking from the same class but when you invoke from another bean it comes into play. Here is the excerpt from Spring documentation

The key thing to understand here is that the client code inside the main(..) of the Main class has a reference to the proxy. This means that method calls on that object reference will be calls on the proxy, and as such the proxy will be able to delegate to all of the interceptors (advice) that are relevant to that particular method call. However, once the call has finally reached the target object, the SimplePojo reference in this case, any method calls that it may make on itself, such as this.bar() or this.foo(), are going to be invoked against the this reference, and not the proxy. This has important implications. It means that self-invocation is not going to result in the advice associated with a method invocation getting a chance to execute.

https://docs.spring.io/spring-framework/reference/core/aop/proxying.html#aop-understanding-aop-proxies