Turn LivenessState and ReadinessState into enums

Prior to this commit, `LivenessState` and `ReadinessState` were
immutable classes. This was done in order to have additional behavior
and information in those classes.

Because the current implementation doesn't need this, this commit turns
those classes into simple enums.
Additional state and information can be added to the
`*StateChangedEvent` classes.

See gh-19593
pull/20667/head
Brian Clozel 5 years ago
parent 6aa3461611
commit eb70fd952f

@ -38,7 +38,7 @@ public class LivenessProbeHealthIndicator extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
if (LivenessState.live().equals(this.applicationAvailabilityProvider.getLivenessState())) {
if (LivenessState.LIVE.equals(this.applicationAvailabilityProvider.getLivenessState())) {
builder.up();
}
else {

@ -38,7 +38,7 @@ public class ReadinessProbeHealthIndicator extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
if (ReadinessState.ready().equals(this.applicationAvailabilityProvider.getReadinessState())) {
if (ReadinessState.READY.equals(this.applicationAvailabilityProvider.getReadinessState())) {
builder.up();
}
else {

@ -46,13 +46,13 @@ class LivenessProbeHealthIndicatorTests {
@Test
void livenessIsLive() {
when(this.stateProvider.getLivenessState()).thenReturn(LivenessState.live());
when(this.stateProvider.getLivenessState()).thenReturn(LivenessState.LIVE);
assertThat(this.healthIndicator.health().getStatus()).isEqualTo(Status.UP);
}
@Test
void livenessIsBroken() {
when(this.stateProvider.getLivenessState()).thenReturn(LivenessState.broken());
when(this.stateProvider.getLivenessState()).thenReturn(LivenessState.BROKEN);
assertThat(this.healthIndicator.health().getStatus()).isEqualTo(Status.DOWN);
}

@ -46,13 +46,13 @@ class ReadinessProbeHealthIndicatorTests {
@Test
void readinessIsReady() {
when(this.stateProvider.getReadinessState()).thenReturn(ReadinessState.ready());
when(this.stateProvider.getReadinessState()).thenReturn(ReadinessState.READY);
assertThat(this.healthIndicator.health().getStatus()).isEqualTo(Status.UP);
}
@Test
void readinessIsUnready() {
when(this.stateProvider.getReadinessState()).thenReturn(ReadinessState.unready());
when(this.stateProvider.getReadinessState()).thenReturn(ReadinessState.UNREADY);
assertThat(this.healthIndicator.health().getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
}

@ -39,10 +39,10 @@ public class ApplicationAvailabilityProvider implements ApplicationListener<Appl
/**
* Create a new {@link ApplicationAvailabilityProvider} instance with
* {@link LivenessState#broken()} and {@link ReadinessState#unready()}.
* {@link LivenessState#BROKEN} and {@link ReadinessState#UNREADY}.
*/
public ApplicationAvailabilityProvider() {
this(LivenessState.broken(), ReadinessState.unready());
this(LivenessState.BROKEN, ReadinessState.UNREADY);
}
/**

@ -16,8 +16,6 @@
package org.springframework.boot.availability;
import java.util.Objects;
/**
* "Liveness" state of the application.
* <p>
@ -28,48 +26,7 @@ import java.util.Objects;
* @author Brian Clozel
* @since 2.3.0
*/
public final class LivenessState {
private final Status status;
LivenessState(Status status) {
this.status = status;
}
public Status getStatus() {
return this.status;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
return this.status == ((LivenessState) obj).status;
}
@Override
public int hashCode() {
return Objects.hash(this.status);
}
@Override
public String toString() {
return "LivenessState{" + "status=" + this.status + '}';
}
public static LivenessState broken() {
return new LivenessState(Status.BROKEN);
}
public static LivenessState live() {
return new LivenessState(Status.LIVE);
}
public enum Status {
public enum LivenessState {
/**
* The application is running and its internal state is correct.
@ -82,5 +39,3 @@ public final class LivenessState {
BROKEN
}
}

@ -47,7 +47,7 @@ public class LivenessStateChangedEvent extends ApplicationEvent {
* @return the application event
*/
public static LivenessStateChangedEvent live(String cause) {
return new LivenessStateChangedEvent(LivenessState.live(), cause);
return new LivenessStateChangedEvent(LivenessState.LIVE, cause);
}
/**
@ -57,7 +57,7 @@ public class LivenessStateChangedEvent extends ApplicationEvent {
* @return the application event
*/
public static LivenessStateChangedEvent broken(String cause) {
return new LivenessStateChangedEvent(LivenessState.broken(), cause);
return new LivenessStateChangedEvent(LivenessState.BROKEN, cause);
}
/**
@ -68,7 +68,7 @@ public class LivenessStateChangedEvent extends ApplicationEvent {
* @return the application event
*/
public static LivenessStateChangedEvent broken(Throwable throwable) {
return new LivenessStateChangedEvent(LivenessState.broken(), throwable.getMessage());
return new LivenessStateChangedEvent(LivenessState.BROKEN, throwable.getMessage());
}
}

@ -16,8 +16,6 @@
package org.springframework.boot.availability;
import java.util.Objects;
/**
* "Readiness" state of the application.
* <p>
@ -28,48 +26,7 @@ import java.util.Objects;
* @author Brian Clozel
* @since 2.3.0
*/
public final class ReadinessState {
private final Availability availability;
private ReadinessState(Availability availability) {
this.availability = availability;
}
public Availability getAvailability() {
return this.availability;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
return this.availability == ((ReadinessState) obj).availability;
}
@Override
public int hashCode() {
return Objects.hash(this.availability);
}
@Override
public String toString() {
return "ReadinessState{" + "availability=" + this.availability + '}';
}
public static ReadinessState ready() {
return new ReadinessState(Availability.READY);
}
public static ReadinessState unready() {
return new ReadinessState(Availability.UNREADY);
}
public enum Availability {
public enum ReadinessState {
/**
* The application is not willing to receive traffic.
@ -82,5 +39,3 @@ public final class ReadinessState {
READY
}
}

@ -43,7 +43,7 @@ public class ReadinessStateChangedEvent extends ApplicationEvent {
* @return the application event
*/
public static ReadinessStateChangedEvent ready() {
return new ReadinessStateChangedEvent(ReadinessState.ready());
return new ReadinessStateChangedEvent(ReadinessState.READY);
}
/**
@ -52,7 +52,7 @@ public class ReadinessStateChangedEvent extends ApplicationEvent {
* @return the application event
*/
public static ReadinessStateChangedEvent unready() {
return new ReadinessStateChangedEvent(ReadinessState.unready());
return new ReadinessStateChangedEvent(ReadinessState.UNREADY);
}
}

@ -30,14 +30,14 @@ class ApplicationAvailabilityProviderTests {
@Test
void initialStateShouldBeFailures() {
ApplicationAvailabilityProvider stateProvider = new ApplicationAvailabilityProvider();
assertThat(stateProvider.getLivenessState()).isEqualTo(LivenessState.broken());
assertThat(stateProvider.getReadinessState()).isEqualTo(ReadinessState.unready());
assertThat(stateProvider.getLivenessState()).isEqualTo(LivenessState.BROKEN);
assertThat(stateProvider.getReadinessState()).isEqualTo(ReadinessState.UNREADY);
}
@Test
void updateLivenessState() {
ApplicationAvailabilityProvider stateProvider = new ApplicationAvailabilityProvider();
LivenessState livenessState = LivenessState.live();
LivenessState livenessState = LivenessState.LIVE;
stateProvider.onApplicationEvent(new LivenessStateChangedEvent(livenessState, "Startup complete"));
assertThat(stateProvider.getLivenessState()).isEqualTo(livenessState);
}
@ -46,7 +46,7 @@ class ApplicationAvailabilityProviderTests {
void updateReadiessState() {
ApplicationAvailabilityProvider stateProvider = new ApplicationAvailabilityProvider();
stateProvider.onApplicationEvent(ReadinessStateChangedEvent.ready());
assertThat(stateProvider.getReadinessState()).isEqualTo(ReadinessState.ready());
assertThat(stateProvider.getReadinessState()).isEqualTo(ReadinessState.READY);
}
}

Loading…
Cancel
Save