-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHealthCheck.java
39 lines (34 loc) · 1.27 KB
/
HealthCheck.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.stream.IntStream;
public class HealthCheck {
public static void main(String[] args) throws InterruptedException, IOException {
var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:8080/actuator/health"))
.header("accept", "application/json")
.build();
var response = client.send(request, BodyHandlers.ofString());
var ok = response.statusCode() == 200 && checkStatus(response.body());
System.out.println(ok);
if (!ok) {
throw new RuntimeException("Healthcheck failed");
}
}
private static boolean checkStatus(final String responseBody) {
final var key = "status";
final var startOffset = key.length() + 3;
final var stopOffset = key.length() + 5;
return IntStream
.iterate(
responseBody.indexOf(key),
index -> index >= 0,
index -> responseBody.indexOf(key, index + 1)
).boxed()
.map(index -> responseBody.substring(index + startOffset, index + stopOffset))
.allMatch(s -> s.equalsIgnoreCase("UP"));
}
}