KjbProperty 리팩토링
This commit is contained in:
@@ -3,6 +3,7 @@ package com.eactive.ext.kjb.ums;
|
||||
import com.eactive.apim.portal.template.entity.MessageCode;
|
||||
import com.eactive.apim.portal.template.entity.MessageRequest;
|
||||
import com.eactive.ext.kjb.common.KjbProperty;
|
||||
import com.eactive.ext.kjb.common.KjbPropertyHolder;
|
||||
import com.eactive.ext.kjb.common.KjbUmsException;
|
||||
import com.eactive.ext.kjb.ums.gson.IUmsGsonObject;
|
||||
import com.eactive.ext.kjb.ums.gson.UmsRequestPayload;
|
||||
@@ -39,39 +40,42 @@ public class KjbUmsService {
|
||||
private final static String CONTENT_TYPE = "application/json; charset=utf-8";
|
||||
private final static String AUTHORIZATION_FMT = "Bearer %s";
|
||||
|
||||
private final KjbProperty property;
|
||||
private final boolean isRealMode;
|
||||
private final CloseableHttpClient httpClient;
|
||||
private final RequestConfig requestConfig;
|
||||
private static volatile KjbUmsService instance;
|
||||
|
||||
private KjbUmsService() {
|
||||
// 싱글톤
|
||||
}
|
||||
|
||||
public KjbUmsService(KjbProperty property) {
|
||||
if (property == null) {
|
||||
log.error("[KJB-UMS] 파라미터 설정 되지 않음");
|
||||
throw new IllegalArgumentException("property is null");
|
||||
}
|
||||
|
||||
isRealMode = property.getUmsHostUrl() != null && StringUtils.isNotEmpty(property.getUmsHostUrl().trim());
|
||||
|
||||
if (isRealMode) {
|
||||
try {
|
||||
ValidationUtil.validateOrThrow(property);
|
||||
} catch (Exception e) {
|
||||
log.error("[KJB-UMS] 파라미터 설정 오류", e);
|
||||
throw e;
|
||||
public static KjbUmsService getInstance() {
|
||||
if (instance == null) {
|
||||
synchronized (KjbUmsService.class) {
|
||||
if (instance == null) {
|
||||
instance = new KjbUmsService();
|
||||
}
|
||||
}
|
||||
|
||||
httpClient = HttpClients.createDefault();
|
||||
requestConfig = RequestConfig.custom()
|
||||
.setConnectTimeout(Timeout.ofSeconds(property.getEaiBatchConnectionTimeout()))
|
||||
.setResponseTimeout(Timeout.ofSeconds(property.getEaiBatchResponseTimeout()))
|
||||
.build();
|
||||
} else {
|
||||
httpClient = HttpClients.createDefault();
|
||||
requestConfig = null;
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
this.property = property;
|
||||
private KjbProperty getProperty() {
|
||||
return KjbPropertyHolder.get();
|
||||
}
|
||||
|
||||
private boolean isRealMode() {
|
||||
String url = getProperty().getUmsHostUrl();
|
||||
return url != null && StringUtils.isNotEmpty(url.trim());
|
||||
}
|
||||
|
||||
private CloseableHttpClient createHttpClient() {
|
||||
return HttpClients.createDefault();
|
||||
}
|
||||
|
||||
private RequestConfig createRequestConfig() {
|
||||
KjbProperty prop = getProperty();
|
||||
return RequestConfig.custom()
|
||||
.setConnectTimeout(Timeout.ofSeconds(prop.getUmsConnectionTimeout()))
|
||||
.setResponseTimeout(Timeout.ofSeconds(prop.getUmsResponseTimeout()))
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -142,13 +146,14 @@ public class KjbUmsService {
|
||||
}
|
||||
|
||||
private UmsCallResult call(String url, UmsRequestPayload requestPayload) throws KjbUmsException {
|
||||
KjbProperty prop = getProperty();
|
||||
ExecutorService executor = Executors.newSingleThreadExecutor();
|
||||
Gson gson = new Gson();
|
||||
String json = gson.toJson(requestPayload);
|
||||
|
||||
HttpPost httpPost = new HttpPost(property.getUmsHostUrl() + url);
|
||||
HttpPost httpPost = new HttpPost(prop.getUmsHostUrl() + url);
|
||||
httpPost.setHeader("Content-Type", CONTENT_TYPE);
|
||||
httpPost.setHeader("Authorization", String.format(AUTHORIZATION_FMT, property.getUmsApiKey()));
|
||||
httpPost.setHeader("Authorization", String.format(AUTHORIZATION_FMT, prop.getUmsApiKey()));
|
||||
httpPost.setEntity(EntityBuilder.create().setText(json).setContentType(ContentType.APPLICATION_JSON).build());
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
@@ -161,52 +166,54 @@ public class KjbUmsService {
|
||||
}
|
||||
}
|
||||
|
||||
httpPost.setConfig(requestConfig);
|
||||
httpPost.setConfig(createRequestConfig());
|
||||
|
||||
if (isRealMode) {
|
||||
Future<UmsCallResult> future = executor.submit(() -> {
|
||||
return httpClient.execute(httpPost, res -> {
|
||||
try {
|
||||
UmsCallResult result;
|
||||
String body = EntityUtils.toString(res.getEntity());
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Response - {}", body);
|
||||
}
|
||||
result = gson.fromJson(body, UmsCallResult.class);
|
||||
result.setUmsMessageId(requestPayload.getMessageIdentityNo());
|
||||
result.setSuccess(true);
|
||||
return result;
|
||||
} catch (JsonSyntaxException e) {
|
||||
log.error("UMS Call error(JSON Syntax)", e);
|
||||
return UmsCallResult.builder()
|
||||
.umsMessageId(requestPayload.getMessageIdentityNo())
|
||||
.isSuccess(false)
|
||||
.throwable(e)
|
||||
.build();
|
||||
} catch (Exception e) {
|
||||
log.error("UMS Call error", e);
|
||||
return UmsCallResult.builder()
|
||||
.umsMessageId(requestPayload.getMessageIdentityNo())
|
||||
.isSuccess(false)
|
||||
.throwable(e)
|
||||
.build();
|
||||
} finally {
|
||||
log.debug("Response status - {}", res.getCode());
|
||||
if (isRealMode()) {
|
||||
try (CloseableHttpClient httpClient = createHttpClient()) {
|
||||
Future<UmsCallResult> future = executor.submit(() -> {
|
||||
return httpClient.execute(httpPost, res -> {
|
||||
try {
|
||||
UmsCallResult result;
|
||||
String body = EntityUtils.toString(res.getEntity());
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Response - {}", body);
|
||||
}
|
||||
result = gson.fromJson(body, UmsCallResult.class);
|
||||
result.setUmsMessageId(requestPayload.getMessageIdentityNo());
|
||||
result.setSuccess(true);
|
||||
return result;
|
||||
} catch (JsonSyntaxException e) {
|
||||
log.error("UMS Call error(JSON Syntax)", e);
|
||||
return UmsCallResult.builder()
|
||||
.umsMessageId(requestPayload.getMessageIdentityNo())
|
||||
.isSuccess(false)
|
||||
.throwable(e)
|
||||
.build();
|
||||
} catch (Exception e) {
|
||||
log.error("UMS Call error", e);
|
||||
return UmsCallResult.builder()
|
||||
.umsMessageId(requestPayload.getMessageIdentityNo())
|
||||
.isSuccess(false)
|
||||
.throwable(e)
|
||||
.build();
|
||||
} finally {
|
||||
log.debug("Response status - {}", res.getCode());
|
||||
|
||||
if (res.getCode() != 200) {
|
||||
Arrays.asList(res.getHeaders()).forEach(o -> {
|
||||
log.warn("[Header] {}: {}", o.getName(), o.getValue());
|
||||
});
|
||||
if (res.getCode() != 200) {
|
||||
Arrays.asList(res.getHeaders()).forEach(o -> {
|
||||
log.warn("[Header] {}: {}", o.getName(), o.getValue());
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
try {
|
||||
return future.get(property.getUmsTimeout(), TimeUnit.SECONDS);
|
||||
} catch (InterruptedException | ExecutionException | TimeoutException e) {
|
||||
log.error("EAI Batch call exception", e);
|
||||
throw new KjbUmsException("EAI Ums call failed", e);
|
||||
return future.get(prop.getUmsTimeout(), TimeUnit.SECONDS);
|
||||
} catch (InterruptedException | ExecutionException | TimeoutException | IOException e) {
|
||||
log.error("UMS call exception", e);
|
||||
throw new KjbUmsException("UMS call failed", e);
|
||||
} finally {
|
||||
executor.shutdown();
|
||||
}
|
||||
} else {
|
||||
log.warn("UMS URL 미지정으로 실제 호출을 진행하지 않음.");
|
||||
|
||||
Reference in New Issue
Block a user