From 9c9075a3c5a88ca895039a9f648a2f1f606e9960 Mon Sep 17 00:00:00 2001 From: Lari Sinisalo <65231729+LariSinisalo@users.noreply.github.com> Date: Mon, 16 Sep 2024 14:49:42 +0300 Subject: [PATCH] Resource leaking fixes for issue #60 --- pom.xml | 12 ++++++++++- .../restclient/RestApiClient.java | 8 +++++++- .../igniterealtime/restclient/RestClient.java | 20 ++++++++++++++++--- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 38a20cc..f30b021 100644 --- a/pom.xml +++ b/pom.xml @@ -39,7 +39,7 @@ 2.22.2 2.10.4 - 3.1.5 + 3.1.8 4.0.0 17 17 @@ -57,6 +57,16 @@ jersey-media-moxy ${jersey.version} + + org.glassfish.jersey.incubator + jersey-injectless-client + ${jersey.version} + + + org.glassfish.jersey.media + jersey-media-jaxb + ${jersey.version} + org.slf4j slf4j-api diff --git a/src/main/java/org/igniterealtime/restclient/RestApiClient.java b/src/main/java/org/igniterealtime/restclient/RestApiClient.java index 07a4ba2..6e65d5f 100644 --- a/src/main/java/org/igniterealtime/restclient/RestApiClient.java +++ b/src/main/java/org/igniterealtime/restclient/RestApiClient.java @@ -26,7 +26,7 @@ /** * The Class RestApiClient. */ -public class RestApiClient { +public class RestApiClient implements AutoCloseable { /** The rest client. */ private RestClient restClient; @@ -766,4 +766,10 @@ private String adjustURL(String url) { return url; } + @Override + public void close() + { + this.restClient.close(); + } + } diff --git a/src/main/java/org/igniterealtime/restclient/RestClient.java b/src/main/java/org/igniterealtime/restclient/RestClient.java index 100c2f7..617c636 100644 --- a/src/main/java/org/igniterealtime/restclient/RestClient.java +++ b/src/main/java/org/igniterealtime/restclient/RestClient.java @@ -47,7 +47,7 @@ /** * The Class RestClient. */ -public final class RestClient { +public final class RestClient implements AutoCloseable { /** * The Constant LOG. @@ -84,6 +84,8 @@ public final class RestClient { */ private SupportedMediaType mediaType; + private Client client; + /** * Gets the. * @@ -237,7 +239,7 @@ private WebTarget createWebTarget(String restPath, Map queryPara try { URI u = new URI(this.baseURI + "/plugins/restapi/v1/" + restPath); LOG.debug("Connecting to:" + u); - Client client = createRestClient(); + Client client = getOrCreateRestClient(); webTarget = client.target(u); if (queryParams != null && !queryParams.isEmpty()) { @@ -276,7 +278,10 @@ private RestClient(RestClientBuilder builder) { * @throws KeyManagementException the key management exception * @throws NoSuchAlgorithmException the no such algorithm exception */ - private Client createRestClient() throws KeyManagementException, NoSuchAlgorithmException { + private Client getOrCreateRestClient() throws KeyManagementException, NoSuchAlgorithmException { + if(this.client != null) + return this.client; + ClientConfig clientConfig = new ClientConfig(); // Set connection timeout if (this.connectionTimeout != 0) { @@ -296,6 +301,8 @@ private Client createRestClient() throws KeyManagementException, NoSuchAlgorithm client = ClientBuilder.newClient(clientConfig); } + this.client = client; + return client; } @@ -557,4 +564,11 @@ public SupportedMediaType getMediaType() { public void setMediaType(SupportedMediaType mediaType) { this.mediaType = mediaType; } + + @Override + public void close() + { + if(this.client != null) + this.client.close(); + } }