Java的异步HttpClient

原文:http://www.cnblogs.com/guogangj/p/5462594.html

上篇提到了高性能处理的关键是异步,而我们当中许多人依旧在使用同步模式的HttpClient访问第三方Web资源,我认为原因之一是:异步的HttpClient诞生较晚,许多人不知道;另外也可能是大多数Web程序其实不在意这点性能损失了。

而要自己实现一个异步的HttpClient则比较困难,通常都是自己开一个新的工作线程,利用HttpClient的同步去访问,完成后再回调这种形式,这样做其实不是真正的异步,因为依旧会有一个线程处于阻塞中,等待着第三方Web资源的返回。

而如今访问第三方Web资源的情景越来越多,最典型就是使用第三方登录平台,如QQ或微信等,我们需要访问腾讯的服务器去验证登录者的身份,根据我的经验,这个过程可能会阻塞好几秒钟,可看作是一个“长时间调用”,所以最好要使用异步方式。

OK,废话少说,要使用异步的HttpClient,请Maven中带上:

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpasyncclient</artifactId>
            <version>4.1.1</version>
        </dependency>

接下来是一个完整的Demo代码:

复制代码
import org.apache.http.HttpResponse;
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.util.EntityUtils;

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

public class Main {
    public static void main(String[] argv) {
        CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault();
        httpclient.start();

        final CountDownLatch latch = new CountDownLatch(1);
        final HttpGet request = new HttpGet("https://www.alipay.com/");

        System.out.println(" caller thread id is : " + Thread.currentThread().getId());

        httpclient.execute(request, new FutureCallback<HttpResponse>() {

            public void completed(final HttpResponse response) {
                latch.countDown();
                System.out.println(" callback thread id is : " + Thread.currentThread().getId());
                System.out.println(request.getRequestLine() + "->" + response.getStatusLine());
                try {
                    String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                    System.out.println(" response content is : " + content);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            public void failed(final Exception ex) {
                latch.countDown();
                System.out.println(request.getRequestLine() + "->" + ex);
                System.out.println(" callback thread id is : " + Thread.currentThread().getId());
            }

            public void cancelled() {
                latch.countDown();
                System.out.println(request.getRequestLine() + " cancelled");
                System.out.println(" callback thread id is : " + Thread.currentThread().getId());
            }

        });
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        try {
            httpclient.close();
        } catch (IOException ignore) {

        }
    }
}
复制代码

 呃……代码很简单,好像也没什么好说的了,稍作封装就可以实现如“getJson()”这样的方法。

也许你还注意到了,这个HttpClient跟同步的版本一样,直接支持https,但如果网站的证书是自签的,默认还是不行的,解决方法当然有,但代码有些麻烦,我觉得还不如直接买张证书来得简单,如果网站是你管的话。

原文地址:https://www.cnblogs.com/shihaiming/p/6286583.html