HttpClientConnectionManager에서 SSLContext 생성시, entropy
source(/dev/random)에서 랜덤시드값 가져올때, 오래걸림 이슈. -Djava.security.egd=file:/dev/./urandom 설정이 들어가있지만, java security provider가 BC사용중이라 안먹는 것 같음. provider를 바꾸자니 성능 테스트 진행 중이라 영향이 갈수있으므로, Pooling 방식 제거.
This commit is contained in:
@@ -1,20 +1,15 @@
|
|||||||
package com.eactive.eai.custom.alarm.ums;
|
package com.eactive.eai.custom.alarm.ums;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
import javax.annotation.PreDestroy;
|
|
||||||
|
|
||||||
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
import org.apache.hc.client5.http.classic.methods.HttpPost;
|
||||||
import org.apache.hc.client5.http.config.RequestConfig;
|
import org.apache.hc.client5.http.config.RequestConfig;
|
||||||
import org.apache.hc.client5.http.entity.EntityBuilder;
|
import org.apache.hc.client5.http.entity.EntityBuilder;
|
||||||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
|
||||||
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
||||||
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
|
|
||||||
import org.apache.hc.client5.http.io.HttpClientConnectionManager;
|
|
||||||
import org.apache.hc.core5.http.ContentType;
|
import org.apache.hc.core5.http.ContentType;
|
||||||
|
import org.apache.hc.core5.http.HttpVersion;
|
||||||
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
import org.apache.hc.core5.http.io.entity.EntityUtils;
|
||||||
import org.apache.hc.core5.util.TimeValue;
|
|
||||||
import org.apache.hc.core5.util.Timeout;
|
import org.apache.hc.core5.util.Timeout;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@@ -43,24 +38,6 @@ public class UmsService {
|
|||||||
private final static String DEFAULT_API_KEY = "7cca6d58-e4f7-474d-9edd-525806bc99ff";
|
private final static String DEFAULT_API_KEY = "7cca6d58-e4f7-474d-9edd-525806bc99ff";
|
||||||
private final static String AUTHORIZATION_FMT = "Bearer %s";
|
private final static String AUTHORIZATION_FMT = "Bearer %s";
|
||||||
|
|
||||||
// 재사용되는 HttpClient (Thread-Safe)
|
|
||||||
private final CloseableHttpClient httpClient;
|
|
||||||
|
|
||||||
public UmsService() {
|
|
||||||
// 커넥션 풀 설정: 매번 소켓을 열고 닫지 않고 재사용함
|
|
||||||
HttpClientConnectionManager cm = PoolingHttpClientConnectionManagerBuilder.create()
|
|
||||||
.setMaxConnTotal(100) // 전체 최대 커넥션 수
|
|
||||||
.setMaxConnPerRoute(20) // 호스트당 최대 커넥션 수
|
|
||||||
.setConnectionTimeToLive(TimeValue.ofMinutes(3)) // TTL
|
|
||||||
.setValidateAfterInactivity(TimeValue.ofSeconds(5)) // 🔥 중요
|
|
||||||
.build();
|
|
||||||
|
|
||||||
this.httpClient = HttpClients.custom()
|
|
||||||
.setConnectionManager(cm)
|
|
||||||
.evictExpiredConnections() // 만료 제거
|
|
||||||
.evictIdleConnections(TimeValue.ofSeconds(20)) // 20초 idle 제거
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
public UmsCallResult send(UmsRequestPayload payload) throws Exception {
|
public UmsCallResult send(UmsRequestPayload payload) throws Exception {
|
||||||
if (payload.getContent() == null) {
|
if (payload.getContent() == null) {
|
||||||
@@ -95,6 +72,9 @@ public class UmsService {
|
|||||||
.setContentType(ContentType.APPLICATION_JSON)
|
.setContentType(ContentType.APPLICATION_JSON)
|
||||||
.setContentEncoding("UTF-8")
|
.setContentEncoding("UTF-8")
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
|
httpPost.setVersion(HttpVersion.HTTP_1_0);
|
||||||
|
httpPost.setHeader("Connection", "close");
|
||||||
|
|
||||||
// 3. 타임아웃 설정 (설정파일 값 적용)
|
// 3. 타임아웃 설정 (설정파일 값 적용)
|
||||||
RequestConfig config = RequestConfig.custom()
|
RequestConfig config = RequestConfig.custom()
|
||||||
@@ -109,7 +89,7 @@ public class UmsService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 4. 실행 (HttpClient 재사용)
|
// 4. 실행 (HttpClient 재사용)
|
||||||
try {
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||||
return httpClient.execute(httpPost, response -> {
|
return httpClient.execute(httpPost, response -> {
|
||||||
String body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
String body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
|
||||||
|
|
||||||
@@ -140,15 +120,4 @@ public class UmsService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@PreDestroy
|
|
||||||
public void destroy() {
|
|
||||||
try {
|
|
||||||
if (httpClient != null) {
|
|
||||||
httpClient.close();
|
|
||||||
logger.info("UMS HttpClient closed.");
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.error("Error closing UMS HttpClient", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user