Apache HttpClient - need to use MultiThreadedHttpConnectionManager?
I have a server that receives requests from clients and based on the requests it connects to some external website and does some operations.
I am using Apache Commons HttpClient
(v 2.0.2) for these connections (I know it is old, but I have to use it due to other restrictions).
My server won't receive frequent requests. I think there might be a lot of requests on first deployment. Then it will only have a few requests per day. Sometimes beatings can occur when many requests appear again.
All connections will be one of three urls: they can be http or https
I was thinking about using separate instances HttpClient
for each request
I need to use a shared object HttpClient
and use it with MultiThreadedHttpConnectionManager
for different connections. How exactly does the MultiThreadedHttpConnectionManager help - does this keep the connection even after the releaseConnection is called? How long will it stay open?
All my connections will be GET and they are going to return 10-20 bytes maximum. I am not downloading anything. The reason I use HttpClient
java libraries instead of core is because sometimes I can use HTTP 1.0 (I don't think Java classes support this) and I can do Http redirection automatically as well.
source to share
I am using PoolingHttpClientConnectionManager
in a significantly multithreaded environment and it works very well.
Here's the client pool implementation:
public class HttpClientPool {
// Single-element enum to implement Singleton.
private static enum Singleton {
// Just one of me so constructor will be called once.
Client;
// The thread-safe client.
private final CloseableHttpClient threadSafeClient;
// The pool monitor.
private final IdleConnectionMonitor monitor;
// The constructor creates it - thus late
private Singleton() {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// Increase max total connection to 200
cm.setMaxTotal(200);
// Increase default max connection per route to 200
cm.setDefaultMaxPerRoute(200);
// Make my builder.
HttpClientBuilder builder = HttpClients.custom()
.setRedirectStrategy(new LaxRedirectStrategy())
.setConnectionManager(cm);
// Build the client.
threadSafeClient = builder.build();
// Start up an eviction thread.
monitor = new IdleConnectionMonitor(cm);
// Start up the monitor.
Thread monitorThread = new Thread(monitor);
monitorThread.setDaemon(true);
monitorThread.start();
}
public CloseableHttpClient get() {
return threadSafeClient;
}
}
public static CloseableHttpClient getClient() {
// The thread safe client is held by the singleton.
return Singleton.Client.get();
}
public static void shutdown() throws InterruptedException, IOException {
// Shutdown the monitor.
Singleton.Client.monitor.shutdown();
}
// Watches for stale connections and evicts them.
private static class IdleConnectionMonitor implements Runnable {
// The manager to watch.
private final PoolingHttpClientConnectionManager cm;
// Use a BlockingQueue to stop everything.
private final BlockingQueue<Stop> stopSignal = new ArrayBlockingQueue<Stop>(1);
IdleConnectionMonitor(PoolingHttpClientConnectionManager cm) {
this.cm = cm;
}
public void run() {
try {
// Holds the stop request that stopped the process.
Stop stopRequest;
// Every 5 seconds.
while ((stopRequest = stopSignal.poll(5, TimeUnit.SECONDS)) == null) {
// Close expired connections
cm.closeExpiredConnections();
// Optionally, close connections that have been idle too long.
cm.closeIdleConnections(60, TimeUnit.SECONDS);
}
// Acknowledge the stop request.
stopRequest.stopped();
} catch (InterruptedException ex) {
// terminate
}
}
// Pushed up the queue.
private static class Stop {
// The return queue.
private final BlockingQueue<Stop> stop = new ArrayBlockingQueue<Stop>(1);
// Called by the process that is being told to stop.
public void stopped() {
// Push me back up the queue to indicate we are now stopped.
stop.add(this);
}
// Called by the process requesting the stop.
public void waitForStopped() throws InterruptedException {
// Wait until the callee acknowledges that it has stopped.
stop.take();
}
}
public void shutdown() throws InterruptedException, IOException {
// Signal the stop to the thread.
Stop stop = new Stop();
stopSignal.add(stop);
// Wait for the stop to complete.
stop.waitForStopped();
// Close the pool.
HttpClientPool.getClient().close();
// Close the connection manager.
cm.close();
}
}
}
All you have to do is CloseableHttpResponse conversation = HttpClientPool.getClient().execute(request);
, and when you're done with it, it's simple close
and it will be returned to the pool.
source to share
I think it all depends on what your SLA is and if the performance is within acceptable / expected response times. Your solution will work without any problem, but it won't scale if your application grows over time.
Using a MultiThreadedHttpConnectionManager
much more elegant / scalable solution than managing three independent HttpClient objects.
source to share