Revert "HttpClientConnectionManager에서 SSLContext 생성시, entropy"

This reverts commit 640f1e4f1e.
This commit is contained in:
cho
2026-03-05 14:29:09 +09:00
parent 640f1e4f1e
commit 7ac995e5ef
@@ -1,15 +1,20 @@
package com.eactive.eai.custom.alarm.ums;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import javax.annotation.PreDestroy;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.config.RequestConfig;
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.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.HttpVersion;
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.springframework.stereotype.Component;
@@ -38,6 +43,24 @@ public class UmsService {
private final static String DEFAULT_API_KEY = "7cca6d58-e4f7-474d-9edd-525806bc99ff";
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 {
if (payload.getContent() == null) {
@@ -73,9 +96,6 @@ public class UmsService {
.setContentEncoding("UTF-8")
.build());
httpPost.setVersion(HttpVersion.HTTP_1_0);
httpPost.setHeader("Connection", "close");
// 3. 타임아웃 설정 (설정파일 값 적용)
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(Timeout.ofMilliseconds(connectionTimeoutMs))
@@ -89,7 +109,7 @@ public class UmsService {
}
// 4. 실행 (HttpClient 재사용)
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
try {
return httpClient.execute(httpPost, response -> {
String body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
@@ -120,4 +140,15 @@ 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);
}
}
}