From eb70fd952fd6ef92468d87dfd553ff63c48e8e55 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Tue, 24 Mar 2020 18:22:43 +0100 Subject: [PATCH] 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 --- .../LivenessProbeHealthIndicator.java | 2 +- .../ReadinessProbeHealthIndicator.java | 2 +- .../LivenessProbeHealthIndicatorTests.java | 4 +- .../ReadinessProbeHealthIndicatorTests.java | 4 +- .../ApplicationAvailabilityProvider.java | 4 +- .../boot/availability/LivenessState.java | 63 +++---------------- .../LivenessStateChangedEvent.java | 6 +- .../boot/availability/ReadinessState.java | 63 +++---------------- .../ReadinessStateChangedEvent.java | 4 +- .../ApplicationAvailabilityProviderTests.java | 8 +-- 10 files changed, 35 insertions(+), 125 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/availability/LivenessProbeHealthIndicator.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/availability/LivenessProbeHealthIndicator.java index b8b3c9bad5..0e0c8a3f89 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/availability/LivenessProbeHealthIndicator.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/availability/LivenessProbeHealthIndicator.java @@ -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 { diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/availability/ReadinessProbeHealthIndicator.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/availability/ReadinessProbeHealthIndicator.java index ca785fe634..004ec729cd 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/availability/ReadinessProbeHealthIndicator.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/availability/ReadinessProbeHealthIndicator.java @@ -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 { diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/availability/LivenessProbeHealthIndicatorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/availability/LivenessProbeHealthIndicatorTests.java index 3b39c74c29..b738b87082 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/availability/LivenessProbeHealthIndicatorTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/availability/LivenessProbeHealthIndicatorTests.java @@ -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); } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/availability/ReadinessProbeHealthIndicatorTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/availability/ReadinessProbeHealthIndicatorTests.java index 7abfa279b5..9036839e32 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/availability/ReadinessProbeHealthIndicatorTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/availability/ReadinessProbeHealthIndicatorTests.java @@ -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); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ApplicationAvailabilityProvider.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ApplicationAvailabilityProvider.java index 5c36c2043c..4e9f9e403a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ApplicationAvailabilityProvider.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ApplicationAvailabilityProvider.java @@ -39,10 +39,10 @@ public class ApplicationAvailabilityProvider implements ApplicationListener @@ -28,59 +26,16 @@ 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 { - - /** - * The application is running and its internal state is correct. - */ - LIVE, +public enum LivenessState { - /** - * The internal state of the application is broken. - */ - BROKEN + /** + * The application is running and its internal state is correct. + */ + LIVE, - } + /** + * The internal state of the application is broken. + */ + BROKEN } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/LivenessStateChangedEvent.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/LivenessStateChangedEvent.java index 7d27a8a0f7..21d9ba32d5 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/LivenessStateChangedEvent.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/LivenessStateChangedEvent.java @@ -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()); } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ReadinessState.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ReadinessState.java index 5b2b2416fc..50cc39242d 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ReadinessState.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ReadinessState.java @@ -16,8 +16,6 @@ package org.springframework.boot.availability; -import java.util.Objects; - /** * "Readiness" state of the application. *

@@ -28,59 +26,16 @@ 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 { - - /** - * The application is not willing to receive traffic. - */ - UNREADY, +public enum ReadinessState { - /** - * The application is ready to receive traffic. - */ - READY + /** + * The application is not willing to receive traffic. + */ + UNREADY, - } + /** + * The application is ready to receive traffic. + */ + READY } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ReadinessStateChangedEvent.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ReadinessStateChangedEvent.java index d26d345415..c38b15db39 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ReadinessStateChangedEvent.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ReadinessStateChangedEvent.java @@ -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); } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/availability/ApplicationAvailabilityProviderTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/availability/ApplicationAvailabilityProviderTests.java index d2458f56cc..b035bf56f1 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/availability/ApplicationAvailabilityProviderTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/availability/ApplicationAvailabilityProviderTests.java @@ -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); } }