Spring / Resilience4j Interview questions
How do you use event publishers to log CircuitBreaker state transitions?
circuitBreaker.getEventPublisher() .onStateTransition(event -> log.info("CB '{}' transitioned {} -> {}", event.getCircuitBreakerName(), event.getStateTransition().getFromState(), event.getStateTransition().getToState())) .onCallNotPermitted(event -> log.warn("Call rejected: CB '{}' is OPEN", event.getCircuitBreakerName())) .onError(event -> log.error("Call failed on '{}': {}", event.getCircuitBreakerName(), event.getThrowable().getMessage()));
Every CircuitBreaker exposes an EventPublisher with dedicated hooks - onStateTransition, onSuccess, onError, onCallNotPermitted, onIgnoredError, and more - so listeners can be attached without modifying the call sites that actually use the breaker.
This is the mechanism most alerting hooks into: a Slack or PagerDuty notification for "dependency X just opened its circuit" is typically wired straight off onStateTransition, filtering for transitions into the OPEN state specifically, rather than polling the breaker's state on a timer.
More Related questions...