Best Practice to Use HttpClient in Multithreaded Environment

后端 未结 5 1875
猫巷女王i
猫巷女王i 2020-11-27 09:40

For a while, I have been using HttpClient in a multithreaded environment. For every thread, when it initiates a connection, it will create a completely new HttpClient instan

5条回答
  •  醉酒成梦
    2020-11-27 10:22

    Definitely Method A because its pooled and thread safe.

    If you are using httpclient 4.x, the connection manager is called ThreadSafeClientConnManager. See this link for further details (scroll down to "Pooling connection manager"). For example:

        HttpParams params = new BasicHttpParams();
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        ClientConnectionManager cm = new ThreadSafeClientConnManager(params, registry);
        HttpClient client = new DefaultHttpClient(cm, params);
    

提交回复
热议问题