HomeGuidesChangelog
GuidesGitHubLog In

Examples

Examples of resilience4j-reactor

Decorate Mono or Flux with a CircuitBreaker

The following example shows how to decorate a Mono by using the custom Reactor operator. Flux is also supported.

The CircuitBreakerOperator checks if a downstream subscriber/observer can acquire a permission to subscribe to an upstream Publisher. If the CircuitBreaker is OPEN, the CircuitBreakerOperator emits a CallNotPermittedException to the downstream subscriber.

CircuitBreaker circuitBreaker = CircuitBreaker.ofDefaults("name");
Mono.fromCallable(backendService::doSomething)
    .transformDeferred(CircuitBreakerOperator.of(circuitBreaker))

Decorate Mono or Flux with a RateLimiter

The following example shows how to decorate a Mono by using the custom Reactor operator. Flux is also supported.

The RateLimiterOperator checks if a downstream subscriber/observer can acquire a permission to subscribe to an upstream Publisher. If the rate limit would be exceeded, the RateLimiterOperator could either delay requesting data from the upstream or it can emit a RequestNotPermitted error to the downstream subscriber.

RateLimiter rateLimiter = RateLimiter.ofDefaults("name");
Mono.fromCallable(backendService::doSomething)
    .transformDeferred(RateLimiterOperator.of(rateLimiter))

Decorate Mono or Flux with a Bulkhead

The following example shows how to decorate a Mono by using the custom Reactor operator. Flux is also supported.

The BulkheadOperator checks if a downstream subscriber/observer can acquire a permission to subscribe to an upstream Publisher. If the Bulhead is full, the BulkheadOperator emits a BulkheadFullException to the downstream subscriber.

Bulkhead bulkhead = Bulkhead.ofDefaults("name");
Mono.fromCallable(backendService::doSomething)
  .transformDeferred(BulkheadOperator.of(bulkhead));

Decorate Mono or Flux with Retry

Retry retry = Retry.ofDefaults("backendName");
Mono.fromCallable(backendService::doSomething)
    .transformDeferred(RetryOperator.of(retry))