Skip to content

Instantly share code, notes, and snippets.

@yonatang
Created March 14, 2020 05:46
Show Gist options
  • Save yonatang/d0bffd73ce9cca241c4b59779d697f83 to your computer and use it in GitHub Desktop.
Save yonatang/d0bffd73ce9cca241c4b59779d697f83 to your computer and use it in GitHub Desktop.
resilience4j Iissue: llegalStateException: Required to bind 2 arguments, but only bound 1
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<java.version>11</java.version>
</properties>
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.2.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-spring-boot2</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
</project>
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
import io.github.resilience4j.bulkhead.annotation.Bulkhead.Type;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.retry.annotation.Retry;
import io.github.resilience4j.timelimiter.annotation.TimeLimiter;
import java.util.Date;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@EnableAsync(proxyTargetClass = true)
public class TestMain {
static {
var WAIT_DURATION="10ms";
System.setProperty("resilience4j.circuitbreaker.instances.r4j.waitDurationInOpenState",WAIT_DURATION);
}
@RestController
class Ctrl {
@Autowired
BadService badService;
@GetMapping("/")
String doStuff() {
badService.exceptionallyBad().whenComplete((v, e) -> {
System.out.println("Value: " + v + ", e: " + e);
if (e != null) {
e.printStackTrace();
}
});
return new Date().toString();
}
@PostConstruct
public void test() throws InterruptedException {
AtomicInteger cnt = new AtomicInteger();
var N = 1000;
var latch = new CountDownLatch(N);
for (int i = 0; i < N; i++) {
badService.exceptionallyBad().whenComplete((v, e) -> {
var c = e.getCause();
if (c instanceof IllegalStateException) {
cnt.incrementAndGet();
System.err.println("That should not happen");
e.printStackTrace();
}
latch.countDown();
});
}
System.out.println("Done");
latch.await();
System.out.println("Failed " + cnt.get() + " out of " + N + " times");
}
}
@CircuitBreaker(name = "r4j")
@Retry(name = "r4j")
@TimeLimiter(name = "r4j")
// @Bulkhead(name = "r4j", type = Type.THREADPOOL)
@Service
class BadService {
CompletableFuture<Void> exceptionallyBad() {
return CompletableFuture.failedFuture(new UnsupportedOperationException());
}
}
public static void main(String[] args) {
SpringApplication.run(TestMain.class, args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment