Java爬虫入门到精通(四)——连接池

采用连接池方式来解决HttpClient频繁创建和销毁的问题

一个完整的爬虫项目绝不可能只执行一次请求,所以HttpClient就会有频繁创建和销毁的问题,这个问题可以使用连接池类解决。

创建HttpClientPoolTest.java,可以在HttpGet出创建断点来观察HttpClient是否一样。

package crawler.test;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class HttpClientPoolTest {

    public static void main(String[] args) {

//        创建连接池管理器
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

//        设置最大连接数
        cm.setMaxTotal(100);

//        设置每台主机的最大连接数
        cm.setDefaultMaxPerRoute(10);

        //使用连接池管理器发起请求
        doGet(cm);
        doGet(cm);
    }

    private static void doGet(PoolingHttpClientConnectionManager cm) {

        //不是每次创建新的HttpClient,而是从连接池中获取HttpClient对象
        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();

        HttpGet httpGet = new HttpGet("https://baidu.com");

        CloseableHttpResponse response = null;

        try {
            response = httpClient.execute(httpGet);

            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "utf8");
                System.out.println(content.length());
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
//                不能关闭HttpClient,由连接池管理的
//                httpClient.close();
            }
        }

    }

}

创建断点,用debug运行



可以看到两次httpCilent对象的Value是不一样的

发布了12 篇原创文章 · 获赞 1 · 访问量 447

猜你喜欢

转载自blog.csdn.net/weixin_43235209/article/details/104572408