Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*
* @author Dmytro Nosan
* @author Stephane Nicoll
* @author Tommy Karlsson
* @since 4.0.0
*/
public class HazelcastHealthIndicator extends AbstractHealthIndicator {
Expand All @@ -42,6 +43,10 @@ public HazelcastHealthIndicator(HazelcastInstance hazelcast) {

@Override
protected void doHealthCheck(Health.Builder builder) {
if (!this.hazelcast.getLifecycleService().isRunning()) {
builder.down();
return;
}
this.hazelcast.executeTransaction((context) -> {
String uuid = this.hazelcast.getLocalEndpoint().getUuid().toString();
builder.up().withDetail("name", this.hazelcast.getName()).withDetail("uuid", uuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import com.hazelcast.core.HazelcastException;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.instance.impl.HazelcastInstanceProxy;
import com.hazelcast.instance.impl.OutOfMemoryHandlerHelper;
import org.junit.jupiter.api.Test;

import org.springframework.boot.autoconfigure.AutoConfigurations;
Expand All @@ -37,25 +39,26 @@
*
* @author Dmytro Nosan
* @author Stephane Nicoll
* @author Tommy Karlsson
*/
@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>
""")
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")
Expand All @@ -69,6 +72,32 @@ void hazelcastUp() {
});
}

@Test
void hazelcastShutdown() {
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class))
.withPropertyValues("spring.hazelcast.config=hazelcast.xml")
.run((context) -> {
HazelcastInstance hazelcast = context.getBean(HazelcastInstance.class);
hazelcast.shutdown();
Health health = new HazelcastHealthIndicator(hazelcast).health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
});
}

@Test
void hazelcastOOMShutdown() {
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(HazelcastAutoConfiguration.class))
.withPropertyValues("spring.hazelcast.config=hazelcast.xml")
.run((context) -> {
HazelcastInstance hazelcast = context.getBean(HazelcastInstance.class);
HazelcastInstance original = ((HazelcastInstanceProxy) hazelcast).getOriginal();
OutOfMemoryHandlerHelper.tryCloseConnections(original);
OutOfMemoryHandlerHelper.tryShutdown(original);
Health health = new HazelcastHealthIndicator(hazelcast).health();
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
});
}

@Test
void hazelcastDown() {
HazelcastInstance hazelcast = mock(HazelcastInstance.class);
Expand Down
Loading