Created
March 28, 2022 08:37
-
-
Save MasatoshiTada/64eca64691006e284fc5d81233995dfa to your computer and use it in GitHub Desktop.
How to test Resilience4j bulkhead
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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