ApacheHttpClient4.5或JavaHttpsURLConnection-多个实例使用线程并行执行GET、POST操作导致时间增加
创始人
2024-09-05 23:00:54
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);
    }
}

相关内容

热门资讯

安装apache-beam==... 出现此错误可能是因为用户的Python版本太低,而apache-beam==2.34.0需要更高的P...
避免在粘贴双引号时向VS 20... 在粘贴双引号时向VS 2022添加反斜杠的问题通常是由于编辑器的自动转义功能引起的。为了避免这个问题...
Android Recycle... 要在Android RecyclerView中实现滑动卡片效果,可以按照以下步骤进行操作:首先,在项...
omi系统和安卓系统哪个好,揭... OMI系统和安卓系统哪个好?这个问题就像是在问“苹果和橘子哪个更甜”,每个人都有自己的答案。今天,我...
原生ios和安卓系统,原生对比... 亲爱的读者们,你是否曾好奇过,为什么你的iPhone和安卓手机在操作体验上有着天壤之别?今天,就让我...
Android - 无法确定任... 这个错误通常发生在Android项目中,表示编译Debug版本的Java代码时出现了依赖关系问题。下...
Android - NDK 预... 在Android NDK的构建过程中,LOCAL_SRC_FILES只能包含一个项目。如果需要在ND...
Akka生成Actor问题 在Akka框架中,可以使用ActorSystem对象生成Actor。但是,当我们在Actor类中尝试...
Agora-RTC-React... 出现这个错误原因是因为在 React 组件中使用,import AgoraRTC from “ago...
Alertmanager在pr... 首先,在Prometheus配置文件中,确保Alertmanager URL已正确配置。例如:ale...