Created
May 1, 2021 15:39
-
-
Save omidp/506c209a2fb016e7f05c40fa84dac616 to your computer and use it in GitHub Desktop.
springboothazelcast4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.stereotype.Component; | |
import org.springframework.util.Assert; | |
/** | |
* {@link HealthIndicator} for Hazelcast. | |
* | |
*/ | |
@Component | |
public class HazelcastHealthIndicator extends AbstractHealthIndicator { | |
private final HazelcastInstance hazelcast; | |
public HazelcastHealthIndicator(HazelcastInstance hazelcast) { | |
super("Hazelcast health check failed"); | |
Assert.notNull(hazelcast, "HazelcastInstance must not be null"); | |
this.hazelcast = hazelcast; | |
} | |
@Override | |
protected void doHealthCheck(Health.Builder builder) { | |
this.hazelcast.executeTransaction((context) -> { | |
builder.up().withDetail("name", this.hazelcast.getName()).withDetail("uuid", | |
this.hazelcast.getLocalEndpoint().getUuid()); | |
return null; | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://github.com/snicoll/spring-boot/commit/2a1e364cc175d513d77c86d877fea834f5189cbd | |
@Override | |
protected void doHealthCheck(Health.Builder builder) { | |
this.hazelcast.executeTransaction((context) -> { | |
builder.up().withDetail("name", this.hazelcast.getName()).withDetail("uuid", | |
this.hazelcast.getLocalEndpoint().getUuid()); | |
builder.up().withDetail("name", this.hazelcast.getName()).withDetail("uuid", extractUuid()); | |
return null; | |
}); | |
} | |
private String extractUuid() { | |
try { | |
return this.hazelcast.getLocalEndpoint().getUuid(); | |
} | |
catch (NoSuchMethodError ex) { | |
// Hazelcast 4 | |
Method endpointAccessor = ReflectionUtils.findMethod(HazelcastInstance.class, "getLocalEndpoint"); | |
Object endpoint = ReflectionUtils.invokeMethod(endpointAccessor, this.hazelcast); | |
Method uuidAccessor = ReflectionUtils.findMethod(endpoint.getClass(), "getUuid"); | |
return ((UUID) ReflectionUtils.invokeMethod(uuidAccessor, endpoint)).toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment