ApacheHttpClient4.5或JavaHttpsURLConnection-多个实例使用线程并行执行GET、POST操作导致时间增加
创始人
2024-09-05 23:00:44
0次

可以使用Java的线程池来控制多个实例的并发执行,从而减少等待时间。

下面是使用Apache HttpClient 4.5的示例代码:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

public class HttpParallelExecutor {
    
    private static final int MAX_TOTAL = 200;
    private static final int MAX_PER_ROUTE = 20;
    
    private HttpClient httpClient;
    private ExecutorService executorService;
    
    public HttpParallelExecutor() {
        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
        connManager.setMaxTotal(MAX_TOTAL);
        connManager.setDefaultMaxPerRoute(MAX_PER_ROUTE);
        this.httpClient = HttpClientBuilder.create().setConnectionManager(connManager).build();
        this.executorService = Executors.newFixedThreadPool(MAX_PER_ROUTE);
    }
    
    public void execute(HttpRequestBase requestBase) {
        executorService.execute(new HttpTask(httpClient, requestBase));
    }
    
    private static class HttpTask implements Runnable {
        private final HttpClient httpClient;
        private final HttpRequestBase requestBase;
        
        public HttpTask(HttpClient httpClient, HttpRequestBase requestBase) {
            this.httpClient = httpClient;
            this.requestBase = requestBase;
        }

        @Override
        public void run() {
            try {
                HttpResponse response = httpClient.execute(requestBase);
                // 处理响应
            } catch (Exception e) {
                // 处理异常
            } finally {
                requestBase.releaseConnection();
            }
        }
    }
}

使用该类来执行GET和POST请求:

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

public class Main {
    
    public static void main(String[] args) throws URISyntaxException, IOException {
        HttpParallelExecutor executor = new HttpParallelExecutor();
        
        HttpGet httpGet1 = new HttpGet(new URI("http://example.com/get1"));
        HttpGet httpGet2 = new HttpGet(new URI("http://example.com/get2"));
        HttpPost httpPost1 = new HttpPost(new URI("http://example.com/post1"));
        HttpPost httpPost2 = new HttpPost(new URI("http://example.com/post2"));
        httpPost1.setEntity(new StringEntity("Hello, World!"));
        httpPost2.setEntity(new StringEntity("Foo bar baz"));
        
        executor.execute(httpGet1);
        executor.execute(httpGet2);
        executor.execute(httpPost1);
        executor.execute(httpPost2);
    }
}

相关内容

热门资讯

专业讨论!wepoker模拟器... wepoker模拟器哪个好用AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏...
重大消息!wpk有那种辅助吗,... 重大消息!wpk有那种辅助吗,WPK有挂吗教程(先前有开挂)AI 智能体黑科技再上新,自动化能力开挂...
推荐几款新版!wepoker底... 您好:wepoker底牌透视这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
总算了解!wpk有作弊吗,德普... 总算了解!wpk有作弊吗,德普之星外卦神器软件教程(竟然有开挂)AI 智能体黑科技再上新,自动化能力...
大神推荐!wepoker透视脚... wepoker透视脚本免费AI 游戏攻略黑科技上线本地缓存功能,离线查阅能力开挂。提前下载对应游戏攻...
避坑细节!如何下载德普之星辅助... 『标题:当前标题』主打赛后复盘的 AI 游戏攻略黑科技走红,对局总结能力开挂。上传对局回放或者战绩截...
一分钟了解!!wepoker辅... AI 黑科技赛道又上新,这款智能处理工具堪称效率开挂,专门面向短视频创作者打造。它打通脚本撰写、AI...
玩家科普!wpk真的有透视嘛,... 玩家科普!wpk真的有透视嘛,WPK透视脚本工具(好像存在有辅助挂)AI 智能体黑科技再上新,自动化...
一分钟揭秘!德普之星辅助工具如... 您好:德普之星辅助工具如何打开这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多...
大家学习交流!wepoker游... 您好:wepoker游戏安装教程这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很...