io.github.resilience4j.reactor.circuitbreaker.operator.CircuitBreakerOperator Java Examples

The following examples show how to use io.github.resilience4j.reactor.circuitbreaker.operator.CircuitBreakerOperator. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReactiveResilience4JCircuitBreaker.java    From spring-cloud-circuitbreaker with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Mono<T> run(Mono<T> toRun, Function<Throwable, Mono<T>> fallback) {
	io.github.resilience4j.circuitbreaker.CircuitBreaker defaultCircuitBreaker = registry
			.circuitBreaker(id, config.getCircuitBreakerConfig());
	circuitBreakerCustomizer
			.ifPresent(customizer -> customizer.customize(defaultCircuitBreaker));
	Mono<T> toReturn = toRun
			.transform(CircuitBreakerOperator.of(defaultCircuitBreaker))
			.timeout(config.getTimeLimiterConfig().getTimeoutDuration())
			// Since we are using the Mono timeout we need to tell the circuit breaker
			// about the error
			.doOnError(TimeoutException.class,
					t -> defaultCircuitBreaker.onError(config.getTimeLimiterConfig()
							.getTimeoutDuration().toMillis(), TimeUnit.MILLISECONDS,
							t));
	if (fallback != null) {
		toReturn = toReturn.onErrorResume(fallback);
	}
	return toReturn;
}
 
Example #2
Source File: ReactiveResilience4JCircuitBreaker.java    From spring-cloud-circuitbreaker with Apache License 2.0 6 votes vote down vote up
public <T> Flux<T> run(Flux<T> toRun, Function<Throwable, Flux<T>> fallback) {
	io.github.resilience4j.circuitbreaker.CircuitBreaker defaultCircuitBreaker = registry
			.circuitBreaker(id, config.getCircuitBreakerConfig());
	circuitBreakerCustomizer
			.ifPresent(customizer -> customizer.customize(defaultCircuitBreaker));
	Flux<T> toReturn = toRun
			.transform(CircuitBreakerOperator.of(defaultCircuitBreaker))
			.timeout(config.getTimeLimiterConfig().getTimeoutDuration())
			// Since we are using the Flux timeout we need to tell the circuit breaker
			// about the error
			.doOnError(TimeoutException.class,
					t -> defaultCircuitBreaker.onError(config.getTimeLimiterConfig()
							.getTimeoutDuration().toMillis(), TimeUnit.MILLISECONDS,
							t));
	if (fallback != null) {
		toReturn = toReturn.onErrorResume(fallback);
	}
	return toReturn;
}
 
Example #3
Source File: ReactorCircuitBreakerAspectExt.java    From resilience4j with Apache License 2.0 6 votes vote down vote up
/**
 * handle the Spring web flux (Flux /Mono) return types AOP based into reactor circuit-breaker
 * See {@link io.github.resilience4j.reactor.circuitbreaker.operator.CircuitBreakerOperator} for
 * details.
 *
 * @param proceedingJoinPoint Spring AOP proceedingJoinPoint
 * @param circuitBreaker      the configured circuitBreaker
 * @param methodName          the method name
 * @return the result object
 * @throws Throwable exception in case of faulty flow
 */
@Override
public Object handle(ProceedingJoinPoint proceedingJoinPoint, CircuitBreaker circuitBreaker,
    String methodName) throws Throwable {
    Object returnValue = proceedingJoinPoint.proceed();
    if (Flux.class.isAssignableFrom(returnValue.getClass())) {
        Flux<?> fluxReturnValue = (Flux<?>) returnValue;
        return fluxReturnValue.transformDeferred(
            io.github.resilience4j.reactor.circuitbreaker.operator.CircuitBreakerOperator
                .of(circuitBreaker));
    } else if (Mono.class.isAssignableFrom(returnValue.getClass())) {
        Mono<?> monoReturnValue = (Mono<?>) returnValue;
        return monoReturnValue.transformDeferred(CircuitBreakerOperator.of(circuitBreaker));
    } else {
        logger.error("Unsupported type for Reactor circuit breaker {}",
            returnValue.getClass().getTypeName());
        throw new IllegalArgumentException(
            "Not Supported type for the circuit breaker in Reactor:" + returnValue.getClass()
                .getName());

    }
}
 
Example #4
Source File: BackendBController.java    From resilience4j-spring-boot2-demo with Apache License 2.0 5 votes vote down vote up
private <T> Mono<T> executeWithFallback(Mono<T> publisher, Function<Throwable, Mono<T>> fallback){
    return publisher
            .transform(TimeLimiterOperator.of(timeLimiter))
            .transform(BulkheadOperator.of(bulkhead))
            .transform(CircuitBreakerOperator.of(circuitBreaker))
            .onErrorResume(TimeoutException.class, fallback)
            .onErrorResume(CallNotPermittedException.class, fallback)
            .onErrorResume(BulkheadFullException.class, fallback);
}
 
Example #5
Source File: BackendBController.java    From resilience4j-spring-boot2-demo with Apache License 2.0 5 votes vote down vote up
private <T> Flux<T> executeWithFallback(Flux<T> publisher, Function<Throwable, Flux<T>> fallback){
    return publisher
            .transform(TimeLimiterOperator.of(timeLimiter))
            .transform(BulkheadOperator.of(bulkhead))
            .transform(CircuitBreakerOperator.of(circuitBreaker))
            .onErrorResume(TimeoutException.class, fallback)
            .onErrorResume(CallNotPermittedException.class, fallback)
            .onErrorResume(BulkheadFullException.class, fallback);
}
 
Example #6
Source File: CombinedOperatorsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitEvents() {
    StepVerifier.create(
        Flux.just("Event 1", "Event 2")
            .transformDeferred(BulkheadOperator.of(bulkhead))
            .transformDeferred(RateLimiterOperator.of(rateLimiter))
            .transformDeferred(CircuitBreakerOperator.of(circuitBreaker))
    ).expectNext("Event 1")
        .expectNext("Event 2")
        .verifyComplete();
}
 
Example #7
Source File: CombinedOperatorsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitEventsWithRetry() {
    StepVerifier.create(
        Flux.just("Event 1", "Event 2")
            .transformDeferred(retryOperator)
            .transformDeferred(BulkheadOperator.of(bulkhead))
            .transformDeferred(RateLimiterOperator.of(rateLimiter))
            .transformDeferred(CircuitBreakerOperator.of(circuitBreaker))
    ).expectNext("Event 1")
        .expectNext("Event 2")
        .verifyComplete();
}
 
Example #8
Source File: CombinedOperatorsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitEvent() {
    StepVerifier.create(
        Mono.just("Event 1")
            .transformDeferred(BulkheadOperator.of(bulkhead))
            .transformDeferred(RateLimiterOperator.of(rateLimiter))
            .transformDeferred(CircuitBreakerOperator.of(circuitBreaker))
    ).expectNext("Event 1")
        .verifyComplete();
}
 
Example #9
Source File: CombinedOperatorsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitEventWithRetry() {
    StepVerifier.create(
        Mono.just("Event 1")
            .transformDeferred(retryOperator)
            .transformDeferred(BulkheadOperator.of(bulkhead))
            .transformDeferred(RateLimiterOperator.of(rateLimiter))
            .transformDeferred(CircuitBreakerOperator.of(circuitBreaker))
    ).expectNext("Event 1")
        .verifyComplete();
}
 
Example #10
Source File: CombinedOperatorsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldPropagateError() {
    StepVerifier.create(
        Flux.error(new IOException("BAM!"))
            .transformDeferred(BulkheadOperator.of(bulkhead))
            .transformDeferred(RateLimiterOperator.of(rateLimiter))
            .transformDeferred(CircuitBreakerOperator.of(circuitBreaker))
    ).expectError(IOException.class)
        .verify(Duration.ofSeconds(1));
}
 
Example #11
Source File: CombinedOperatorsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithCircuitBreakerOpenExceptionEvenWhenErrorDuringSubscribe() {
    circuitBreaker.transitionToOpenState();
    StepVerifier.create(
        Flux.error(new IOException("BAM!"))
            .transformDeferred(CircuitBreakerOperator.of(circuitBreaker))
            .transformDeferred(BulkheadOperator.of(bulkhead))
            .transformDeferred(RateLimiterOperator.of(rateLimiter))
    ).expectError(CallNotPermittedException.class)
        .verify(Duration.ofSeconds(1));
}
 
Example #12
Source File: CombinedOperatorsTest.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldEmitErrorWithCircuitBreakerOpenExceptionEvenWhenErrorNotOnSubscribe() {
    circuitBreaker.transitionToOpenState();
    StepVerifier.create(
        Flux.error(new IOException("BAM!"), true)
            .transformDeferred(CircuitBreakerOperator.of(circuitBreaker))
            .transformDeferred(BulkheadOperator.of(bulkhead))
            .transformDeferred(RateLimiterOperator.of(rateLimiter))
    ).expectError(CallNotPermittedException.class)
        .verify(Duration.ofSeconds(1));
}
 
Example #13
Source File: BackendBController.java    From resilience4j-spring-boot2-demo with Apache License 2.0 4 votes vote down vote up
private <T> Mono<T> execute(Mono<T> publisher){
    return publisher
            .transform(BulkheadOperator.of(bulkhead))
            .transform(CircuitBreakerOperator.of(circuitBreaker))
            .transform(RetryOperator.of(retry));
}
 
Example #14
Source File: BackendBController.java    From resilience4j-spring-boot2-demo with Apache License 2.0 4 votes vote down vote up
private <T> Flux<T> execute(Flux<T> publisher){
    return publisher
            .transform(BulkheadOperator.of(bulkhead))
            .transform(CircuitBreakerOperator.of(circuitBreaker))
            .transform(RetryOperator.of(retry));
}