Skip to content

Commit be988d7

Browse files
committed
Polish "Add HealthIndicator for Hazelcast"
See gh-17499
1 parent fca5a2b commit be988d7

File tree

7 files changed

+21
-46
lines changed

7 files changed

+21
-46
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,6 @@
6464
<artifactId>hazelcast-spring</artifactId>
6565
<optional>true</optional>
6666
</dependency>
67-
<dependency>
68-
<groupId>com.hazelcast</groupId>
69-
<artifactId>hazelcast-client</artifactId>
70-
<scope>test</scope>
71-
</dependency>
7267
<dependency>
7368
<groupId>com.sun.mail</groupId>
7469
<artifactId>jakarta.mail</artifactId>

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/hazelcast/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
*/
1616

1717
/**
18-
* Auto-configuration for Hazelcast's actuator.
18+
* Auto-configuration for actuator Hazelcast concerns.
1919
*/
2020
package org.springframework.boot.actuate.autoconfigure.hazelcast;

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

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.hazelcast;
1818

19-
import com.hazelcast.client.config.ClientConfig;
20-
import com.hazelcast.config.Config;
21-
import com.hazelcast.core.Hazelcast;
2219
import com.hazelcast.core.HazelcastInstance;
23-
import org.junit.jupiter.api.AfterEach;
2420
import org.junit.jupiter.api.Test;
2521

2622
import org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAutoConfiguration;
@@ -40,17 +36,10 @@
4036
*/
4137
class HazelcastHealthIndicatorAutoConfigurationIntegrationTests {
4238

43-
private final HazelcastInstance hazelcastServer = Hazelcast.newHazelcastInstance(new Config());
44-
45-
private ApplicationContextRunner contextRunner = new ApplicationContextRunner().withBean(ClientConfig.class)
39+
private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
4640
.withConfiguration(AutoConfigurations.of(HazelcastHealthIndicatorAutoConfiguration.class,
4741
HazelcastAutoConfiguration.class, HealthIndicatorAutoConfiguration.class));
4842

49-
@AfterEach
50-
void shutdown() {
51-
this.hazelcastServer.shutdown();
52-
}
53-
5443
@Test
5544
void hazelcastUp() {
5645
this.contextRunner.run((context) -> {
@@ -66,7 +55,7 @@ void hazelcastUp() {
6655
@Test
6756
void hazelcastDown() {
6857
this.contextRunner.run((context) -> {
69-
shutdown();
58+
context.getBean(HazelcastInstance.class).shutdown();
7059
assertThat(context).hasSingleBean(HazelcastHealthIndicator.class);
7160
Health health = context.getBean(HazelcastHealthIndicator.class).health();
7261
assertThat(health.getStatus()).isEqualTo(Status.DOWN);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ void runShouldCreateIndicator() {
4848
void runWhenDisabledShouldNotCreateIndicator() {
4949
this.contextRunner.withPropertyValues("management.health.hazelcast.enabled:false")
5050
.run((context) -> assertThat(context).doesNotHaveBean(HazelcastHealthIndicator.class)
51-
.doesNotHaveBean(HazelcastHealthIndicator.class)
5251
.hasSingleBean(ApplicationHealthIndicator.class));
5352
}
5453

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

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,22 @@
1616

1717
package org.springframework.boot.actuate.hazelcast;
1818

19-
import java.util.LinkedHashMap;
20-
import java.util.Map;
21-
22-
import com.hazelcast.core.Endpoint;
2319
import com.hazelcast.core.HazelcastInstance;
24-
import com.hazelcast.transaction.TransactionalTask;
2520

2621
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
2722
import org.springframework.boot.actuate.health.Health;
2823
import org.springframework.boot.actuate.health.HealthIndicator;
2924
import org.springframework.util.Assert;
3025

3126
/**
32-
* {@link HealthIndicator} for a Hazelcast.
27+
* {@link HealthIndicator} for Hazelcast.
3328
*
3429
* @author Dmytro Nosan
30+
* @author Stephane Nicoll
3531
* @since 2.2.0
3632
*/
3733
public class HazelcastHealthIndicator extends AbstractHealthIndicator {
3834

39-
private static final TransactionalTask<?> TASK = (context) -> null;
40-
4135
private final HazelcastInstance hazelcast;
4236

4337
public HazelcastHealthIndicator(HazelcastInstance hazelcast) {
@@ -48,16 +42,11 @@ public HazelcastHealthIndicator(HazelcastInstance hazelcast) {
4842

4943
@Override
5044
protected void doHealthCheck(Health.Builder builder) {
51-
this.hazelcast.executeTransaction(TASK);
52-
builder.up().withDetails(getDetails());
53-
}
54-
55-
private Map<String, Object> getDetails() {
56-
Map<String, Object> details = new LinkedHashMap<>();
57-
Endpoint endpoint = this.hazelcast.getLocalEndpoint();
58-
details.put("name", this.hazelcast.getName());
59-
details.put("uuid", endpoint.getUuid());
60-
return details;
45+
this.hazelcast.executeTransaction((context) -> {
46+
builder.up().withDetail("name", this.hazelcast.getName()).withDetail("uuid",
47+
this.hazelcast.getLocalEndpoint().getUuid());
48+
return null;
49+
});
6150
}
6251

6352
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.hazelcast.core.Endpoint;
2020
import com.hazelcast.core.HazelcastException;
2121
import com.hazelcast.core.HazelcastInstance;
22+
import com.hazelcast.transaction.TransactionalTask;
2223
import org.junit.jupiter.api.Test;
2324

2425
import org.springframework.boot.actuate.health.Health;
@@ -33,22 +34,23 @@
3334
* Tests for {@link HazelcastHealthIndicator}.
3435
*
3536
* @author Dmytro Nosan
37+
* @author Stephane Nicoll
3638
*/
3739
class HazelcastHealthIndicatorTests {
3840

3941
private final HazelcastInstance hazelcast = mock(HazelcastInstance.class);
4042

41-
private final HazelcastHealthIndicator healthIndicator = new HazelcastHealthIndicator(this.hazelcast);
42-
4343
@Test
4444
void hazelcastUp() {
4545
Endpoint endpoint = mock(Endpoint.class);
4646
when(this.hazelcast.getName()).thenReturn("hz0-instance");
4747
when(this.hazelcast.getLocalEndpoint()).thenReturn(endpoint);
4848
when(endpoint.getUuid()).thenReturn("7581bb2f-879f-413f-b574-0071d7519eb0");
49-
50-
Health health = this.healthIndicator.health();
51-
49+
when(this.hazelcast.executeTransaction(any())).thenAnswer((invocation) -> {
50+
TransactionalTask<?> task = invocation.getArgument(0);
51+
return task.execute(null);
52+
});
53+
Health health = new HazelcastHealthIndicator(this.hazelcast).health();
5254
assertThat(health.getStatus()).isEqualTo(Status.UP);
5355
assertThat(health.getDetails()).containsOnlyKeys("name", "uuid").containsEntry("name", "hz0-instance")
5456
.containsEntry("uuid", "7581bb2f-879f-413f-b574-0071d7519eb0");
@@ -57,9 +59,7 @@ void hazelcastUp() {
5759
@Test
5860
void hazelcastDown() {
5961
when(this.hazelcast.executeTransaction(any())).thenThrow(new HazelcastException());
60-
61-
Health health = this.healthIndicator.health();
62-
62+
Health health = new HazelcastHealthIndicator(this.hazelcast).health();
6363
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
6464
}
6565

spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,9 @@ The following `HealthIndicators` are auto-configured by Spring Boot when appropr
764764
|{sc-spring-boot-actuator}/elasticsearch/ElasticsearchHealthIndicator.{sc-ext}[`ElasticsearchHealthIndicator`]
765765
|Checks that an Elasticsearch cluster is up.
766766

767+
|{sc-spring-boot-actuator}/hazelcast/HazelcastHealthIndicator.{sc-ext}[`HazelcastHealthIndicator`]
768+
|Checks that an Hazelcast server is up.
769+
767770
|{sc-spring-boot-actuator}/influx/InfluxDbHealthIndicator.{sc-ext}[`InfluxDbHealthIndicator`]
768771
|Checks that an InfluxDB server is up.
769772

0 commit comments

Comments
 (0)