Apache HttpClient - 需要使用MultiThreadedHttpConnectionManager吗?

时间:2014-09-04 13:38:25

标签: java multithreading http apache-commons-httpclient

我有服务器接收来自客户端的请求,并根据请求连接到某些外部网站&做一些操作。

我正在使用Apache Commons HttpClient(v 2.0.2)来进行这些连接(我知道它已经过时了,但由于其他限制我必须使用它。)

我的服务器不会经常收到请求。我认为首次部署时可能会有很多请求。然后它只会是一天的几个请求。偶尔会有很多请求可能会偶尔发生冲突。

所有连接都将是3个URL之一 - 它们可能是http或https

我在考虑为每个请求使用HttpClient的单独实例

我是否需要使用常见的HttpClient对象&与MultiThreadedHttpConnectionManager一起用于不同的连接。 MultiThreadedHttpConnectionManager究竟是如何帮助的 - 即使在调用releaseConnection之后它是否保持连接打开?它能保持多长时间?

我所有的联系都将是GET&它们最多会返回10-20个字节。我没有下载任何东西。我使用HttpClient而不是核心java库的原因是偶尔,我可能想使用HTTP 1.0(我不认为java类支持这个),我也可能想要自动执行Http重定向。

2 个答案:

答案 0 :(得分:0)

我在相当多线程的环境中使用PoolingHttpClientConnectionManager并且它运行良好。

以下是客户端池的实现:

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();
        }

    }

}

您需要做的只是CloseableHttpResponse conversation = HttpClientPool.getClient().execute(request);,当您完成它时,只需close它就会返回到游泳池。

答案 1 :(得分:0)

我认为这完全取决于您的SLA是什么以及性能是否在可接受/预期的响应时间内。您的解决方案可以正常运行,但如果您的应用程序需求随着时间的推移而不可扩展

使用MultiThreadedHttpConnectionManager比管理3个独立的HttpClient对象更加优雅/可扩展。