Java异步执行多个HTTP请求的例子(需要apache http类库)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java异步执行多个HTTP请求的例子(需要apache http类库)相关的知识,希望对你有一定的参考价值。

直接上代码

 

package org.jivesoftware.spark.util;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

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.jivesoftware.DebugPrint;
import org.jivesoftware.spark.SparkManager;

//异步埋点数据采集工具类
public class HotClickAsync {
    static ExecutorService service = Executors.newSingleThreadExecutor(); //单一线程
    // 调用http请求。不阻塞主线程
    public static void SendRequest(final String event)
            throws InterruptedException, IOException {
        
        Runnable run = new Runnable() {
            @Override
            public void run() {
                try {
                    SendRequestAsync(event,SparkManager.getSessionManager().getUsername());
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        };
        service.execute(run);
    }

    // 阻塞HTTP调用
    private static void SendRequestAsync(String event,String username)
            throws InterruptedException, IOException {
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(1000) // http超时
                .setConnectTimeout(1000).build(); // 连接超时
        CloseableHttpAsyncClient httpclient = HttpAsyncClients.custom()
                .setDefaultRequestConfig(requestConfig).build();
        try {
            httpclient.start();
            final HttpGet[] requests = new HttpGet[] { new HttpGet(
                    "http://XXXXXX.cn:81/HotClick.aspx?event="+ event +"&username="+username) // 第一个采集地址
            // , new HttpGet("http://mta.qq.com")//第二个采集地址, http://mta.qq.com/
            };
            // 同步计数
            final CountDownLatch latch = new CountDownLatch(requests.length);
            for (final HttpGet request : requests) {
                httpclient.execute(request, new FutureCallback<HttpResponse>() {

                    @Override
                    public void completed(final HttpResponse response) {
                        latch.countDown();
                        DebugPrint.outStirng(request.getRequestLine() + "####->"
                                + response.getStatusLine());
                        
                    }

                    @Override
                    public void failed(final Exception ex) {
                        latch.countDown();
                        DebugPrint.outStirng(request.getRequestLine() + "####->" + ex);
                    }

                    @Override
                    public void cancelled() {
                        latch.countDown();
                        //DebugPrint.outStirng(request.getRequestLine()
                        //        + " cancelled");
                    }

                });
            }
            latch.await();
        } finally {
            httpclient.close();
        }
        DebugPrint.outStirng(" ###  HotClickAsync Done ###");

    }

}

 

以上是关于Java异步执行多个HTTP请求的例子(需要apache http类库)的主要内容,如果未能解决你的问题,请参考以下文章

angularjs $q$http 处理多个异步请求

Java I/O模型

angular中处理多个异步请求的方法汇总

多个 HTTP 请求触发 HTTP Client 超时

多个异步 http get 请求

使用python 2.7的简单异步HTTP请求