poseidon_rishi
poseidon_rishi

Reputation: 81

How to configure to events in Resilience4j Spring starter

I have configured by resilience4j circuitbreaker factory bean like below.But i couldnot get a function to ovveride event listeners example to open , close etc .Please help

@Bean
public Customizer<Resilience4JCircuitBreakerFactory> globalCustomConfiguration() {
    CircuitBreakerConfig circuitBreakerConfig = CircuitBreakerConfig.custom()
            .failureRateThreshold(Float.parseFloat(failureRateThreshold))
            .waitDurationInOpenState(Duration.ofMillis(Long.parseLong(waitDurationInOpenState)))
            .slidingWindowSize(Integer.parseInt(slidingWindowSize)).build();
    TimeLimiterConfig timeLimiterConfig = TimeLimiterConfig.custom()
            .timeoutDuration(Duration.ofSeconds(Long.parseLong(timelimiterDuration))).build();

    // the circuitBreakerConfig and timeLimiterConfig objects
    return factory -> factory.configureDefault(id -> new Resilience4JConfigBuilder(id)
            .timeLimiterConfig(timeLimiterConfig).circuitBreakerConfig(circuitBreakerConfig).build());
}

Upvotes: 3

Views: 4526

Answers (1)

Robert Winkler
Robert Winkler

Reputation: 1907

I recommend to use resilience4j-spring-boot2. It provides a lot features like annotation support, external configuration, metrics, and many more -> https://resilience4j.readme.io/docs/getting-started-3

Our starter supports the following:

You can add a RegistryEventConsumer bean in order to add event consumers to newly created instances. For example, you can add an RegistryEventConsumer to the CircuitBreakerRegistry in order to register a logging event consumer to every newly created CircuitBreaker instance.

@Bean
public RegistryEventConsumer<CircuitBreaker> myRegistryEventConsumer() {

    return new RegistryEventConsumer<CircuitBreaker>() {
        @Override
        public void onEntryAddedEvent(EntryAddedEvent<CircuitBreaker> entryAddedEvent) {
            entryAddedEvent.getAddedEntry().getEventPublisher().onEvent(event -> LOG.info(event.toString()));
        }

        @Override
        public void onEntryRemovedEvent(EntryRemovedEvent<CircuitBreaker> entryRemoveEvent) {

        }

        @Override
        public void onEntryReplacedEvent(EntryReplacedEvent<CircuitBreaker> entryReplacedEvent) {

        }
    };
}

Upvotes: 3

Related Questions