用于多线程应用程序的HTTP客户端

时间:2011-11-10 20:56:25

标签: java http asynchronous

我正在实现基于Java的HTTP bot以进行性能测试。任何人都可以建议Java HTTP客户端库,适用于多线程环境。

看起来标准答案是Apache HTTP client,但它是同步的,在我看来,在这种情况下我需要一些异步解决方案。

3 个答案:

答案 0 :(得分:2)

您应该使用HTTP客户端ThreadSafeClientConnManager。它允许您跨线程重用HttpClient的一个实例。有关详细信息,请参阅this指南。

答案 1 :(得分:1)

这是一个实现你想要的简单例子(基于apache httpclient):

public class ApacheHttpTransportImpl extends BaseHttpTransport {
    private final CloseableHttpClient threadSafeClient;
    private final IdleConnectionMonitorThread monitor;  
    public ApacheHttpTransportImpl() throws NoSuchAlgorithmException, KeyManagementException {
        super(config);
        ConnectionSocketFactory socketFactory = new PlainConnectionSocketFactory();
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", socketFactory).build();
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        cm.setMaxTotal(256);
        cm.setDefaultMaxPerRoute(64);
        RequestConfig clientConfig = RequestConfig.custom().setConnectTimeout(2000)
.setSocketTimeout(1000).setConnectionRequestTimeout(2000).build();
        threadSafeClient = HttpClients.custom().setDefaultRequestConfig(clientConfig).setConnectionManager(cm).build();
        monitor = new IdleConnectionMonitorThread(cm, this);
        monitor.setDaemon(true);
        monitor.start();
    }
    public CloseableHttpClient get() {
        return threadSafeClient;
    }

    private static class IdleConnectionMonitorThread extends Thread {
        private final PoolingHttpClientConnectionManager cm;
        private final BlockingQueue<Stop> stopSignal = new ArrayBlockingQueue<Stop>(1);
        private final ApacheHttpTransportImpl cp;
        private static class Stop {
            private final BlockingQueue<Stop> stop = new ArrayBlockingQueue<Stop>(1);
            public void stopped() {
                stop.add(this);
            }
            public void waitForStopped() throws InterruptedException {
                stop.take();
            }
        }
        IdleConnectionMonitorThread(PoolingHttpClientConnectionManager cm, ApacheHttpTransportImpl cp) {
            super();
            this.cm = cm;
            this.cp = cp;
        }
        @Override
        public void run() {
            try {
                Stop stopRequest;
                while ((stopRequest = stopSignal.poll(5, TimeUnit.SECONDS)) == null) {
                    cm.closeExpiredConnections();
                    cm.closeIdleConnections(60, TimeUnit.SECONDS);
                }
                stopRequest.stopped();
            } catch (InterruptedException e) {
            }
        }       
    }
}

以下是它的使用方法:

ApacheHttpTransportImpl transport = new ApacheHttpTransportImpl();
HttpGet httpGet = new HttpGet("http://www.google.com");
CloseableHttpResponse httpResponse = transpot.get().execute(httpGet);

此外,您可以查看Unirest - 使用它非常简单,尽管它也基于apache httpclient。

答案 2 :(得分:0)

是的,这让我对Apache客户端感到困惑。如果您只需要简单的东西,可以使用URL和openConnection()