Neetu
Neetu

Reputation: 121

How to track the change in state of the CircuitBreaker if it is created using @CircuitBreaker annotaion

@CircuitBreaker(name = "test")

I used above annotation to create the circuit breaker. Now I want to track any change in the state of circuit breaker. e.g - when it goes from open to close, I want to get to know and execute some piece of code.

Is there way to do that?

I went through different documentarians, but didn't find any solution.

Upvotes: 0

Views: 1253

Answers (1)

Neetu
Neetu

Reputation: 121

Found the solution. Just need to add the below bean in your code.

@Bean
public RegistryEventConsumer<CircuitBreaker> myRegistryEventConsumer() {

    return new RegistryEventConsumer<io.github.resilience4j.circuitbreaker.CircuitBreaker>() {
        @Override
        public void onEntryAddedEvent(EntryAddedEvent<CircuitBreaker> entryAddedEvent) {
            entryAddedEvent.getAddedEntry().getEventPublisher().onEvent(event -> System.out.println(event.toString()));
        }

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

        }

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

        }
    };
}

Upvotes: 2

Related Questions