JAVA-15023: Chnages made for upgrading Apache HttpClient Connection M… (#14402)
This commit is contained in:
+151
-190
File diff suppressed because it is too large
Load Diff
+8
-9
@@ -2,12 +2,12 @@ package com.baeldung.httpclient.conn;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
|
||||
import org.apache.hc.core5.http.HttpEntity;
|
||||
import org.apache.hc.core5.http.HttpResponse;
|
||||
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -45,22 +45,21 @@ public class MultiHttpClientConnThread extends Thread {
|
||||
try {
|
||||
logger.debug("Thread Running: " + getName());
|
||||
|
||||
logger.debug("Thread Running: " + getName());
|
||||
|
||||
if (connManager != null) {
|
||||
logger.info("Before - Leased Connections = " + connManager.getTotalStats().getLeased());
|
||||
logger.info("Before - Available Connections = " + connManager.getTotalStats().getAvailable());
|
||||
}
|
||||
|
||||
final HttpResponse response = client.execute(get);
|
||||
HttpEntity entity = client.execute(get).getEntity();
|
||||
|
||||
if (connManager != null) {
|
||||
leasedConn = connManager.getTotalStats().getLeased();
|
||||
logger.info("After - Leased Connections = " + connManager.getTotalStats().getLeased());
|
||||
logger.info("After - Available Connections = " + connManager.getTotalStats().getAvailable());
|
||||
}
|
||||
EntityUtils.consume(entity);
|
||||
|
||||
EntityUtils.consume(response.getEntity());
|
||||
} catch (final IOException ex) {
|
||||
logger.error("", ex);
|
||||
}
|
||||
|
||||
+3
-4
@@ -2,10 +2,9 @@ package com.baeldung.httpclient.conn;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
||||
+349
File diff suppressed because it is too large
Load Diff
+41
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.httpclient.httpclient.conn;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.http.conn.HttpClientConnectionManager;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
|
||||
public class IdleConnectionMonitorThread extends Thread {
|
||||
private final HttpClientConnectionManager connMgr;
|
||||
private volatile boolean shutdown;
|
||||
|
||||
IdleConnectionMonitorThread(final PoolingHttpClientConnectionManager connMgr) {
|
||||
super();
|
||||
this.connMgr = connMgr;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
@Override
|
||||
public final void run() {
|
||||
try {
|
||||
while (!shutdown) {
|
||||
synchronized (this) {
|
||||
wait(1000);
|
||||
connMgr.closeExpiredConnections();
|
||||
connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
} catch (final InterruptedException ex) {
|
||||
shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
private void shutdown() {
|
||||
shutdown = true;
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.httpclient.httpclient.conn;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class MultiHttpClientConnThread extends Thread {
|
||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
private final CloseableHttpClient client;
|
||||
private final HttpGet get;
|
||||
|
||||
private PoolingHttpClientConnectionManager connManager;
|
||||
private int leasedConn;
|
||||
|
||||
MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get, final PoolingHttpClientConnectionManager connManager) {
|
||||
this.client = client;
|
||||
this.get = get;
|
||||
this.connManager = connManager;
|
||||
leasedConn = 0;
|
||||
}
|
||||
|
||||
MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get) {
|
||||
this.client = client;
|
||||
this.get = get;
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
final int getLeasedConn() {
|
||||
return leasedConn;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public final void run() {
|
||||
try {
|
||||
logger.debug("Thread Running: " + getName());
|
||||
|
||||
logger.debug("Thread Running: " + getName());
|
||||
|
||||
if (connManager != null) {
|
||||
logger.info("Before - Leased Connections = " + connManager.getTotalStats().getLeased());
|
||||
logger.info("Before - Available Connections = " + connManager.getTotalStats().getAvailable());
|
||||
}
|
||||
|
||||
final HttpResponse response = client.execute(get);
|
||||
|
||||
if (connManager != null) {
|
||||
leasedConn = connManager.getTotalStats().getLeased();
|
||||
logger.info("After - Leased Connections = " + connManager.getTotalStats().getLeased());
|
||||
logger.info("After - Available Connections = " + connManager.getTotalStats().getAvailable());
|
||||
}
|
||||
|
||||
EntityUtils.consume(response.getEntity());
|
||||
} catch (final IOException ex) {
|
||||
logger.error("", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.httpclient.httpclient.conn;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.http.client.ClientProtocolException;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
public class TesterVersion_MultiHttpClientConnThread extends Thread {
|
||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||
|
||||
private final CloseableHttpClient client;
|
||||
private final HttpGet get;
|
||||
private PoolingHttpClientConnectionManager connManager;
|
||||
|
||||
TesterVersion_MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get, final PoolingHttpClientConnectionManager connManager) {
|
||||
this.client = client;
|
||||
this.get = get;
|
||||
this.connManager = Preconditions.checkNotNull(connManager);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Override
|
||||
public final void run() {
|
||||
try {
|
||||
logger.debug("Thread Running: " + getName());
|
||||
|
||||
logger.info("Before - Leased Connections = " + connManager.getTotalStats().getLeased());
|
||||
logger.info("Before - Available Connections = " + connManager.getTotalStats().getAvailable());
|
||||
|
||||
client.execute(get);
|
||||
|
||||
logger.info("After - Leased Connections = " + connManager.getTotalStats().getLeased());
|
||||
logger.info("After - Available Connections = " + connManager.getTotalStats().getAvailable());
|
||||
} catch (final IOException ex) {
|
||||
logger.error("", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user