Skip to content

Commit e1db00b

Browse files
committed
Merge pull request #46877 from tommyk-gears
* pr/46877: Polish contribution Improve Hazelcast health indicator to check that Hazelcast is running Closes gh-46877
2 parents aa46a66 + 375b0b8 commit e1db00b

File tree

2 files changed

+56
-16
lines changed

2 files changed

+56
-16
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/hazelcast/HazelcastHealthIndicator.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*
2929
* @author Dmytro Nosan
3030
* @author Stephane Nicoll
31+
* @author Tommy Karlsson
3132
* @since 2.2.0
3233
*/
3334
public class HazelcastHealthIndicator extends AbstractHealthIndicator {
@@ -42,6 +43,10 @@ public HazelcastHealthIndicator(HazelcastInstance hazelcast) {
4243

4344
@Override
4445
protected void doHealthCheck(Health.Builder builder) {
46+
if (!this.hazelcast.getLifecycleService().isRunning()) {
47+
builder.down();
48+
return;
49+
}
4550
this.hazelcast.executeTransaction((context) -> {
4651
String uuid = this.hazelcast.getLocalEndpoint().getUuid().toString();
4752
builder.up().withDetail("name", this.hazelcast.getName()).withDetail("uuid", uuid);

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/hazelcast/HazelcastHealthIndicatorTests.java

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.hazelcast.core.HazelcastException;
2020
import com.hazelcast.core.HazelcastInstance;
21+
import com.hazelcast.core.LifecycleService;
2122
import org.junit.jupiter.api.Test;
2223

2324
import org.springframework.boot.actuate.health.Health;
@@ -30,32 +31,34 @@
3031
import static org.assertj.core.api.Assertions.assertThat;
3132
import static org.mockito.ArgumentMatchers.any;
3233
import static org.mockito.BDDMockito.given;
34+
import static org.mockito.BDDMockito.then;
3335
import static org.mockito.Mockito.mock;
3436

3537
/**
3638
* Tests for {@link HazelcastHealthIndicator}.
3739
*
3840
* @author Dmytro Nosan
3941
* @author Stephane Nicoll
42+
* @author Tommy Karlsson
4043
*/
44+
@WithResource(name = "hazelcast.xml", content = """
45+
<hazelcast xmlns="http://www.hazelcast.com/schema/config"
46+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
47+
xsi:schemaLocation="http://www.hazelcast.com/schema/config
48+
http://www.hazelcast.com/schema/config/hazelcast-config-5.0.xsd">
49+
<instance-name>actuator-hazelcast</instance-name>
50+
<map name="defaultCache" />
51+
<network>
52+
<join>
53+
<auto-detection enabled="false"/>
54+
<multicast enabled="false"/>
55+
</join>
56+
</network>
57+
</hazelcast>
58+
""")
4159
class HazelcastHealthIndicatorTests {
4260

4361
@Test
44-
@WithResource(name = "hazelcast.xml", content = """
45-
<hazelcast xmlns="http://www.hazelcast.com/schema/config"
46-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
47-
xsi:schemaLocation="http://www.hazelcast.com/schema/config
48-
http://www.hazelcast.com/schema/config/hazelcast-config-5.0.xsd">
49-
<instance-name>actuator-hazelcast</instance-name>
50-
<map name="defaultCache" />
51-
<network>
52-
<join>
53-
<auto-detection enabled="false"/>
54-
<multicast enabled="false"/>
55-
</join>
56-
</network>
57-
</hazelcast>
58-
""")
5962
void hazelcastUp() {
6063
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class))
6164
.withPropertyValues("spring.hazelcast.config=hazelcast.xml")
@@ -69,12 +72,44 @@ void hazelcastUp() {
6972
});
7073
}
7174

75+
@Test
76+
void hazelcastShutdown() {
77+
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class))
78+
.withPropertyValues("spring.hazelcast.config=hazelcast.xml")
79+
.run((context) -> {
80+
HazelcastInstance hazelcast = context.getBean(HazelcastInstance.class);
81+
hazelcast.shutdown();
82+
Health health = new HazelcastHealthIndicator(hazelcast).health();
83+
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
84+
});
85+
}
86+
87+
@Test
88+
void hazelcastLifecycleNotRunning() {
89+
HazelcastInstance hazelcast = mockHazelcastInstance(false);
90+
Health health = new HazelcastHealthIndicator(hazelcast).health();
91+
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
92+
then(hazelcast).should().getLifecycleService();
93+
then(hazelcast).shouldHaveNoMoreInteractions();
94+
}
95+
7296
@Test
7397
void hazelcastDown() {
74-
HazelcastInstance hazelcast = mock(HazelcastInstance.class);
98+
HazelcastInstance hazelcast = mockHazelcastInstance(true);
7599
given(hazelcast.executeTransaction(any())).willThrow(new HazelcastException());
76100
Health health = new HazelcastHealthIndicator(hazelcast).health();
77101
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
102+
then(hazelcast).should().getLifecycleService();
103+
then(hazelcast).should().executeTransaction(any());
104+
then(hazelcast).shouldHaveNoMoreInteractions();
105+
}
106+
107+
private static HazelcastInstance mockHazelcastInstance(boolean isRunning) {
108+
LifecycleService lifecycleService = mock(LifecycleService.class);
109+
given(lifecycleService.isRunning()).willReturn(isRunning);
110+
HazelcastInstance hazelcastInstance = mock(HazelcastInstance.class);
111+
given(hazelcastInstance.getLifecycleService()).willReturn(lifecycleService);
112+
return hazelcastInstance;
78113
}
79114

80115
}

0 commit comments

Comments
 (0)