An availability change event records a change in an application's liveness or readiness state. Spring, a dependency, or application code can publish it through the application context. section 18.3.3
Publish different states for different operational conditions:
- Use ReadinessState.REFUSING_TRAFFIC while initialization is incomplete, then publish ReadinessState.ACCEPTING_TRAFFIC. section 18.3.3
- Use LivenessState.BROKEN after an unrecoverable fatal error, allowing Kubernetes to restart the application; publish LivenessState.CORRECT for a healthy state. section 18.3.3
Delay readiness during startup
An ApplicationRunner can receive the Spring application context and call AvailabilityChangeEvent.publish(context, ReadinessState.REFUSING_TRAFFIC) before initialization finishes. Afterward, publish ReadinessState.ACCEPTING_TRAFFIC so the application can accept traffic. section 18.3.3
@Bean
public ApplicationRunner disableLiveness(ApplicationContext context) {
return args -> {
AvailabilityChangeEvent.publish(context, ReadinessState.REFUSING_TRAFFIC);
};
}
Shows the Spring bean and publish call used to change readiness during application startup. section 18.3.3
