Merge pull request #24 from egmp777/master

HttpClientConnectionManager Tests
This commit is contained in:
Eugen
2014-07-01 18:11:47 +03:00
4 changed files with 500 additions and 7 deletions
@@ -0,0 +1,39 @@
package org.baeldung.httpclient;
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;
public IdleConnectionMonitorThread(final PoolingHttpClientConnectionManager connMgr) {
super();
this.connMgr = connMgr;
}
@Override
public void run() {
try {
while (!shutdown) {
synchronized (this) {
wait(1000);
connMgr.closeExpiredConnections();
connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
}
}
} catch (final InterruptedException ex) {
shutdown();
}
}
public void shutdown() {
shutdown = true;
synchronized (this) {
notifyAll();
}
}
}
@@ -0,0 +1,61 @@
package org.baeldung.httpclient;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
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;
public class MultiHttpClientConnThread extends Thread {
private final CloseableHttpClient client;
private final HttpGet get;
private PoolingHttpClientConnectionManager connManager = null;
private static HttpResponse response;
private Logger logger;
public int leasedConn;
public MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get, final PoolingHttpClientConnectionManager connManager) {
this.client = client;
this.get = get;
this.connManager = connManager;
logger = Logger.getLogger(MultiHttpClientConnThread.class.getName());
leasedConn = 0;
}
public MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get) {
this.client = client;
this.get = get;
logger = Logger.getLogger(MultiHttpClientConnThread.class.getName());
}
public int getLeasedConn() {
return leasedConn;
}
@Override
public void run() {
try {
if (this != null)
logger.log(Level.SEVERE, "Thread Running: " + getName());
response = client.execute(get);
if (connManager != null) {
logger.log(Level.SEVERE, "Leased Connections " + connManager.getTotalStats().getLeased());
leasedConn = connManager.getTotalStats().getLeased();
logger.log(Level.SEVERE, "Available Connections " + connManager.getTotalStats().getAvailable());
}
EntityUtils.consume(response.getEntity());
} catch (final ClientProtocolException ex) {
} catch (final IOException ex) {
}
}
}
@@ -0,0 +1,49 @@
package org.baeldung.httpclient;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
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;
public class TesterVersion_MultiHttpClientConnThread extends Thread {
private final CloseableHttpClient client;
private final HttpGet get;
private PoolingHttpClientConnectionManager connManager = null;
private Logger logger;
public int leasedConn;
public TesterVersion_MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get, final PoolingHttpClientConnectionManager connManager) {
this.client = client;
this.get = get;
this.connManager = connManager;
logger = Logger.getLogger(TesterVersion_MultiHttpClientConnThread.class.getName());
leasedConn = 0;
}
public int getLeasedConn() {
return leasedConn;
}
@Override
public void run() {
try {
if (this != null)
logger.log(Level.SEVERE, "Thread Running: " + getName());
client.execute(get);
if (connManager != null) {
logger.log(Level.SEVERE, "Leased Connections " + connManager.getTotalStats().getLeased());
leasedConn = connManager.getTotalStats().getLeased();
logger.log(Level.SEVERE, "Available Connections " + connManager.getTotalStats().getAvailable());
}
} catch (final ClientProtocolException ex) {
} catch (final IOException ex) {
}
}
}