簡體   English   中英

多線程應用程序的HTTP客戶端

[英]HTTP client for multithreaded application

我正在實現基於Java的HTTP bot以進行性能測試。 任何人都可以建議Java HTTP客戶端庫,適合多線程環境。

看起來標准答案是Apache HTTP客戶端 ,但它是同步的,在我看來,在這種情況下,我需要一些異步解決方案。

您應該使用HTTP Client的ThreadSafeClientConnManager 它允許您跨線程重用一個HttpClient實例。 有關更多信息,請參見指南。

這是一個實現所需內容的簡單示例(基於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。

是的,這使我對Apache Client感到困惑。 如果您只需要簡單的內容,則可以使用URL和openConnection()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM