From e1f25302f1d0cab07b9defba1727d89a135cd145 Mon Sep 17 00:00:00 2001 From: Anders Flemmen Date: Tue, 4 Jun 2019 16:15:23 +0200 Subject: [PATCH] Gjorde utsendingsklient closeable, slik at den closer requestfactory som igjen stopper jetty klienten --- .../fiks/io/klient/FiksIOUtsendingKlient.java | 8 +++++- .../klient/FiksIOUtsendingKlientBuilder.java | 15 ----------- .../no/ks/fiks/io/klient/RequestFactory.java | 4 ++- .../ks/fiks/io/klient/RequestFactoryImpl.java | 27 ++++++++++++++++++- .../ks/fiks/io/klient/RequestFactoryTest.java | 4 +-- 5 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/main/java/no/ks/fiks/io/klient/FiksIOUtsendingKlient.java b/src/main/java/no/ks/fiks/io/klient/FiksIOUtsendingKlient.java index 9619d27..9d2f93a 100644 --- a/src/main/java/no/ks/fiks/io/klient/FiksIOUtsendingKlient.java +++ b/src/main/java/no/ks/fiks/io/klient/FiksIOUtsendingKlient.java @@ -13,6 +13,7 @@ import org.eclipse.jetty.client.util.MultiPartContentProvider; import org.eclipse.jetty.client.util.StringContentProvider; +import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; @@ -27,7 +28,7 @@ import static org.eclipse.jetty.http.HttpStatus.isServerError; @Slf4j -public class FiksIOUtsendingKlient { +public class FiksIOUtsendingKlient implements Closeable { private final RequestFactory requestFactory; private final AuthenticationStrategy authenticationStrategy; @@ -84,4 +85,9 @@ private String serialiser(@NonNull Object metadata) { throw new RuntimeException("Feil under serialisering av metadata", e); } } + + @Override + public void close() throws IOException { + requestFactory.close(); + } } diff --git a/src/main/java/no/ks/fiks/io/klient/FiksIOUtsendingKlientBuilder.java b/src/main/java/no/ks/fiks/io/klient/FiksIOUtsendingKlientBuilder.java index 159438f..1fb6fed 100644 --- a/src/main/java/no/ks/fiks/io/klient/FiksIOUtsendingKlientBuilder.java +++ b/src/main/java/no/ks/fiks/io/klient/FiksIOUtsendingKlientBuilder.java @@ -76,7 +76,6 @@ public FiksIOUtsendingKlient build() { private RequestFactory createRequestFactory() { return RequestFactoryImpl.builder() - .client(getOrCreateHttpClient()) .scheme(scheme) .hostName(hostName) .portNumber(portNumber) @@ -90,18 +89,4 @@ private Function getOrCreateRequestInterceptor() { private ObjectMapper getOrCreateObjectMapper() { return objectMapper == null ? new ObjectMapper().findAndRegisterModules() : objectMapper; } - - private HttpClient getOrCreateHttpClient() { - final HttpClient internalClient = httpClient == null ? new HttpClient(new SslContextFactory.Client()) : httpClient; - if(! internalClient.isStarted()) { - log.debug("Starting http client"); - try { - internalClient.start(); - } catch (Exception e) { - log.warn("Feil under oppstart av Jetty HttpClient", e); - throw new IllegalStateException("Kunne ikke starte http client", e); - } - } - return internalClient; - } } diff --git a/src/main/java/no/ks/fiks/io/klient/RequestFactory.java b/src/main/java/no/ks/fiks/io/klient/RequestFactory.java index 1cddfde..1b01965 100644 --- a/src/main/java/no/ks/fiks/io/klient/RequestFactory.java +++ b/src/main/java/no/ks/fiks/io/klient/RequestFactory.java @@ -3,6 +3,8 @@ import org.eclipse.jetty.client.api.ContentProvider; import org.eclipse.jetty.client.api.Request; -public interface RequestFactory { +import java.io.Closeable; + +public interface RequestFactory extends Closeable { Request createSendToFiksIORequest(ContentProvider contentProvider); } diff --git a/src/main/java/no/ks/fiks/io/klient/RequestFactoryImpl.java b/src/main/java/no/ks/fiks/io/klient/RequestFactoryImpl.java index 87263ba..1bb0d3f 100644 --- a/src/main/java/no/ks/fiks/io/klient/RequestFactoryImpl.java +++ b/src/main/java/no/ks/fiks/io/klient/RequestFactoryImpl.java @@ -5,8 +5,10 @@ import org.eclipse.jetty.client.api.ContentProvider; import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.http.HttpMethod; +import org.eclipse.jetty.util.ssl.SslContextFactory; + +import java.io.IOException; -@Builder public class RequestFactoryImpl implements RequestFactory { static final String BASE_PATH = "/fiks-io/api/v1/"; private final HttpClient client; @@ -14,6 +16,20 @@ public class RequestFactoryImpl implements RequestFactory { private final String hostName; private final Integer portNumber; + @Builder + public RequestFactoryImpl(String scheme, String hostName, Integer portNumber) { + this.scheme = scheme; + this.hostName = hostName; + this.portNumber = portNumber; + + this.client = new HttpClient(new SslContextFactory.Client()); + try { + client.start(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + @Override public Request createSendToFiksIORequest(ContentProvider contentProvider) { return client.newRequest(hostName, portNumber) @@ -22,4 +38,13 @@ public Request createSendToFiksIORequest(ContentProvider contentProvider) { .path(BASE_PATH + "send") .content(contentProvider); } + + @Override + public void close() { + try { + client.stop(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } } diff --git a/src/test/java/no/ks/fiks/io/klient/RequestFactoryTest.java b/src/test/java/no/ks/fiks/io/klient/RequestFactoryTest.java index 0e4e28b..e70a7f2 100644 --- a/src/test/java/no/ks/fiks/io/klient/RequestFactoryTest.java +++ b/src/test/java/no/ks/fiks/io/klient/RequestFactoryTest.java @@ -1,11 +1,10 @@ package no.ks.fiks.io.klient; -import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.api.Request; import org.eclipse.jetty.client.util.StringContentProvider; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertEquals; class RequestFactoryTest { @@ -18,7 +17,6 @@ void createSendToFiksIORequest() { .scheme(scheme) .hostName(hostName) .portNumber(portNumber) - .client(new HttpClient()) .build() .createSendToFiksIORequest(new StringContentProvider("stuff")); assertEquals(RequestFactoryImpl.BASE_PATH + "send", sendToFiksIORequest.getPath());