Skip to content

Instantly share code, notes, and snippets.

@MasatoshiTada
Created March 28, 2022 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MasatoshiTada/64eca64691006e284fc5d81233995dfa to your computer and use it in GitHub Desktop.
Save MasatoshiTada/64eca64691006e284fc5d81233995dfa to your computer and use it in GitHub Desktop.
How to test Resilience4j bulkhead
import io.github.resilience4j.bulkhead.Bulkhead;
import io.github.resilience4j.bulkhead.BulkheadFullException;
import io.github.resilience4j.bulkhead.BulkheadRegistry;
import io.vavr.control.Try;
import org.springframework.stereotype.Component;
@Component
public class MyBulkhead {
private final Bulkhead bulkhead;
public MyBulkhead(BulkheadRegistry bulkheadRegistry) {
this.bulkhead = bulkheadRegistry.bulkhead("sample");
}
public void doLimit(Runnable r) {
Try.runRunnable(Bulkhead.decorateRunnable(bulkhead, r))
.recover(BulkheadFullException.class, e -> {
throw new MyException("Exceeded.");
}).recover(throwable -> {
throw new RuntimeException(throwable);
}).get();
}
}
class MyException extends RuntimeException {
public MyException(String message) {
super(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment