diff --git a/build.gradle b/build.gradle index 7ce554b..3b1dfd7 100644 --- a/build.gradle +++ b/build.gradle @@ -53,6 +53,4 @@ dependencies { implementation group: 'ch.qos.logback', name: 'logback-classic', version: '1.4.6' implementation group: 'ch.qos.logback.contrib', name: 'logback-json-classic', version: '0.1.5' implementation group: 'ch.qos.logback.contrib', name: 'logback-jackson', version: '0.1.5' - - implementation group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.12' } diff --git a/src/main/java/fi/hsl/suomenlinna_hfp/Main.java b/src/main/java/fi/hsl/suomenlinna_hfp/Main.java index 2707e7b..3d0a0b1 100644 --- a/src/main/java/fi/hsl/suomenlinna_hfp/Main.java +++ b/src/main/java/fi/hsl/suomenlinna_hfp/Main.java @@ -107,11 +107,12 @@ public static void main(String[] args) throws Throwable { } if (config.getBoolean("health.enabled")) { + HealthNotificationService healthNotificationService = null; if (!config.getString("health.postEndpoint").equals("")) { - createHealthServerWithNotification(vehiclePositionProvider, mqttHfpPublisher, configType != ConfigType.SBDRIVE, config.getString("health.postEndpoint")); - } else { - createHealthServerWithoutNotification(vehiclePositionProvider, mqttHfpPublisher, configType != ConfigType.SBDRIVE); + healthNotificationService = new HealthNotificationService(config.getString("health.postEndpoint"), httpClient); } + + createHealthServer(vehiclePositionProvider, mqttHfpPublisher, configType != ConfigType.SBDRIVE, healthNotificationService); } new HfpProducer(transportMode, vehicleIdMap, tripProcessor, gtfsProvider, vehiclePositionProvider, passengerCountProvider, mqttHfpPublisher).run(); @@ -135,16 +136,8 @@ public static ConfigType getByName(String name) { } } - private static void createHealthServerWithNotification(VehiclePositionProvider vehiclePositionProvider, MqttHfpPublisher mqttHfpPublisher, boolean publisherHealthCheck, String postEndpoint) throws IOException { - HealthServer healthServer = new HealthServer(8080, new HealthNotificationService(postEndpoint)); - healthServer.addCheck(() -> System.nanoTime() - vehiclePositionProvider.getLastReceivedTime() < Duration.of(10, ChronoUnit.MINUTES).toNanos()); - if (publisherHealthCheck) { - healthServer.addCheck(() -> System.nanoTime() - mqttHfpPublisher.getLastSentTime() < Duration.of(10, ChronoUnit.MINUTES).toNanos()); - } - } - - private static void createHealthServerWithoutNotification(VehiclePositionProvider vehiclePositionProvider, MqttHfpPublisher mqttHfpPublisher, boolean publisherHealthCheck) throws IOException { - HealthServer healthServer = new HealthServer(8080); + private static void createHealthServer(VehiclePositionProvider vehiclePositionProvider, MqttHfpPublisher mqttHfpPublisher, boolean publisherHealthCheck, HealthNotificationService healthNotificationService) throws IOException { + HealthServer healthServer = healthNotificationService == null ? new HealthServer(8080) : new HealthServer(8080, healthNotificationService); healthServer.addCheck(() -> System.nanoTime() - vehiclePositionProvider.getLastReceivedTime() < Duration.of(10, ChronoUnit.MINUTES).toNanos()); if (publisherHealthCheck) { healthServer.addCheck(() -> System.nanoTime() - mqttHfpPublisher.getLastSentTime() < Duration.of(10, ChronoUnit.MINUTES).toNanos()); diff --git a/src/main/java/fi/hsl/suomenlinna_hfp/health/HealthNotificationService.java b/src/main/java/fi/hsl/suomenlinna_hfp/health/HealthNotificationService.java index 306141c..7cccf7f 100644 --- a/src/main/java/fi/hsl/suomenlinna_hfp/health/HealthNotificationService.java +++ b/src/main/java/fi/hsl/suomenlinna_hfp/health/HealthNotificationService.java @@ -1,32 +1,34 @@ package fi.hsl.suomenlinna_hfp.health; -import org.apache.http.*; -import org.apache.http.client.methods.*; -import org.apache.http.entity.*; -import org.apache.http.impl.client.*; - -import java.io.*; +import java.io.IOException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.nio.charset.StandardCharsets; public class HealthNotificationService { - - private final String postEndpoint; - private final CloseableHttpClient apacheDefaultClient; - public HealthNotificationService(String postEndpoint) { + private final HttpClient httpClient; + + public HealthNotificationService(String postEndpoint, HttpClient httpClient) { this.postEndpoint = postEndpoint; - apacheDefaultClient = HttpClients.createDefault(); + this.httpClient = httpClient; } void notifySlackChannel() throws IOException { - HttpPost httpPost = new HttpPost(postEndpoint); - httpPost.setHeader("Accept", "application/json"); - httpPost.setHeader("Content-Type", "application/json"); - - String inputJson = "{\"text\": \"Suomenlinnan lautoissa ongelmia!\"}"; - StringEntity stringEntity = new StringEntity(inputJson); - httpPost.setEntity(stringEntity); - HttpResponse response = apacheDefaultClient.execute(httpPost); - + final String message = "{\"text\": \"Suomenlinnan lautoissa ongelmia!\"}"; + + final HttpRequest request = HttpRequest.newBuilder() + .POST(HttpRequest.BodyPublishers.ofString(message, StandardCharsets.UTF_8)) + .setHeader("Accept", "application/json") + .setHeader("Content-Type", "application/json") + .build(); + + try { + httpClient.send(request, HttpResponse.BodyHandlers.discarding()); + } catch (InterruptedException e) { + throw new IOException("HTTP client was interrupted", e); + } } }