health.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. {% macro health_check(scheme) -%}
  2. public class HealthCheck {
  3. public static void main(String[] args) {
  4. try {
  5. String healthUrl = "{{ scheme }}://localhost:9000/health";
  6. java.net.HttpURLConnection conn = (java.net.HttpURLConnection)
  7. java.net.URI.create(healthUrl).toURL().openConnection();
  8. // For HTTPS, disable SSL verification (health check only)
  9. if (conn instanceof javax.net.ssl.HttpsURLConnection) {
  10. javax.net.ssl.HttpsURLConnection httpsConn = (javax.net.ssl.HttpsURLConnection) conn;
  11. // Trust all certificates
  12. javax.net.ssl.TrustManager[] trustAll = new javax.net.ssl.TrustManager[] {
  13. new javax.net.ssl.X509TrustManager() {
  14. public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; }
  15. public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) { }
  16. public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) { }
  17. }
  18. };
  19. javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
  20. sc.init(null, trustAll, new java.security.SecureRandom());
  21. httpsConn.setSSLSocketFactory(sc.getSocketFactory());
  22. // Disable hostname verification
  23. httpsConn.setHostnameVerifier((hostname, session) -> true);
  24. }
  25. int responseCode = conn.getResponseCode();
  26. System.exit(responseCode == java.net.HttpURLConnection.HTTP_OK ? 0 : 1);
  27. } catch (Exception e) {
  28. // Any exception means unhealthy
  29. System.exit(1);
  30. }
  31. }
  32. }
  33. {%- endmacro %}