diff --git a/source/pom.xml b/source/pom.xml index 6faea2a2..444a0ae5 100644 --- a/source/pom.xml +++ b/source/pom.xml @@ -58,25 +58,12 @@ - 4.5.14 - aerius_taskmanager taskmanager-report/target/site/jacoco-aggregate/jacoco.xml ${basedir}/../${aggregate.report.dir} - - - - - org.apache.httpcomponents - httpclient - ${httpcomponents-httpclient.version} - - - - aerius-nexus-public diff --git a/source/taskmanager-client/pom.xml b/source/taskmanager-client/pom.xml index bbaa065b..f2a738b7 100644 --- a/source/taskmanager-client/pom.xml +++ b/source/taskmanager-client/pom.xml @@ -37,12 +37,6 @@ amqp-client - - - org.apache.httpcomponents - httpclient - - ch.qos.logback diff --git a/source/taskmanager/pom.xml b/source/taskmanager/pom.xml index 9652322c..18d36201 100644 --- a/source/taskmanager/pom.xml +++ b/source/taskmanager/pom.xml @@ -42,6 +42,12 @@ commons-cli commons-cli + + + + org.apache.httpcomponents.client5 + httpclient5 + diff --git a/source/taskmanager/src/main/java/nl/aerius/taskmanager/mq/RabbitMQQueueMonitor.java b/source/taskmanager/src/main/java/nl/aerius/taskmanager/mq/RabbitMQQueueMonitor.java index e1a1deaa..c089b7be 100644 --- a/source/taskmanager/src/main/java/nl/aerius/taskmanager/mq/RabbitMQQueueMonitor.java +++ b/source/taskmanager/src/main/java/nl/aerius/taskmanager/mq/RabbitMQQueueMonitor.java @@ -19,28 +19,23 @@ import java.io.IOException; import java.io.InputStreamReader; import java.net.URI; -import java.net.URISyntaxException; -import java.net.URL; import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.TimeUnit; - -import org.apache.http.HttpHost; -import org.apache.http.HttpStatus; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.AuthCache; -import org.apache.http.client.CredentialsProvider; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.protocol.HttpClientContext; -import org.apache.http.impl.auth.BasicScheme; -import org.apache.http.impl.client.BasicAuthCache; -import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; + +import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.config.ConnectionConfig; +import org.apache.hc.client5.http.config.RequestConfig; +import org.apache.hc.client5.http.impl.auth.BasicScheme; +import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder; +import org.apache.hc.client5.http.io.HttpClientConnectionManager; +import org.apache.hc.client5.http.protocol.HttpClientContext; +import org.apache.hc.core5.http.HttpHost; +import org.apache.hc.core5.http.HttpStatus; +import org.apache.hc.core5.util.Timeout; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -60,7 +55,7 @@ class RabbitMQQueueMonitor { private static final Logger LOG = LoggerFactory.getLogger(RabbitMQQueueMonitor.class); - private static final int TIMEOUT = (int) TimeUnit.SECONDS.toMillis(3); + private static final Timeout TIMEOUT = Timeout.ofSeconds(3); private final ObjectMapper objectMapper = new ObjectMapper(); private final ConnectionConfiguration configuration; @@ -75,21 +70,17 @@ class RabbitMQQueueMonitor { */ public RabbitMQQueueMonitor(final ConnectionConfiguration configuration) { this.configuration = configuration; - httpClient = HttpClientBuilder.create().setDefaultRequestConfig(getDefaultRequestConfig()).build(); - targetHost = new HttpHost(configuration.getBrokerHost(), configuration.getBrokerManagementPort(), "http"); - final CredentialsProvider credsProvider = new BasicCredentialsProvider(); - credsProvider.setCredentials( - new AuthScope(configuration.getBrokerHost(), configuration.getBrokerManagementPort()), - new UsernamePasswordCredentials(configuration.getBrokerUsername(), configuration.getBrokerPassword())); - // Create AuthCache instance - final AuthCache authCache = new BasicAuthCache(); - // Generate BASIC scheme object and add it to the local auth cache + httpClient = HttpClientBuilder.create() + .setConnectionManager(getDefaultConnectionManager()) + .setDefaultRequestConfig(getDefaultRequestConfig()) + .build(); + targetHost = new HttpHost("http", configuration.getBrokerHost(), configuration.getBrokerManagementPort()); + // Generate BASIC scheme object and pre-emptively set the credentials (saves a roundtrip to figure out it should do so) final BasicScheme basicAuth = new BasicScheme(); - authCache.put(targetHost, basicAuth); + basicAuth.initPreemptive(new UsernamePasswordCredentials(configuration.getBrokerUsername(), configuration.getBrokerPassword().toCharArray())); context = HttpClientContext.create(); - context.setCredentialsProvider(credsProvider); - context.setAuthCache(authCache); + context.resetAuthExchange(targetHost, basicAuth); } /** @@ -125,7 +116,7 @@ public Map getQueueStates() { } else { LOG.error("Queue configuration from RabbitMQ admin json returned an unexpected value."); } - } catch (final URISyntaxException | IOException e) { + } catch (final IOException e) { LOG.info("Error getting RabbitMQ status from admin api: {}", e.getMessage()); } return queueStates; @@ -147,27 +138,34 @@ private static int getJsonIntPrimitive(final JsonNode jsonObject, final String k return jsonObject == null || !jsonObject.has(key) ? 0 : jsonObject.get(key).intValue(); } - protected JsonNode getJsonResultFromApi(final String apiPath) throws URISyntaxException, IOException { - // Not using new URI constructor because it will escape the %2f in apiPath to %252f. - final URI uri = new URL("http", configuration.getBrokerHost(), configuration.getBrokerManagementPort(), apiPath).toURI(); + protected JsonNode getJsonResultFromApi(final String apiPath) throws IOException { + final URI uri = URI.create("http://" + configuration.getBrokerHost() + ":" + configuration.getBrokerManagementPort() + apiPath); - try (final CloseableHttpResponse response = httpClient.execute(targetHost, new HttpGet(uri), context)) { - if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { + return httpClient.execute(targetHost, new HttpGet(uri), context, response -> { + if (response.getCode() == HttpStatus.SC_OK) { try (final InputStreamReader is = new InputStreamReader(response.getEntity().getContent(), StandardCharsets.UTF_8)) { return objectMapper.readTree(is); } } else { throw new IOException(String.format("Status code wasn't 200 when retrieving json result. Status was: %d, %s", - response.getStatusLine().getStatusCode(), response.getStatusLine())); + response.getCode(), response.getReasonPhrase())); } - } + }); + } + + private static HttpClientConnectionManager getDefaultConnectionManager() { + return PoolingHttpClientConnectionManagerBuilder.create() + .setDefaultConnectionConfig(ConnectionConfig.custom() + .setConnectTimeout(TIMEOUT) + .setSocketTimeout(TIMEOUT) + .build()) + .build(); } private static RequestConfig getDefaultRequestConfig() { return RequestConfig.custom() - .setConnectTimeout(TIMEOUT) .setConnectionRequestTimeout(TIMEOUT) - .setSocketTimeout(TIMEOUT) + .setResponseTimeout(TIMEOUT) .build(); } }