mirror of
https://github.com/wahyd4/pinpoint.git
synced 2026-08-18 17:26:14 +10:00
[유치수] [tomcat-profiler-8] async http client interceptor 추가.
git-svn-id: http://svn.bds.nhncorp.com/pe/hippo-testweb/trunk@3520 84d0f5b1-2673-498c-a247-62c4ff18d310
This commit is contained in:
@@ -36,13 +36,18 @@
|
||||
<artifactId>lucy-npc</artifactId>
|
||||
<version>1.5.19</version>
|
||||
</dependency>
|
||||
<!--
|
||||
<dependency>
|
||||
<groupId>com.ning</groupId>
|
||||
<artifactId>async-http-client</artifactId>
|
||||
<version>1.8.3</version>
|
||||
</dependency>
|
||||
<!--
|
||||
<dependency>
|
||||
<groupId>com.nhncorp.lucy</groupId>
|
||||
<artifactId>lucy-rb-nimmconnector</artifactId>
|
||||
<version>1.0.2</version>
|
||||
</dependency>
|
||||
-->
|
||||
-->
|
||||
<dependency>
|
||||
<groupId>com.nhncorp.lucy</groupId>
|
||||
<artifactId>lucy-nimmconnector</artifactId>
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.nhn.pinpoint.testweb.controller;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
import com.nhn.pinpoint.testweb.util.AsyncHttpInvoker;
|
||||
import com.ning.http.client.Response;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author netspider
|
||||
*
|
||||
*/
|
||||
@Controller
|
||||
public class AsyncHTTPClientController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AsyncHTTPClientController.class);
|
||||
|
||||
private final AsyncHttpInvoker httpInvoker;
|
||||
|
||||
public AsyncHTTPClientController() {
|
||||
httpInvoker = new AsyncHttpInvoker();
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/asynchttp1")
|
||||
public String asynchttp(Model model) {
|
||||
Response r1 = httpInvoker.requestGet("http://www.naver.com", null, null);
|
||||
logger.debug("r1={}" + r1.toString());
|
||||
Response r2 = httpInvoker.requestPost("http://www.naver.com", null, "");
|
||||
logger.debug("r2={}" + r2.toString());
|
||||
|
||||
Response r3 = httpInvoker.requestGet("http://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&ie=utf8&query=naver&x=0&y=0", null, null);
|
||||
logger.debug("r3={}" + r3.toString());
|
||||
|
||||
Map<String, String> params = new HashMap<String, String>();
|
||||
params.put("query", "naver");
|
||||
params.put("where", "nexearch");
|
||||
Response r4 = httpInvoker.requestGet("http://search.naver.com/search.naver", null, null);
|
||||
logger.debug("r4={}" + r4.toString());
|
||||
|
||||
Response r5 = httpInvoker.requestGet("http://localhost:10080/allInOne2.pinpoint", null, null);
|
||||
logger.debug("r5={}", r5.toString());
|
||||
|
||||
return "http";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package com.nhn.pinpoint.testweb.util;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.ning.http.client.AsyncHttpClient;
|
||||
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
|
||||
import com.ning.http.client.AsyncHttpClientConfig;
|
||||
import com.ning.http.client.Response;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author netspider
|
||||
*
|
||||
*/
|
||||
public class AsyncHttpInvoker {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AsyncHttpInvoker.class);
|
||||
|
||||
private final AsyncHttpClient asyncHttpClient;
|
||||
private String defaultUserAgent;
|
||||
|
||||
public AsyncHttpInvoker() {
|
||||
asyncHttpClient = new AsyncHttpClient(new AsyncHttpClientConfig.Builder().setAllowPoolingConnection(true).setCompressionEnabled(true).build());
|
||||
defaultUserAgent = "pinpoint/test";
|
||||
logger.debug("init HttpClient : defaultAgent={}", defaultUserAgent);
|
||||
}
|
||||
|
||||
public Response requestPost(String url, List<Entry<String, String>> headers, String body) {
|
||||
if (url == null) {
|
||||
return null;
|
||||
}
|
||||
BoundRequestBuilder requestBuilder = asyncHttpClient.preparePost(url);
|
||||
try {
|
||||
requestBuilder = this.addHeader(requestBuilder, headers);
|
||||
requestBuilder.setBody(body).setBodyEncoding("UTF-8");
|
||||
Future<Response> f = requestBuilder.execute();
|
||||
Response response = f.get(500L, TimeUnit.MILLISECONDS);
|
||||
|
||||
logger.debug("\n\t [POST] url \t: " + url + "\n\t headers \t: " + headers + "\n\t body \t\t: " + body + "\n\t reponse \t: " + response.toString());
|
||||
return response;
|
||||
} catch (Exception e) {
|
||||
logger.debug("request read-timeout : url \t: " + url + "\n\t headers \t: " + headers + "\n\t body \t: " + body);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public Response requestGet(String url, Map<String, List<String>> queries, List<Entry<String, String>> headers) {
|
||||
if (url == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
BoundRequestBuilder requestBuilder = asyncHttpClient.prepareGet(url + queriesToQueryString(queries));
|
||||
|
||||
requestBuilder = this.addHeader(requestBuilder, headers);
|
||||
|
||||
try {
|
||||
Future<Response> f = requestBuilder.execute();
|
||||
Response response = f.get(500L, TimeUnit.MILLISECONDS);
|
||||
|
||||
logger.debug("\n\t [GET] url \t: " + url + "\n\t headers \t: " + headers + "\n\t queries \t: " + queries + "\n\t reponse \t: " + response.toString());
|
||||
|
||||
return response;
|
||||
} catch (Exception e) {
|
||||
logger.debug("request read-timeout : url \t: " + url + "\n\t headers \t: " + headers + "\n\t queries \t: " + queries);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private String queriesToQueryString(Map<String, List<String>> queries) {
|
||||
if (queries == null) {
|
||||
return StringUtils.EMPTY;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
// not implemented haha.
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private BoundRequestBuilder addHeader(BoundRequestBuilder requestBuilder, List<Entry<String, String>> headers) {
|
||||
if (headers == null) {
|
||||
return requestBuilder;
|
||||
}
|
||||
|
||||
for (Entry<String, String> entry : headers) {
|
||||
if (requestBuilder != null) {
|
||||
if (!entry.getKey().equals("User-Agent")) {
|
||||
requestBuilder.addHeader(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// rewrite user-agent
|
||||
if (requestBuilder != null) {
|
||||
requestBuilder.addHeader("User-Agent", this.defaultUserAgent);
|
||||
}
|
||||
|
||||
return requestBuilder;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.nhn.pinpoint.testweb.httpclient;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.ning.http.client.AsyncCompletionHandler;
|
||||
import com.ning.http.client.AsyncHttpClient;
|
||||
import com.ning.http.client.AsyncHttpClientConfig;
|
||||
import com.ning.http.client.ListenableFuture;
|
||||
import com.ning.http.client.Response;
|
||||
import com.ning.http.client.providers.netty.NettyAsyncHttpProvider;
|
||||
|
||||
public class AsyncHTTPClientTest {
|
||||
|
||||
@Test
|
||||
public void asyncHttpClient() {
|
||||
AsyncHttpClientConfig config = new AsyncHttpClientConfig.Builder().build();
|
||||
AsyncHttpClient client = new AsyncHttpClient(new NettyAsyncHttpProvider(config), config);
|
||||
|
||||
try {
|
||||
ListenableFuture<Response> future = client.prepareGet("http://www.naver.com").execute(new AsyncCompletionHandler<Response>() {
|
||||
@Override
|
||||
public Response onCompleted(Response response) throws Exception {
|
||||
// do something
|
||||
return response;
|
||||
}
|
||||
});
|
||||
|
||||
Response response = future.get(300L, TimeUnit.MILLISECONDS);
|
||||
|
||||
System.out.println(response.getResponseBody());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
} catch (TimeoutException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user