异步Http请求封装工具类AsyncHttpClientUtil

建立异步Http请求客户端之后,所有http请求都复用该HttpAsyncClient,内部只对应一个Reactor I/O线程。
一个Reactor I/O线程其实可以同时处理成千上万个连接请求,完全满足我的项目需要。

单例HttpAsyncClient

    package com.peas.pay.util;  

    import java.io.IOException;  
    import java.io.UnsupportedEncodingException;  
    import java.util.HashMap;  
    import java.util.List;  
    import java.util.Map;  

    import org.apache.http.HttpEntity;  
    import org.apache.http.HttpResponse;  
    import org.apache.http.client.config.RequestConfig;  
    import org.apache.http.client.entity.UrlEncodedFormEntity;  
    import org.apache.http.client.methods.HttpGet;  
    import org.apache.http.client.methods.HttpPost;  
    import org.apache.http.client.methods.HttpUriRequest;  
    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.reactor.IOReactorConfig;  
    import org.apache.http.message.BasicNameValuePair;  
    import org.apache.http.util.EntityUtils;  

    import com.google.common.base.Preconditions;  
    import com.google.common.collect.Lists;  

    import lombok.extern.log4j.Log4j;  

    /** 
     * Created by xiekunliang on 2018/3/24. 异步Http请求封装工具类 
     */  
    @Log4j  
    public class AsyncHttpClientUtil  
    {  
        private static CloseableHttpAsyncClient httpClient;  
        private static volatile boolean isClientStart;  

        /** 
         * 创建CloseableHttpAsyncClient 
         * @return 
         */  
        private static CloseableHttpAsyncClient createCustomAsyncClient()  
        {  
            Preconditions.checkState(!isClientStart, "客户端HttpAsyncClient已经建立过了");  
            IOReactorConfig ioReactorConfig = IOReactorConfig.custom()  
                    .setIoThreadCount(Runtime.getRuntime().availableProcessors())  
                    .setConnectTimeout(60000)  
                    .setSoTimeout(60000)  
                    .build();  
            // 设置超时时间 毫秒为单位  
            RequestConfig requestConfig = RequestConfig  
                    .copy(RequestConfig.DEFAULT)  
                    .setConnectTimeout(60000)  
                    .build();  
            return HttpAsyncClients  
                    .custom()  
                    .setDefaultIOReactorConfig(ioReactorConfig)  
                    .setDefaultRequestConfig(requestConfig)  
                    .build();  

        }  


        public static void startHttpClient()  
        {  
            httpClient = createCustomAsyncClient();  
            httpClient.start();  
            isClientStart = true;  
        }  

        public static void closeHttpClient()  
        {  
            try {  
                httpClient.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
            isClientStart = false;  
        }  

        public static void http(String method,   
                                String url,   
                                Object parameter,   
                                StringFutureCallback callback)  
        {  
            Preconditions.checkNotNull(method);  
            if ("GET".equals(method))  
            {  
                get(url, callback);  
            }  
            else if ("POST".equals(method))  
            {  
                post(url, parameter, callback);  
            }  
        }  


        public static void get(String url, StringFutureCallback callback)  
        {  
            Preconditions.checkArgument(isClientStart, "还没有建立Http Client");  
            HttpUriRequest request = new HttpGet(url);  
            httpClient.execute(request, new DefaultFutureCallback(callback));  
        }  

        public static void post(String url, Object parameter, StringFutureCallback callback)  
        {  
            Preconditions.checkArgument(isClientStart, "还没有建立Http Client");  
            HttpPost httpPost = new HttpPost(url);  
            if (parameter != null)  
            {  
                List pairs = Lists.newArrayList();  
                UrlEncodedFormEntity entity = null;  
                try {  
                    if (parameter instanceof HashMap)  
                    {  
                        Map parameters = (Map) parameter;  
                        parameters.forEach((k, v) -> pairs.add(new BasicNameValuePair(k,v)));  
                        entity = new UrlEncodedFormEntity(pairs, "UTF-8");  
                    }  
                } catch (UnsupportedEncodingException e) {  
                    e.printStackTrace();  
                }  
                httpPost.setEntity(entity);  
            }  
            httpClient.execute(httpPost, new DefaultFutureCallback(callback));  
        }  
    /** 
         * 字符串类型结果回调 
         */  
        public interface StringFutureCallback  
        {  
            void success(String content);  
        }  
    public static class DefaultFutureCallback implements FutureCallback  
        {  
            private StringFutureCallback callback;  
            public DefaultFutureCallback(StringFutureCallback callback)  
            {  
                this.callback = callback;  
            }  
            @Override  
            public void completed(HttpResponse httpResponse)  
            {  
                HttpEntity entity = httpResponse.getEntity();  
                String content = "";  
                try  
                {  
                    content = EntityUtils.toString(entity, "UTF-8");  
                }  
                catch (IOException e)  
                {  
                    e.printStackTrace();  
                }  
                callback.success(content);  
            }  

            @Override  
            public void failed(Exception e)  
            {  
                e.printStackTrace();  
            }  

            @Override  
            public void cancelled()  
            {  
                log.debug("http request cancelled");  
            }  
        }  
    }  

你可能感兴趣的:(网络编程)