Skip to content

Instantly share code, notes, and snippets.

@ex0b1t
Created December 10, 2022 20:30
Show Gist options
  • Save ex0b1t/cd6613f762ec117b115a4482b9d36554 to your computer and use it in GitHub Desktop.
Save ex0b1t/cd6613f762ec117b115a4482b9d36554 to your computer and use it in GitHub Desktop.
Actuator Health Check for Docker Compose
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;
public class ActuatorHealth {
public static void main(String[] args) throws InterruptedException, IOException {
String url = (args != null && args.length > 0) ? args[0].trim() : "http://localhost:8080/actuator/health/readiness";
System.out.println("Health: " + url);
var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
.uri(URI.create(url))
.header("accept", "application/json")
.build();
var response = client.send(request, BodyHandlers.ofString());
if (response.statusCode() != 200 || !response.body().contains("UP")) {
throw new RuntimeException("Healthcheck failed");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment