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 { Mapmap = 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); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)