Skip to content

Commit eb70fd9

Browse files
committed
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
1 parent 6aa3461 commit eb70fd9

File tree

10 files changed

+35
-125
lines changed

10 files changed

+35
-125
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/availability/LivenessProbeHealthIndicator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public LivenessProbeHealthIndicator(ApplicationAvailabilityProvider applicationA
3838

3939
@Override
4040
protected void doHealthCheck(Health.Builder builder) throws Exception {
41-
if (LivenessState.live().equals(this.applicationAvailabilityProvider.getLivenessState())) {
41+
if (LivenessState.LIVE.equals(this.applicationAvailabilityProvider.getLivenessState())) {
4242
builder.up();
4343
}
4444
else {

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/availability/ReadinessProbeHealthIndicator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public ReadinessProbeHealthIndicator(ApplicationAvailabilityProvider application
3838

3939
@Override
4040
protected void doHealthCheck(Health.Builder builder) throws Exception {
41-
if (ReadinessState.ready().equals(this.applicationAvailabilityProvider.getReadinessState())) {
41+
if (ReadinessState.READY.equals(this.applicationAvailabilityProvider.getReadinessState())) {
4242
builder.up();
4343
}
4444
else {

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/availability/LivenessProbeHealthIndicatorTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ void setUp() {
4646

4747
@Test
4848
void livenessIsLive() {
49-
when(this.stateProvider.getLivenessState()).thenReturn(LivenessState.live());
49+
when(this.stateProvider.getLivenessState()).thenReturn(LivenessState.LIVE);
5050
assertThat(this.healthIndicator.health().getStatus()).isEqualTo(Status.UP);
5151
}
5252

5353
@Test
5454
void livenessIsBroken() {
55-
when(this.stateProvider.getLivenessState()).thenReturn(LivenessState.broken());
55+
when(this.stateProvider.getLivenessState()).thenReturn(LivenessState.BROKEN);
5656
assertThat(this.healthIndicator.health().getStatus()).isEqualTo(Status.DOWN);
5757
}
5858

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/availability/ReadinessProbeHealthIndicatorTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ void setUp() {
4646

4747
@Test
4848
void readinessIsReady() {
49-
when(this.stateProvider.getReadinessState()).thenReturn(ReadinessState.ready());
49+
when(this.stateProvider.getReadinessState()).thenReturn(ReadinessState.READY);
5050
assertThat(this.healthIndicator.health().getStatus()).isEqualTo(Status.UP);
5151
}
5252

5353
@Test
5454
void readinessIsUnready() {
55-
when(this.stateProvider.getReadinessState()).thenReturn(ReadinessState.unready());
55+
when(this.stateProvider.getReadinessState()).thenReturn(ReadinessState.UNREADY);
5656
assertThat(this.healthIndicator.health().getStatus()).isEqualTo(Status.OUT_OF_SERVICE);
5757
}
5858

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ApplicationAvailabilityProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public class ApplicationAvailabilityProvider implements ApplicationListener<Appl
3939

4040
/**
4141
* Create a new {@link ApplicationAvailabilityProvider} instance with
42-
* {@link LivenessState#broken()} and {@link ReadinessState#unready()}.
42+
* {@link LivenessState#BROKEN} and {@link ReadinessState#UNREADY}.
4343
*/
4444
public ApplicationAvailabilityProvider() {
45-
this(LivenessState.broken(), ReadinessState.unready());
45+
this(LivenessState.BROKEN, ReadinessState.UNREADY);
4646
}
4747

4848
/**

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/LivenessState.java

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.availability;
1818

19-
import java.util.Objects;
20-
2119
/**
2220
* "Liveness" state of the application.
2321
* <p>
@@ -28,59 +26,16 @@
2826
* @author Brian Clozel
2927
* @since 2.3.0
3028
*/
31-
public final class LivenessState {
32-
33-
private final Status status;
34-
35-
LivenessState(Status status) {
36-
this.status = status;
37-
}
38-
39-
public Status getStatus() {
40-
return this.status;
41-
}
42-
43-
@Override
44-
public boolean equals(Object obj) {
45-
if (this == obj) {
46-
return true;
47-
}
48-
if (obj == null || getClass() != obj.getClass()) {
49-
return false;
50-
}
51-
return this.status == ((LivenessState) obj).status;
52-
}
53-
54-
@Override
55-
public int hashCode() {
56-
return Objects.hash(this.status);
57-
}
58-
59-
@Override
60-
public String toString() {
61-
return "LivenessState{" + "status=" + this.status + '}';
62-
}
63-
64-
public static LivenessState broken() {
65-
return new LivenessState(Status.BROKEN);
66-
}
67-
68-
public static LivenessState live() {
69-
return new LivenessState(Status.LIVE);
70-
}
71-
72-
public enum Status {
73-
74-
/**
75-
* The application is running and its internal state is correct.
76-
*/
77-
LIVE,
29+
public enum LivenessState {
7830

79-
/**
80-
* The internal state of the application is broken.
81-
*/
82-
BROKEN
31+
/**
32+
* The application is running and its internal state is correct.
33+
*/
34+
LIVE,
8335

84-
}
36+
/**
37+
* The internal state of the application is broken.
38+
*/
39+
BROKEN
8540

8641
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/LivenessStateChangedEvent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public LivenessState getLivenessState() {
4747
* @return the application event
4848
*/
4949
public static LivenessStateChangedEvent live(String cause) {
50-
return new LivenessStateChangedEvent(LivenessState.live(), cause);
50+
return new LivenessStateChangedEvent(LivenessState.LIVE, cause);
5151
}
5252

5353
/**
@@ -57,7 +57,7 @@ public static LivenessStateChangedEvent live(String cause) {
5757
* @return the application event
5858
*/
5959
public static LivenessStateChangedEvent broken(String cause) {
60-
return new LivenessStateChangedEvent(LivenessState.broken(), cause);
60+
return new LivenessStateChangedEvent(LivenessState.BROKEN, cause);
6161
}
6262

6363
/**
@@ -68,7 +68,7 @@ public static LivenessStateChangedEvent broken(String cause) {
6868
* @return the application event
6969
*/
7070
public static LivenessStateChangedEvent broken(Throwable throwable) {
71-
return new LivenessStateChangedEvent(LivenessState.broken(), throwable.getMessage());
71+
return new LivenessStateChangedEvent(LivenessState.BROKEN, throwable.getMessage());
7272
}
7373

7474
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ReadinessState.java

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.availability;
1818

19-
import java.util.Objects;
20-
2119
/**
2220
* "Readiness" state of the application.
2321
* <p>
@@ -28,59 +26,16 @@
2826
* @author Brian Clozel
2927
* @since 2.3.0
3028
*/
31-
public final class ReadinessState {
32-
33-
private final Availability availability;
34-
35-
private ReadinessState(Availability availability) {
36-
this.availability = availability;
37-
}
38-
39-
public Availability getAvailability() {
40-
return this.availability;
41-
}
42-
43-
@Override
44-
public boolean equals(Object obj) {
45-
if (this == obj) {
46-
return true;
47-
}
48-
if (obj == null || getClass() != obj.getClass()) {
49-
return false;
50-
}
51-
return this.availability == ((ReadinessState) obj).availability;
52-
}
53-
54-
@Override
55-
public int hashCode() {
56-
return Objects.hash(this.availability);
57-
}
58-
59-
@Override
60-
public String toString() {
61-
return "ReadinessState{" + "availability=" + this.availability + '}';
62-
}
63-
64-
public static ReadinessState ready() {
65-
return new ReadinessState(Availability.READY);
66-
}
67-
68-
public static ReadinessState unready() {
69-
return new ReadinessState(Availability.UNREADY);
70-
}
71-
72-
public enum Availability {
73-
74-
/**
75-
* The application is not willing to receive traffic.
76-
*/
77-
UNREADY,
29+
public enum ReadinessState {
7830

79-
/**
80-
* The application is ready to receive traffic.
81-
*/
82-
READY
31+
/**
32+
* The application is not willing to receive traffic.
33+
*/
34+
UNREADY,
8335

84-
}
36+
/**
37+
* The application is ready to receive traffic.
38+
*/
39+
READY
8540

8641
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/availability/ReadinessStateChangedEvent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public ReadinessState getReadinessState() {
4343
* @return the application event
4444
*/
4545
public static ReadinessStateChangedEvent ready() {
46-
return new ReadinessStateChangedEvent(ReadinessState.ready());
46+
return new ReadinessStateChangedEvent(ReadinessState.READY);
4747
}
4848

4949
/**
@@ -52,7 +52,7 @@ public static ReadinessStateChangedEvent ready() {
5252
* @return the application event
5353
*/
5454
public static ReadinessStateChangedEvent unready() {
55-
return new ReadinessStateChangedEvent(ReadinessState.unready());
55+
return new ReadinessStateChangedEvent(ReadinessState.UNREADY);
5656
}
5757

5858
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/availability/ApplicationAvailabilityProviderTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ class ApplicationAvailabilityProviderTests {
3030
@Test
3131
void initialStateShouldBeFailures() {
3232
ApplicationAvailabilityProvider stateProvider = new ApplicationAvailabilityProvider();
33-
assertThat(stateProvider.getLivenessState()).isEqualTo(LivenessState.broken());
34-
assertThat(stateProvider.getReadinessState()).isEqualTo(ReadinessState.unready());
33+
assertThat(stateProvider.getLivenessState()).isEqualTo(LivenessState.BROKEN);
34+
assertThat(stateProvider.getReadinessState()).isEqualTo(ReadinessState.UNREADY);
3535
}
3636

3737
@Test
3838
void updateLivenessState() {
3939
ApplicationAvailabilityProvider stateProvider = new ApplicationAvailabilityProvider();
40-
LivenessState livenessState = LivenessState.live();
40+
LivenessState livenessState = LivenessState.LIVE;
4141
stateProvider.onApplicationEvent(new LivenessStateChangedEvent(livenessState, "Startup complete"));
4242
assertThat(stateProvider.getLivenessState()).isEqualTo(livenessState);
4343
}
@@ -46,7 +46,7 @@ void updateLivenessState() {
4646
void updateReadiessState() {
4747
ApplicationAvailabilityProvider stateProvider = new ApplicationAvailabilityProvider();
4848
stateProvider.onApplicationEvent(ReadinessStateChangedEvent.ready());
49-
assertThat(stateProvider.getReadinessState()).isEqualTo(ReadinessState.ready());
49+
assertThat(stateProvider.getReadinessState()).isEqualTo(ReadinessState.READY);
5050
}
5151

5252
}

0 commit comments

Comments
 (0)