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
This commit is contained in:
Brian Clozel
2020-03-24 18:22:43 +01:00
parent 6aa3461611
commit eb70fd952f
10 changed files with 35 additions and 125 deletions

View File

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

View File

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

View File

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

View File

@@ -46,13 +46,13 @@ class ReadinessProbeHealthIndicatorTests {
@Test @Test
void readinessIsReady() { void readinessIsReady() {
when(this.stateProvider.getReadinessState()).thenReturn(ReadinessState.ready()); when(this.stateProvider.getReadinessState()).thenReturn(ReadinessState.READY);
assertThat(this.healthIndicator.health().getStatus()).isEqualTo(Status.UP); assertThat(this.healthIndicator.health().getStatus()).isEqualTo(Status.UP);
} }
@Test @Test
void readinessIsUnready() { 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); assertThat(this.healthIndicator.health().getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
} }

View File

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

View File

@@ -16,8 +16,6 @@
package org.springframework.boot.availability; package org.springframework.boot.availability;
import java.util.Objects;
/** /**
* "Liveness" state of the application. * "Liveness" state of the application.
* <p> * <p>
@@ -28,59 +26,16 @@ import java.util.Objects;
* @author Brian Clozel * @author Brian Clozel
* @since 2.3.0 * @since 2.3.0
*/ */
public final class LivenessState { public enum LivenessState {
private final Status status; /**
* The application is running and its internal state is correct.
*/
LIVE,
LivenessState(Status status) { /**
this.status = status; * The internal state of the application is broken.
} */
BROKEN
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 {
/**
* The application is running and its internal state is correct.
*/
LIVE,
/**
* The internal state of the application is broken.
*/
BROKEN
}
} }

View File

@@ -47,7 +47,7 @@ public class LivenessStateChangedEvent extends ApplicationEvent {
* @return the application event * @return the application event
*/ */
public static LivenessStateChangedEvent live(String cause) { 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 * @return the application event
*/ */
public static LivenessStateChangedEvent broken(String cause) { 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 * @return the application event
*/ */
public static LivenessStateChangedEvent broken(Throwable throwable) { public static LivenessStateChangedEvent broken(Throwable throwable) {
return new LivenessStateChangedEvent(LivenessState.broken(), throwable.getMessage()); return new LivenessStateChangedEvent(LivenessState.BROKEN, throwable.getMessage());
} }
} }

View File

@@ -16,8 +16,6 @@
package org.springframework.boot.availability; package org.springframework.boot.availability;
import java.util.Objects;
/** /**
* "Readiness" state of the application. * "Readiness" state of the application.
* <p> * <p>
@@ -28,59 +26,16 @@ import java.util.Objects;
* @author Brian Clozel * @author Brian Clozel
* @since 2.3.0 * @since 2.3.0
*/ */
public final class ReadinessState { public enum ReadinessState {
private final Availability availability; /**
* The application is not willing to receive traffic.
*/
UNREADY,
private ReadinessState(Availability availability) { /**
this.availability = availability; * The application is ready to receive traffic.
} */
READY
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 {
/**
* The application is not willing to receive traffic.
*/
UNREADY,
/**
* The application is ready to receive traffic.
*/
READY
}
} }

View File

@@ -43,7 +43,7 @@ public class ReadinessStateChangedEvent extends ApplicationEvent {
* @return the application event * @return the application event
*/ */
public static ReadinessStateChangedEvent ready() { 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 * @return the application event
*/ */
public static ReadinessStateChangedEvent unready() { public static ReadinessStateChangedEvent unready() {
return new ReadinessStateChangedEvent(ReadinessState.unready()); return new ReadinessStateChangedEvent(ReadinessState.UNREADY);
} }
} }

View File

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