Skip to content

Commit

Permalink
Gjorde utsendingsklient closeable, slik at den closer requestfactory …
Browse files Browse the repository at this point in the history
…som igjen stopper jetty klienten
  • Loading branch information
Anders Flemmen committed Jun 4, 2019
1 parent 16b2fc8 commit e1f2530
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public FiksIOUtsendingKlient build() {

private RequestFactory createRequestFactory() {
return RequestFactoryImpl.builder()
.client(getOrCreateHttpClient())
.scheme(scheme)
.hostName(hostName)
.portNumber(portNumber)
Expand All @@ -90,18 +89,4 @@ private Function<Request, Request> 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;
}
}
4 changes: 3 additions & 1 deletion src/main/java/no/ks/fiks/io/klient/RequestFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
27 changes: 26 additions & 1 deletion src/main/java/no/ks/fiks/io/klient/RequestFactoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,31 @@
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;
private final String scheme;
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)
Expand All @@ -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);
}
}
}
4 changes: 1 addition & 3 deletions src/test/java/no/ks/fiks/io/klient/RequestFactoryTest.java
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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());
Expand Down

0 comments on commit e1f2530

Please sign in to comment.