사용자 도구

사이트 도구


java:apache_http_client

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판 이전 판
다음 판
이전 판
java:apache_http_client [2015/04/14 10:51]
kwon37xi
java:apache_http_client [2021/07/27 10:26] (현재)
kwon37xi
줄 2: 줄 2:
   * [[http://hc.apache.org/|Apache Http Client]]   * [[http://hc.apache.org/|Apache Http Client]]
   * [[https://hc.apache.org/httpclient-3.x/preference-api.html|HttpClient 3.x preference api]]   * [[https://hc.apache.org/httpclient-3.x/preference-api.html|HttpClient 3.x preference api]]
 +  * [[https://hc.apache.org/httpcomponents-core-5.1.x/|Apache HttpComponents – HttpCore Overview]]
 +  * [[https://hc.apache.org/httpcomponents-client-5.1.x/current/httpclient5-fluent/clirr-report.html|Apache HttpComponents – HttpClient Overview]]
  
 ===== Connection 갯수 ===== ===== Connection 갯수 =====
줄 12: 줄 14:
     ....     ....
 </code> </code>
 +
 ===== Connection/Socket Timeout ===== ===== Connection/Socket Timeout =====
-  * [[http://www.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/|Configuring Timeout with Apache HttpClient 4.0]]+  * [[http://www.baeldung.com/httpclient-timeout|HttpClient timeout]]<code java> 
 +int timeout = 5; 
 +RequestConfig config = RequestConfig.custom() 
 +  .setConnectTimeout(timeout * 1000) 
 +  .setConnectionRequestTimeout(timeout * 1000) 
 +  .setSocketTimeout(timeout * 1000).build(); 
 +CloseableHttpClient client =  
 +  HttpClientBuilder.create().setDefaultRequestConfig(config).build(); 
 +</code> 
 +  * connectionTimeout : 서버에 소켓 연결을 맺을 때의 타임아웃 
 +  * connectionRequestTimeout : ConnectionManager(커넥션풀)로부터 꺼내올 때의 타임아웃 
 +  * socketTimeout : 요청/응답간의 타임아웃. 
 + 
 +===== Evict Idle Connections ===== 
 +  * KeepAlive 설정이 있을 경우, 서버에서 KeepAlive 시간 동안 사용되지 않은 커넥션(idle connection)을 죽이는 일이 생고 이를 ''HttpClient''가 인지하지 못해서 클라이언트측에서 오류가 발생할 수 있다. 
 +  * 클라이언트 측에서도 Idle Connection을 주기적으로 지워주면 된다. 
 <code java> <code java>
-DefaultHttpClient httpClient = new DefaultHttpClient(); +HttpClient httpClient = HttpClientBuilder.create(). 
-HttpParams params = httpClient.getParams(); +      .evictIdleConnections(2000LTimeUnit.MILLISECONDS
-HttpConnectionParams.setConnectionTimeout(httpParamsconnectionTimeoutMillis); +      .... 
-HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMillis);+      .build();
 </code> </code>
 +  * [[http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/impl/client/HttpClientBuilder.html#evictIdleConnections(long,%20java.util.concurrent.TimeUnit)|HttpClientBuilder.evictIdleConnections()]] 참조
  
 +>> Please note this method has no effect if the instance of HttpClient is configuted to use a shared connection manager.
 +>> Please note this method may not be used when the instance of HttpClient is created inside an EJB container.
 ===== Request Character Encoding ===== ===== Request Character Encoding =====
 ==== 일반 ==== ==== 일반 ====
줄 42: 줄 64:
   * [[http://www.baeldung.com/httpasyncclient-tutorial|HttpAsyncClient tutorial]]   * [[http://www.baeldung.com/httpasyncclient-tutorial|HttpAsyncClient tutorial]]
  
-===== JVM 버그 대응 ===== +===== Stale Check ===== 
-  * 아래와 같은 오류가 발생할 수 있다.<code> +  * Keep Alive 상태시 서버와 클라이언트 모두 접속중인 커넥션을 상대방에게 알리지 않고 접속을 끊을 수 있다. 
- +  * Stale Check에 대해 각 전별 버그 여부 등을 확인고 올바로 설정해야 한다. 
- +  * http://hc.apache.org/httpclient-3.x/performance.html 
-Caused by: java.util.NoSuchElementException: null +> HTTP specification permits both the client and the server to terminate a persistent (keep-alive) connection at any time without notice to the counterpart, thus rendering the connection invalid or staleBy default HttpClient performs a check, just prior to executing a request, to determine if the active connection is staleThe cost of this operation is about 15-30 ms, depending on the JRE usedDisabling stale connection check may result in slight performance improvement, especially for small payload responses, at the risk of getting an I/O error when executing a request over a connection that has been closed at the server side
-at java.util.ArrayList$Itr.next(ArrayList.java:757) ~[na:1.6.0_18] +
-at org.apache.http.impl.cookie.BestMatchSpec.formatCookies(BestMatchSpec.java:189) ~[httpclient-4.1.3.jar:4.1.3] +> See the http.connection.stalecheck parameter documentation for more information
-at org.apache.http.client.protocol.RequestAddCookies.process(RequestAddCookies.java:196) ~[httpclient-4.1.3.jar:4.1.3] +
-at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:108) ~[httpcore-4.1.4.jar:4.1.4] +
-at org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:174) ~[httpcore-4.1.4.jar:4.1.4] +
-at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:462) ~[httpclient-4.1.3.jar:4.1.3] +
-at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820) ~[httpclient-4.1.3.jar:4.1.3] +
-at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754) ~[httpclient-4.1.3.jar:4.1.3] +
-</code> +
-  * 이는 JVM의 Loop 최적화 관련 그로 보이며 Java 8과 Java 7u60 이상 전에서는 발생하지 않는다. 그 하 버전에서는 JVM 시작시 다음과 옵션을 줘야 한다.<code> +
--XX:-LoopUnswitching +
-</code> +
-  * [[https://issues.apache.org/jira/browse/HTTPCLIENT-1173|[HTTPCLIENT-1173] NoSuchElementException in org.apache.http.impl.cookie.BestMatchSpec.formatCookies]] +
-  * [[http://bugs.java.com/view_bug.do?bug_id=8021898|Bug ID: JDK-8021898 Broken JIT compiler optimization for loop unswitching]]+
  
 +===== 참조 =====
 +  * [[https://www.baeldung.com/httpclient-timeout|HttpClient timeout | Baeldung]]
 +  * [[https://danlebrero.com/2019/12/11/apache-http-client-timeouts-config-production-asynchronous-http-client-pictures/|Apache Http Client and Asynchronous HTTP client timeouts explained in pictures]]
 +  * [[https://github.com/dakrone/clj-http|dakrone/clj-http: An idiomatic clojure http client wrapping the apache client. Offically supported version.]]
 +  * [[https://d2.naver.com/helloworld/0881672|nGrinder에 적용한 HttpCore 5와 HttpClient 5 살펴보기]]
java/apache_http_client.1428976302.txt.gz · 마지막으로 수정됨: 2015/04/14 10:51 저자 kwon37xi