栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

httpclient

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

httpclient

package com.oppo.recommend.push.util;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.BoundRequestBuilder;
import org.asynchttpclient.Response;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.ExecutionException;

import static org.asynchttpclient.Dsl.asyncHttpClient;
import static org.asynchttpclient.Dsl.config;

@Slf4j
public class AsyncHttpClientUtil {

    
    private static volatile AsyncHttpClient HTTP_CLIENT = null;

    static  {
        // gracefully shutdown
        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                if (null != HTTP_CLIENT) {
                    try {
                        HTTP_CLIENT.close();
                    } catch (IOException e) {
                        log.error("close httpclient error [{}]", ExceptionUtils.getStackTrace(e));
                    } finally {
                        try {
                            if (null != HTTP_CLIENT) {
                                HTTP_CLIENT.close();
                            }
                        } catch (IOException e) {
                            log.error("close httpclient error [{}]", ExceptionUtils.getStackTrace(e));
                        }
                    }
                }
            }
        }));
    }

    public static AsyncHttpClient getHTTPClient() {
        if (null == HTTP_CLIENT) {
            synchronized (AsyncHttpClientUtil.class) {
                if (null == HTTP_CLIENT) {
                    HTTP_CLIENT = asyncHttpClient(config()
                            .setMaxConnections(800)
                            .setMaxConnectionsPerHost(30)
                            .setPooledConnectionIdleTimeout(100)
                            .setConnectionTtl(500)
                    );
                }
            }
        }
        return HTTP_CLIENT;
    }

    public static String doPostJsonAndGetString(String url, String docId, String title) throws ExecutionException, InterruptedException {
        Map map = new HashMap<>();
        List docIdList = new ArrayList<>();
        List titleList = new ArrayList<>();
        docIdList.add(docId);
        titleList.add(title);
        map.put("_caller_id", "recommended_video");
        map.put("doc_id", docIdList);
        map.put("doc_content", titleList);
        return doPostJsonAndGetString(url, map);

    }

    public static String doPostJsonAndGetString(String url, Map params) throws ExecutionException, InterruptedException {
        return doPostJsonAndGetString(url, GsonFactory.getNonPrettyGson().toJson(params));
    }

    public static String doPostJsonAndGetString(String url, String jsonStr) throws ExecutionException, InterruptedException {
        return AsyncHttpClientUtil.getHTTPClient().preparePost(url)
                .addHeader("Content-Type", "application/json")
                .setBody(jsonStr)
                .execute()
                .toCompletableFuture()
                .thenApply(Response::getResponseBody).get();
    }


    public static String doGetString(String url, Map map) throws ExecutionException, InterruptedException {
        BoundRequestBuilder request = AsyncHttpClientUtil.getHTTPClient().prepareGet(url);
        if (Objects.nonNull(map)) {
            for (String key : map.keySet()) {
                request.addQueryParam(key, map.get(key));
            }
        }
        return request.execute().toCompletableFuture().thenApply(Response::getResponseBody).get();
    }

    public static String doGetImeiString(String url, Map map) throws ExecutionException, InterruptedException {
        BoundRequestBuilder request = AsyncHttpClientUtil.getHTTPClient().prepareGet(url).addHeader("systemId", "browser");
        if (Objects.nonNull(map)) {
            for (String key : map.keySet()) {
                request.addQueryParam(key, map.get(key));
            }
        }
        return request.execute().toCompletableFuture().thenApply(Response::getResponseBody).get();
    }


    public static void main(String... args) throws ExecutionException, InterruptedException {
        Map params = new HashMap<>();
        List docidList = new ArrayList<>();
        docidList.add("123");
        docidList.add("223");
        docidList.add("323");
        List titleList = new ArrayList<>();
        titleList.add("老司机把丰田霸道当船开,开门瞬间众人都乐了。");
        titleList.add("美女集市一首《除了你》, 胖妹跟唱还送礼,以菜代花头一次见");
        titleList.add("婆婆故意将儿子跟儿媳灌醉,结果等儿子醒来的时候,懵圈了!");

        params.put("doc_id", docidList);
        params.put("doc_content", titleList);
        String feature = AsyncHttpClientUtil.doPostJsonAndGetString("http://10.34.94.5:8001/get_top_sec_cat/", params);
        System.out.println(feature);
        System.out.println(AsyncHttpClientUtil.doPostJsonAndGetString("http://10.34.94.5:8001/get_top_sec_cat/",
                "123", "老司机把丰田霸道当船开,开门瞬间众人都乐了。"));
        //       AsyncHttpClientUtil.getHTTPClient().prepareGet("http://distfiles.macports.org/scala2.11/").execute()
        //               .toCompletableFuture().thenApply(Response::getResponseBody).thenAccept(System.out::println);
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/658129.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号