Move code from spring-boot-actuator to spring-boot-hazelcast

This commit is contained in:
Andy Wilkinson
2025-05-09 15:51:01 +01:00
committed by Phillip Webb
parent b7588f3ebd
commit 108ee378ff
8 changed files with 14 additions and 12 deletions

View File

@@ -41,7 +41,6 @@ dependencies {
optional("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
optional("com.github.ben-manes.caffeine:caffeine")
optional("com.google.code.findbugs:jsr305")
optional("com.hazelcast:hazelcast-spring")
optional("com.zaxxer:HikariCP")
optional("io.lettuce:lettuce-core")
optional("io.micrometer:micrometer-observation")
@@ -95,7 +94,6 @@ dependencies {
testFixturesImplementation(project(":spring-boot-project:spring-boot-webmvc"))
testImplementation(project(":spring-boot-project:spring-boot-autoconfigure"))
testImplementation(project(":spring-boot-project:spring-boot-hazelcast"))
testImplementation(project(":spring-boot-project:spring-boot-jackson"))
testImplementation(project(":spring-boot-project:spring-boot-jersey"))
testImplementation(project(":spring-boot-project:spring-boot-jsonb"))

View File

@@ -1,52 +0,0 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.hazelcast;
import com.hazelcast.core.HazelcastInstance;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.util.Assert;
/**
* {@link HealthIndicator} for Hazelcast.
*
* @author Dmytro Nosan
* @author Stephane Nicoll
* @since 2.2.0
*/
public class HazelcastHealthIndicator extends AbstractHealthIndicator {
private final HazelcastInstance hazelcast;
public HazelcastHealthIndicator(HazelcastInstance hazelcast) {
super("Hazelcast health check failed");
Assert.notNull(hazelcast, "'hazelcast' must not be null");
this.hazelcast = hazelcast;
}
@Override
protected void doHealthCheck(Health.Builder builder) {
this.hazelcast.executeTransaction((context) -> {
String uuid = this.hazelcast.getLocalEndpoint().getUuid().toString();
builder.up().withDetail("name", this.hazelcast.getName()).withDetail("uuid", uuid);
return null;
});
}
}

View File

@@ -1,20 +0,0 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Actuator support for Hazelcast.
*/
package org.springframework.boot.actuate.hazelcast;

View File

@@ -1,80 +0,0 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.hazelcast;
import com.hazelcast.core.HazelcastException;
import com.hazelcast.core.HazelcastInstance;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Status;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.hazelcast.autoconfigure.HazelcastAutoConfiguration;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.testsupport.classpath.resources.WithResource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link HazelcastHealthIndicator}.
*
* @author Dmytro Nosan
* @author Stephane Nicoll
*/
class HazelcastHealthIndicatorTests {
@Test
@WithResource(name = "hazelcast.xml", content = """
<hazelcast xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hazelcast.com/schema/config
http://www.hazelcast.com/schema/config/hazelcast-config-5.0.xsd">
<instance-name>actuator-hazelcast</instance-name>
<map name="defaultCache" />
<network>
<join>
<auto-detection enabled="false"/>
<multicast enabled="false"/>
</join>
</network>
</hazelcast>
""")
void hazelcastUp() {
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class))
.withPropertyValues("spring.hazelcast.config=hazelcast.xml")
.run((context) -> {
HazelcastInstance hazelcast = context.getBean(HazelcastInstance.class);
Health health = new HazelcastHealthIndicator(hazelcast).health();
assertThat(health.getStatus()).isEqualTo(Status.UP);
assertThat(health.getDetails()).containsOnlyKeys("name", "uuid")
.containsEntry("name", "actuator-hazelcast");
assertThat(health.getDetails().get("uuid")).asString().isNotEmpty();
});
}
@Test
void hazelcastDown() {
HazelcastInstance hazelcast = mock(HazelcastInstance.class);
given(hazelcast.executeTransaction(any())).willThrow(new HazelcastException());
Health health = new HazelcastHealthIndicator(hazelcast).health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
}
}