Search in sources :

Example 1 with EventConsumer

use of io.github.resilience4j.core.EventConsumer in project resilience4j by resilience4j.

the class CircuitBreakerAutoTransitionStateMachineTest method setUp.

@Before
public void setUp() {
    CircuitBreakerConfig circuitBreakerConfigGroupA = CircuitBreakerConfig.custom().failureRateThreshold(50).ringBufferSizeInClosedState(5).ringBufferSizeInHalfOpenState(3).enableAutomaticTransitionFromOpenToHalfOpen().waitDurationInOpenState(Duration.ofSeconds(2)).recordFailure(error -> !(error instanceof NumberFormatException)).build();
    CircuitBreakerConfig circuitBreakerConfigGroupB = CircuitBreakerConfig.custom().failureRateThreshold(50).ringBufferSizeInClosedState(5).ringBufferSizeInHalfOpenState(3).enableAutomaticTransitionFromOpenToHalfOpen().waitDurationInOpenState(Duration.ofSeconds(1)).recordFailure(error -> !(error instanceof NumberFormatException)).build();
    // Instantiate multiple circuit breakers in two groups, A & B
    for (int i = 0; i < TOTAL_NUMBER_CIRCUIT_BREAKERS; i++) {
        stateTransitionFromOpenToHalfOpen.put(i, 0);
        // On state transition from OPEN to HALF_OPEN, increment a count
        int finalI = i;
        EventConsumer<CircuitBreakerOnStateTransitionEvent> eventConsumer = transition -> {
            if (transition.getStateTransition().getFromState().equals(CircuitBreaker.State.OPEN) && transition.getStateTransition().getToState().equals(CircuitBreaker.State.HALF_OPEN)) {
                Integer currentCount = stateTransitionFromOpenToHalfOpen.get(finalI);
                stateTransitionFromOpenToHalfOpen.put(finalI, currentCount + 1);
            }
        };
        CircuitBreaker circuitBreaker;
        if (i < TOTAL_NUMBER_CIRCUIT_BREAKERS / 2) {
            circuitBreaker = new CircuitBreakerStateMachine("testNameA" + i, circuitBreakerConfigGroupA);
            circuitBreaker.getEventPublisher().onStateTransition(eventConsumer);
            circuitBreakersGroupA.add(circuitBreaker);
        } else {
            circuitBreaker = new CircuitBreakerStateMachine("testNameB" + i, circuitBreakerConfigGroupB);
            circuitBreaker.getEventPublisher().onStateTransition(eventConsumer);
            circuitBreakersGroupB.add(circuitBreaker);
        }
    }
}
Also used : CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CircuitBreakerOnStateTransitionEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerOnStateTransitionEvent) Test(org.junit.Test) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) EventConsumer(io.github.resilience4j.core.EventConsumer) List(java.util.List) Duration(java.time.Duration) Map(java.util.Map) Thread.sleep(java.lang.Thread.sleep) BDDAssertions.assertThat(org.assertj.core.api.BDDAssertions.assertThat) Before(org.junit.Before) CircuitBreaker(io.github.resilience4j.circuitbreaker.CircuitBreaker) CircuitBreakerOnStateTransitionEvent(io.github.resilience4j.circuitbreaker.event.CircuitBreakerOnStateTransitionEvent) CircuitBreakerConfig(io.github.resilience4j.circuitbreaker.CircuitBreakerConfig) Before(org.junit.Before)

Aggregations

CircuitBreaker (io.github.resilience4j.circuitbreaker.CircuitBreaker)1 CircuitBreakerConfig (io.github.resilience4j.circuitbreaker.CircuitBreakerConfig)1 CircuitBreakerOnStateTransitionEvent (io.github.resilience4j.circuitbreaker.event.CircuitBreakerOnStateTransitionEvent)1 EventConsumer (io.github.resilience4j.core.EventConsumer)1 Thread.sleep (java.lang.Thread.sleep)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Function (java.util.function.Function)1 BDDAssertions.assertThat (org.assertj.core.api.BDDAssertions.assertThat)1 Before (org.junit.Before)1 Test (org.junit.Test)1