I had to deal with the same problem as you.
For me, TimeLimiter from resilience4j solve my problem and not need to use Hystrix anymore.
This is my application.properties:
resilience4j.timelimiter.configs.default.timeout-duration=3s
resilience4j.timelimiter.instances.paymentCalc.base-config=default
# The max amount of time a call can last
resilience4j.timelimiter.instances.paymentCalc.timeout-duration=1s
# Cancel the Running Completable Futures After TimeOut.
resilience4j.timelimiter.instances.paymentCalc.cancel-running-future=true
# Max amount of parallel executions allowed by the bulkhead
resilience4j.bulkhead.configs.default.max-concurrent-calls=2
# Max amount of time a thread should be blocked for when attempting to enter a saturated bulkhead.
resilience4j.bulkhead.configs.default.max-wait-duration=0
resilience4j.bulkhead.instances.paymentCalc.base-config=default
This is my implementation:
// Bulkhead module is added because TimeLimiter needs separate execution thread instead of request thread
@Bulkhead(name = "paymentCalc", fallbackMethod = "localPaymentGenerate", type = Bulkhead.Type.THREADPOOL)
@TimeLimiter(name = "paymentCalc", fallbackMethod = "localPaymentGenerate")
@GetMapping(value = "/{workerId}/days/{days}")
public CompletableFuture<ResponseEntity<Payment>> getPayment(@PathVariable Long workerId, @PathVariable Integer days){
...
}
public CompletableFuture<ResponseEntity<Payment>> localPaymentGenerate(Long workerId, Integer days, Exception e){
System.out.println(e.getMessage()); // prints "TimeLimiter 'paymentCalc' recorded a timeout exception"
...
}
But, be careful, code not worked using spring-cloud-starter-circuitbreaker-resilience4j
dependencie, and i have spent a lot time trying to solve it.
The only thing that i needed to change to make @TimeLimiter get works is to change my dependecies.
I'm using spring-boot <version>2.5.4</version>
, <java.version>16</java.version>
and my dependencies are:
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>