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?
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
https://docs.spring.io/spring-framework/reference/core/aop/proxying.html#aop-understanding-aop-proxies