线程池跑http请求任务

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClients;
import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager;
import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.apache.http.nio.reactor.ConnectingIOReactor;
import org.apache.http.nio.reactor.IOReactorException;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

/**
 * @author lws
 */
public class Main {
    private static final int DEFAULT_TIME_OUT = 30000;
    private static final int MAX_CONNECTION_COUNT = 20;
    private static final int TOTAL_REQUEST_COUNT = 3000;

    public static void main(String[] args) throws IOException, InterruptedException, ExecutionException {
        final HttpGet request = new HttpGet("https://www.alipay.com/");
        List list = new ArrayList<>();
        long startTime = System.currentTimeMillis();
        try (CloseableHttpAsyncClient client = getHttpClient()) {
            client.start();
            List> futureList = new ArrayList<>();
            for (int i = 0; i < TOTAL_REQUEST_COUNT; i++) {
                futureList.add(client.execute(request, new FutureCallback() {
                    @Override
                    public void completed(HttpResponse httpResponse) {

                    }

                    @Override
                    public void failed(Exception e) {

                    }

                    @Override
                    public void cancelled() {

                    }
                }));
            }
            while (futureList.size() > 0) {
                Iterator> iterable = futureList.iterator();
                while (iterable.hasNext()) {
                    Future future = iterable.next();
                    if (future.isDone() && !future.isCancelled()) {
                        String content = EntityUtils.toString(future.get().getEntity(), "UTF-8");
                        list.add(random() + " " + content.substring(0, 10));
                        iterable.remove();
                    } else {
                        Thread.sleep(1);
                    }
                }
            }
        }
        long endTime = System.currentTimeMillis();
        List result = list.stream()
                .filter(str -> (Integer.parseInt(StringUtils.substringBefore(str, " "))) == 10)
                .collect(Collectors.toList());
        long resultTime = System.currentTimeMillis();
        System.out.println("获取" + TOTAL_REQUEST_COUNT + "次请求结果总耗时:" + (endTime - startTime) + "ms");
        System.out.println("处理" + TOTAL_REQUEST_COUNT + "次请求结果总耗时:" + (resultTime - endTime) + "ms");
    }

    private static CloseableHttpAsyncClient getHttpClient() throws IOReactorException {
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(DEFAULT_TIME_OUT)
                .setSocketTimeout(DEFAULT_TIME_OUT)
                .setConnectionRequestTimeout(DEFAULT_TIME_OUT)
                .build();

        IOReactorConfig ioReactorConfig = IOReactorConfig.custom().
                setIoThreadCount(Runtime.getRuntime().availableProcessors())
                .setSoKeepAlive(true)
                .build();

        ConnectingIOReactor ioReactor = new DefaultConnectingIOReactor(ioReactorConfig);
        PoolingNHttpClientConnectionManager connectionManager = new PoolingNHttpClientConnectionManager(ioReactor);
        connectionManager.setMaxTotal(MAX_CONNECTION_COUNT);
        connectionManager.setDefaultMaxPerRoute(MAX_CONNECTION_COUNT);

        return HttpAsyncClients.custom().
                setConnectionManager(connectionManager)
                .setDefaultRequestConfig(requestConfig)
                .build();
    }

    private static int random() {
        return new Random().nextInt(100);
    }
}

线程池的请求效率与带宽有影响,在贷款不满足的时候,可以适当减少连接数

你可能感兴趣的:(线程池跑http请求任务)