KjbProperty 리팩토링
This commit is contained in:
@@ -2,7 +2,7 @@ package com.eactive.ext.kjb.obp;
|
||||
|
||||
import com.eactive.ext.kjb.common.KjbObpException;
|
||||
import com.eactive.ext.kjb.common.KjbProperty;
|
||||
import lombok.Getter;
|
||||
import com.eactive.ext.kjb.common.KjbPropertyHolder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
@@ -10,30 +10,29 @@ import org.apache.commons.lang3.StringUtils;
|
||||
public class KjbObpModule {
|
||||
private static final String FAKE_PARTNER_CODE = "999999-99";
|
||||
|
||||
private static KjbObpModule _instance;
|
||||
private static volatile KjbObpModule instance;
|
||||
|
||||
@Getter
|
||||
private final KjbProperty property;
|
||||
private final ObpClient client;
|
||||
|
||||
public static synchronized KjbObpModule getInstance(KjbProperty kjbProperty) {
|
||||
if (_instance == null) {
|
||||
_instance = new KjbObpModule(kjbProperty);
|
||||
} else {
|
||||
if (kjbProperty.equals(_instance.getProperty())) {
|
||||
log.debug("KjbProperty 파라미터가 변경되어 KjbObpModule 인스턴스 재 생성");
|
||||
_instance = new KjbObpModule(kjbProperty);
|
||||
}
|
||||
}
|
||||
return _instance;
|
||||
private KjbObpModule() {
|
||||
// 싱글톤
|
||||
}
|
||||
|
||||
private KjbObpModule(KjbProperty kjbProperty) {
|
||||
if (kjbProperty == null) {
|
||||
throw new IllegalArgumentException("KjbProperty cannot be null");
|
||||
public static KjbObpModule getInstance() {
|
||||
if (instance == null) {
|
||||
synchronized (KjbObpModule.class) {
|
||||
if (instance == null) {
|
||||
instance = new KjbObpModule();
|
||||
}
|
||||
}
|
||||
}
|
||||
this.property = kjbProperty;
|
||||
this.client = ObpClient.getInstance(kjbProperty);
|
||||
return instance;
|
||||
}
|
||||
|
||||
private static KjbProperty getProperty() {
|
||||
return KjbPropertyHolder.get();
|
||||
}
|
||||
|
||||
private ObpClient getClient() {
|
||||
return ObpClient.getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,13 +52,15 @@ public class KjbObpModule {
|
||||
throw new IllegalArgumentException("BizNo must be 10 digits. (input: " + bizNo + ")");
|
||||
}
|
||||
|
||||
ObpClient client = getClient();
|
||||
|
||||
// 가상모드: URL 미설정 시 가상 파트너코드 반환
|
||||
if (!client.isRealMode()) {
|
||||
log.info("[OBP] 가상모드 - 가상 partnerCode 반환: {}", FAKE_PARTNER_CODE);
|
||||
return FAKE_PARTNER_CODE;
|
||||
}
|
||||
|
||||
String path = String.format(property.getObpUrlOrganizationFmt(), bizNo);
|
||||
String path = String.format(getProperty().getObpUrlOrganizationFmt(), bizNo);
|
||||
log.debug("[OBP] queryPartnerCode - bizNo: {}, path: {}", bizNo, path);
|
||||
|
||||
ObpResponse response = client.get(path);
|
||||
@@ -81,11 +82,4 @@ public class KjbObpModule {
|
||||
|
||||
return org.getPartnerCode();
|
||||
}
|
||||
|
||||
/**
|
||||
* ObpClient 반환 (직접 API 호출용)
|
||||
*/
|
||||
public ObpClient getClient() {
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.eactive.ext.kjb.obp;
|
||||
|
||||
import com.eactive.ext.kjb.common.KjbProperty;
|
||||
import lombok.Getter;
|
||||
import com.eactive.ext.kjb.common.KjbPropertyHolder;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||
@@ -22,46 +22,40 @@ import java.util.concurrent.*;
|
||||
|
||||
@Slf4j
|
||||
public class ObpClient {
|
||||
private static ObpClient _instance;
|
||||
private static volatile ObpClient instance;
|
||||
|
||||
@Getter
|
||||
private final KjbProperty property;
|
||||
private final CloseableHttpClient httpClient;
|
||||
private final RequestConfig requestConfig;
|
||||
private final boolean isRealMode;
|
||||
|
||||
public synchronized static ObpClient getInstance(KjbProperty kjbProperty) {
|
||||
if (_instance == null) {
|
||||
_instance = new ObpClient(kjbProperty);
|
||||
} else {
|
||||
if (kjbProperty.equals(_instance.getProperty())) {
|
||||
log.debug("KjbProperty 파라미터가 변경되어 ObpClient 인스턴스 재 생성");
|
||||
_instance = new ObpClient(kjbProperty);
|
||||
}
|
||||
}
|
||||
|
||||
return _instance;
|
||||
private ObpClient() {
|
||||
// 싱글톤
|
||||
}
|
||||
|
||||
private ObpClient(KjbProperty kjbProperty) {
|
||||
if (kjbProperty == null) {
|
||||
throw new IllegalArgumentException("KjbProperty cannot be null");
|
||||
public static ObpClient getInstance() {
|
||||
if (instance == null) {
|
||||
synchronized (ObpClient.class) {
|
||||
if (instance == null) {
|
||||
instance = new ObpClient();
|
||||
}
|
||||
}
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
this.property = kjbProperty;
|
||||
this.isRealMode = StringUtils.isNotEmpty(kjbProperty.getObpUrl());
|
||||
private static KjbProperty getProperty() {
|
||||
return KjbPropertyHolder.get();
|
||||
}
|
||||
|
||||
this.requestConfig = RequestConfig.custom()
|
||||
.setConnectTimeout(Timeout.ofSeconds(kjbProperty.getObpConnectionTimeout()))
|
||||
.setResponseTimeout(Timeout.ofSeconds(kjbProperty.getObpResponseTimeout()))
|
||||
/**
|
||||
* 실제 모드 여부 (URL이 설정되어 있는지 확인)
|
||||
*/
|
||||
public boolean isRealMode() {
|
||||
return StringUtils.isNotEmpty(getProperty().getObpUrl());
|
||||
}
|
||||
|
||||
private RequestConfig createRequestConfig() {
|
||||
KjbProperty prop = getProperty();
|
||||
return RequestConfig.custom()
|
||||
.setConnectTimeout(Timeout.ofSeconds(prop.getObpConnectionTimeout()))
|
||||
.setResponseTimeout(Timeout.ofSeconds(prop.getObpResponseTimeout()))
|
||||
.build();
|
||||
|
||||
if (isRealMode) {
|
||||
this.httpClient = HttpClients.createDefault();
|
||||
} else {
|
||||
this.httpClient = null;
|
||||
log.warn("[OBP] URL이 설정되지 않음. 실제 OBP 연동 없이 동작함");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,11 +74,11 @@ public class ObpClient {
|
||||
* @return ObpResponse
|
||||
*/
|
||||
public ObpResponse get(String path, Map<String, String> additionalHeaders) {
|
||||
String url = property.getObpUrl() + path;
|
||||
String url = getProperty().getObpUrl() + path;
|
||||
HttpGet httpGet = new HttpGet(url);
|
||||
applyDefaultHeaders(httpGet);
|
||||
applyAdditionalHeaders(httpGet, additionalHeaders);
|
||||
httpGet.setConfig(requestConfig);
|
||||
httpGet.setConfig(createRequestConfig());
|
||||
|
||||
return execute(httpGet);
|
||||
}
|
||||
@@ -107,11 +101,11 @@ public class ObpClient {
|
||||
* @return ObpResponse
|
||||
*/
|
||||
public ObpResponse post(String path, String jsonBody, Map<String, String> additionalHeaders) {
|
||||
String url = property.getObpUrl() + path;
|
||||
String url = getProperty().getObpUrl() + path;
|
||||
HttpPost httpPost = new HttpPost(url);
|
||||
applyDefaultHeaders(httpPost);
|
||||
applyAdditionalHeaders(httpPost, additionalHeaders);
|
||||
httpPost.setConfig(requestConfig);
|
||||
httpPost.setConfig(createRequestConfig());
|
||||
|
||||
if (StringUtils.isNotEmpty(jsonBody)) {
|
||||
httpPost.setEntity(EntityBuilder.create()
|
||||
@@ -124,9 +118,10 @@ public class ObpClient {
|
||||
}
|
||||
|
||||
private void applyDefaultHeaders(HttpUriRequestBase request) {
|
||||
KjbProperty prop = getProperty();
|
||||
request.setHeader("Content-Type", "application/json; charset=utf-8");
|
||||
request.setHeader(property.getObpTrustSystemKey(), property.getObpTrustSystemValue());
|
||||
request.setHeader(property.getObpPartnerCodeKey(), property.getObpPartnerCodeValue());
|
||||
request.setHeader(prop.getObpTrustSystemKey(), prop.getObpTrustSystemValue());
|
||||
request.setHeader(prop.getObpPartnerCodeKey(), prop.getObpPartnerCodeValue());
|
||||
}
|
||||
|
||||
private void applyAdditionalHeaders(HttpUriRequestBase request, Map<String, String> additionalHeaders) {
|
||||
@@ -136,7 +131,7 @@ public class ObpClient {
|
||||
}
|
||||
|
||||
private ObpResponse execute(HttpUriRequestBase request) {
|
||||
if (!isRealMode) {
|
||||
if (!isRealMode()) {
|
||||
log.warn("[OBP] URL 미설정으로 실제 호출하지 않음. 요청: {} {}", request.getMethod(), request.getRequestUri());
|
||||
ObpResponse mockResponse = new ObpResponse();
|
||||
mockResponse.setStatus(200);
|
||||
@@ -146,7 +141,7 @@ public class ObpClient {
|
||||
|
||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
|
||||
try {
|
||||
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
||||
log.debug("[OBP] 요청: {} {}", request.getMethod(), request.getRequestUri());
|
||||
|
||||
Future<ObpResponse> future = executor.submit(() ->
|
||||
@@ -162,7 +157,7 @@ public class ObpClient {
|
||||
})
|
||||
);
|
||||
|
||||
ObpResponse response = future.get(property.getObpTimeout(), TimeUnit.SECONDS);
|
||||
ObpResponse response = future.get(getProperty().getObpTimeout(), TimeUnit.SECONDS);
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("[OBP] 응답: status={}, body={}", response.getStatus(), response.getBody());
|
||||
@@ -170,7 +165,7 @@ public class ObpClient {
|
||||
|
||||
return response;
|
||||
|
||||
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
||||
} catch (InterruptedException | ExecutionException | TimeoutException | java.io.IOException e) {
|
||||
log.error("[OBP] 호출 실패: {} {}", request.getMethod(), request.getRequestUri(), e);
|
||||
ObpResponse errorResponse = new ObpResponse();
|
||||
errorResponse.setStatus(-1);
|
||||
@@ -180,11 +175,4 @@ public class ObpClient {
|
||||
executor.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 실제 모드 여부
|
||||
*/
|
||||
public boolean isRealMode() {
|
||||
return isRealMode;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user