1.应用程序的可用性:
default LivenessState getLivenessState() {
return getState(LivenessState.class, LivenessState.BROKEN);
}
/**
* Return the {@link ReadinessState} of the application.
* @return the readiness state
*/
default ReadinessState getReadinessState() {
return getState(ReadinessState.class, ReadinessState.REFUSING_TRAFFIC);
}
应用程序:可用性:
private final Map<Class<? extends AvailabilityState>, AvailabilityChangeEvent<?>> events = new ConcurrentHashMap<>();
private final Log logger;
public ApplicationAvailabilityBean() {
this(LogFactory.getLog(ApplicationAvailabilityBean.class));
}
ApplicationAvailabilityBean(Log logger) {
this.logger = logger;
}
@Override
public <S extends AvailabilityState> S getState(Class<S> stateType, S defaultState) {
Assert.notNull(stateType, "StateType must not be null");
Assert.notNull(defaultState, "DefaultState must not be null");
S state = getState(stateType);
return (state != null) ? state : defaultState;
}
@Override
public <S extends AvailabilityState> S getState(Class<S> stateType) {
AvailabilityChangeEvent<S> event = getLastChangeEvent(stateType);
return (event != null) ? event.getState() : null;
}
@Override
@SuppressWarnings("unchecked")
public <S extends AvailabilityState> AvailabilityChangeEvent<S> getLastChangeEvent(Class<S> stateType) {
return (AvailabilityChangeEvent<S>) this.events.get(stateType);
}
改变事件:
public class AvailabilityChangeEvent<S extends AvailabilityState> extends PayloadApplicationEvent<S> {
/**
* Create a new {@link AvailabilityChangeEvent} instance.
* @param source the source of the event
* @param state the availability state (never {@code null})
*/
public AvailabilityChangeEvent(Object source, S state) {
super(source, state);
}
/**
* Return the changed availability state.
* @return the availability state
*/
public S getState() {
return getPayload();
}
@Override
public ResolvableType getResolvableType() {
return ResolvableType.forClassWithGenerics(getClass(), getStateType());
}
private Class<?> getStateType() {
S state = getState();
if (state instanceof Enum) {
return ((Enum<?>) state).getDeclaringClass();
}
return state.getClass();
}
/**
* Convenience method that can be used to publish an {@link AvailabilityChangeEvent}
* to the given application context.
* @param <S> the availability state type
* @param context the context used to publish the event
* @param state the changed availability state
*/
public static <S extends AvailabilityState> void publish(ApplicationContext context, S state) {
Assert.notNull(context, "Context must not be null");
publish(context, context, state);
}
/**
* Convenience method that can be used to publish an {@link AvailabilityChangeEvent}
* to the given application context.
* @param <S> the availability state type
* @param publisher the publisher used to publish the event
* @param source the source of the event
* @param state the changed availability state
*/
public static <S extends AvailabilityState> void publish(ApplicationEventPublisher publisher, Object source,
S state) {
Assert.notNull(publisher, "Publisher must not be null");
publisher.publishEvent(new AvailabilityChangeEvent<>(source, state));
}
LivenessState:
public enum LivenessState implements AvailabilityState {
/**
* The application is running and its internal state is correct.
*/
CORRECT,
/**
* The application is running but its internal state is broken.
*/
BROKEN
}
ReadinessState:
```java
public enum ReadinessState implements AvailabilityState {
/**
* The application is ready to receive traffic.
*/
ACCEPTING_TRAFFIC,
/**
* The application is not willing to receive traffic.
*/
REFUSING_TRAFFIC
}