UMS 코드 개선

This commit is contained in:
Rinjae
2025-11-10 13:45:55 +09:00
parent 1a426270fe
commit 9ec3d92ad6
3 changed files with 92 additions and 113 deletions
+1
View File
@@ -15,5 +15,6 @@ eapim-admin 에서 포함하여 테스트 이용시 느린 부팅 속도 떄문
GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa-jenkins' git pull origin jenkins_with_weblogic
# test run
KJB_EAPIM_TEST_ENABLED=TRUE kjb-gradle.sh :eapim-admin-kjb:test --tests "com.eactive.ext.kjb.ums.test.UmsTest.testSms" -Psafedb
KJB_EAPIM_TEST_ENABLED=TRUE kjb-gradle.sh :eapim-admin-kjb:test --tests "com.eactive.ext.kjb.ums.test.UmsTest.testEmailInServer" -Psafedb
```
@@ -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.KjbUmsException;
import com.eactive.ext.kjb.ums.gson.IUmsGsonObject;
import com.eactive.ext.kjb.ums.gson.UmsRequestPayload;
import com.eactive.ext.kjb.ums.gson.VerifyPhoneUmsTemplate;
@@ -24,14 +25,11 @@ import org.apache.hc.core5.util.Timeout;
import org.springframework.util.StreamUtils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiConsumer;
import java.util.concurrent.*;
@Slf4j
public class KjbUmsService {
@@ -82,7 +80,7 @@ public class KjbUmsService {
}
public boolean send(MessageRequest messageRequest) {
public boolean send(MessageRequest messageRequest) throws KjbUmsException {
if (messageRequest == null) {
throw new IllegalArgumentException("messageRequest is null");
}
@@ -106,7 +104,7 @@ public class KjbUmsService {
return true;
}
public String sendKakaoAlimTalk(String guid, UmsBizWorkCode bizWorkCode, String cpno, IUmsGsonObject content) {
public String sendKakaoAlimTalk(String guid, UmsBizWorkCode bizWorkCode, String cpno, IUmsGsonObject content) throws KjbUmsException {
if (content == null) {
throw new IllegalArgumentException("content is null");
}
@@ -130,102 +128,84 @@ public class KjbUmsService {
throw ex;
}
return callUms(SEND_URL, payload, (result, ex) -> {
if (ex != null) {
log.error("UMS 발송 실패", ex);
throw new RuntimeException(ex);
}
UmsCallResult result = call(SEND_URL, payload);
if (!result.isSuccess()) {
throw new KjbUmsException(result.getThrowable().getMessage());
}
if (result == null || !result.isSuccess()) {
log.error("UMS 발송 실패(return) - {}", JsonUtil.toPrettyString(result));
throw new RuntimeException("UMS 발송실패 - " + JsonUtil.toPrettyString(result != null ? result.getThrowable().getMessage() : "UMS 발송실패 - 알수 없는 오류"));
}
log.info("UMS Call success. ({})", result);
}).join().getUmsMessageId();
return result.getUmsMessageId();
}
private CompletableFuture<UmsCallResult> callUms(@SuppressWarnings("SameParameterValue") String url, UmsRequestPayload requestPayload,
BiConsumer<UmsCallResult, Throwable> callback) {
return CompletableFuture
.supplyAsync(() -> {
Gson gson = new Gson();
String json = gson.toJson(requestPayload);
private UmsCallResult call(String url, UmsRequestPayload requestPayload) throws KjbUmsException {
ExecutorService executor = Executors.newSingleThreadExecutor();
Gson gson = new Gson();
String json = gson.toJson(requestPayload);
HttpPost httpPost = new HttpPost(property.getUmsHostUrl() + url);
httpPost.setHeader("Content-Type", CONTENT_TYPE);
httpPost.setHeader("Authorization", String.format(AUTHORIZATION_FMT, property.getUmsApiKey()));
httpPost.setEntity(EntityBuilder.create().setText(json).setContentType(ContentType.APPLICATION_JSON).build());
HttpPost httpPost = new HttpPost(property.getUmsHostUrl() + url);
httpPost.setHeader("Content-Type", CONTENT_TYPE);
httpPost.setHeader("Authorization", String.format(AUTHORIZATION_FMT, property.getUmsApiKey()));
httpPost.setEntity(EntityBuilder.create().setText(json).setContentType(ContentType.APPLICATION_JSON).build());
if (log.isDebugEnabled()) {
try {
log.debug("Header - {}", httpPost.getHeader("Content-Type"));
log.debug("Header - {}", httpPost.getHeader("Authorization"));
log.debug("Request Body : {}", StreamUtils.copyToString(httpPost.getEntity().getContent(), Charset.defaultCharset()));
} catch (ProtocolException | IOException e) {
throw new RuntimeException(e);
}
}
if (log.isDebugEnabled()) {
try {
log.debug("Header - {}", httpPost.getHeader("Content-Type"));
log.debug("Header - {}", httpPost.getHeader("Authorization"));
log.debug("Request Body : {}", StreamUtils.copyToString(httpPost.getEntity().getContent(), Charset.defaultCharset()));
} catch (ProtocolException | IOException e) {
throw new RuntimeException(e);
}
}
httpPost.setConfig(requestConfig);
AtomicReference<UmsCallResult> result = new AtomicReference<>();
if (isRealMode) {
try {
httpClient.execute(httpPost, res -> {
try {
String body = EntityUtils.toString(res.getEntity());
if (log.isDebugEnabled()) {
log.debug("Response - {}", body);
}
result.set(gson.fromJson(body, UmsCallResult.class));
result.get().setUmsMessageId(requestPayload.getMessageIdentityNo());
result.get().setSuccess(true);
return result;
} finally {
log.debug("Response status - {}", res.getCode());
if (res.getCode() != 200) {
Arrays.asList(res.getHeaders()).forEach(o -> {
log.warn("[Header] {}: {}", o.getName(), o.getValue());
});
}
}
});
} catch (JsonSyntaxException e) {
log.error("UMS Call error(JSON Syntax)", e);
result.set(
UmsCallResult.builder()
.umsMessageId(requestPayload.getMessageIdentityNo())
.isSuccess(false)
.throwable(e)
.build()
);
} catch (Exception e) {
log.error("UMS Call error", e);
result.set(
UmsCallResult.builder()
.umsMessageId(requestPayload.getMessageIdentityNo())
.isSuccess(false)
.throwable(e)
.build()
);
}
} else {
log.warn("UMS URL 미지정으로 실제 호출을 진행하지 않음.");
result.set(UmsCallResult.builder().isSuccess(true).build());
}
httpPost.setConfig(requestConfig);
if (isRealMode) {
Future<UmsCallResult> future = executor.submit(() -> {
return httpClient.execute(httpPost, res -> {
try {
log.debug("UMS Call URI : {}", httpPost.getUri().toASCIIString());
log.debug("UMS Call Request payload : {}", json);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
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());
return result.get();
})
.whenComplete(callback);
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);
}
} else {
log.warn("UMS URL 미지정으로 실제 호출을 진행하지 않음.");
return UmsCallResult.builder().isSuccess(true).build();
}
}
}
@@ -4,10 +4,12 @@ import com.eactive.apim.portal.template.entity.MessageCode;
import com.eactive.apim.portal.template.entity.MessageRequest;
import com.eactive.ext.kjb.common.KjbEaiBatchException;
import com.eactive.ext.kjb.common.KjbProperty;
import com.eactive.ext.kjb.common.KjbUmsException;
import com.eactive.ext.kjb.ums.KjbEmailSendModule;
import com.eactive.ext.kjb.ums.KjbUmsService;
import com.eactive.ext.kjb.utils.OsUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@@ -23,16 +25,22 @@ public class UmsTest {
@BeforeAll
public static void init() {
prop = new KjbProperty();
prop.setEaiBatchUrl("http://172.31.32.111:10230/BATAppWeb/EaiBatCall");
prop.setUmsEaiBatchInterfaceId("UAGFF00001UIE");
prop.setUmsHostUrl("http://172.31.35.144:9021");
prop.setUmsApiKey("7cca6d58-e4f7-474d-9edd-525806bc99ff");
if (OsUtil.isWindows()) {
prop.setUmsEaiBatchSendDir("c:\\kjb-logs\\sendmail\\snd");
prop.setUmsEaiBatchBackupDir("c:\\kjb-logs\\sendmail\\bak");
prop.setUmsEaiBatchErrorDir("c:\\kjb-logs\\sendmail\\error");
}
if (OsUtil.isLinux()) {
prop.setUmsEaiBatchSendDir("/Data/eapim/portal/sendmail/snd");
prop.setUmsEaiBatchBackupDir("/Data/eapim/portal/sendmail/bak");
prop.setUmsEaiBatchErrorDir("/Data/eapim/portal/sendmail/error");
}
}
@@ -42,9 +50,6 @@ public class UmsTest {
"true".equalsIgnoreCase(System.getenv("KJB_EAPIM_TEST_ENABLED")),
"OS 환경변수 내 KJB_EAPIM_TEST_ENABLED=TRUE 설정되어 있어야 동작함");
prop.setUmsHostUrl("http://172.31.35.144:9021");
prop.setUmsApiKey("7cca6d58-e4f7-474d-9edd-525806bc99ff");
MessageRequest messageRequest = new MessageRequest();
messageRequest.setId(UUID.randomUUID().toString());
messageRequest.setMessageCode(MessageCode.USER_VERIFICATION_MOBILEPHONE);
@@ -52,10 +57,17 @@ public class UmsTest {
messageRequest.setEmail("dearmai@dearmai.net");
messageRequest.setUsername("김광은");
messageRequest.setSubject("제목 - test");
messageRequest.setMessage("junit test");
messageRequest.setMessage("{\"ATHN_NO\"; \"369369\"}");
KjbUmsService umsService = new KjbUmsService(prop);
umsService.send(messageRequest);
try {
boolean result = umsService.send(messageRequest);
} catch (KjbUmsException e) {
log.error(e.getMessage(), e);
Assertions.fail(e);
return;
}
log.info("done");
}
@@ -66,13 +78,6 @@ public class UmsTest {
"true".equalsIgnoreCase(System.getenv("KJB_EAPIM_TEST_ENABLED")),
"OS 환경변수 내 KJB_EAPIM_TEST_ENABLED=TRUE 설정되어 있어야 동작함");
prop.setEaiBatchUrl("http://172.31.32.111:10230/BATAppWeb/EaiBatCall");
prop.setEaiBatchUrl("");
prop.setUmsEaiBatchSendDir("c:\\kjb-logs\\sendmail\\snd");
prop.setUmsEaiBatchBackupDir("c:\\kjb-logs\\sendmail\\bak");
prop.setUmsEaiBatchErrorDir("c:\\kjb-logs\\sendmail\\error");
prop.setUmsEaiBatchInterfaceId("UAGFF00001UIE");
System.getProperties().keySet().forEach(key -> {
log.info("{}: {}", key, System.getProperty((String) key));
@@ -105,13 +110,6 @@ public class UmsTest {
"true".equalsIgnoreCase(System.getenv("KJB_EAPIM_TEST_ENABLED")),
"OS 환경변수 내 KJB_EAPIM_TEST_ENABLED=TRUE 설정되어 있어야 동작함");
prop.setEaiBatchUrl("http://172.31.32.111:10230/BATAppWeb/EaiBatCall");
prop.setUmsEaiBatchSendDir("/Data/eapim/portal/sendmail/snd");
prop.setUmsEaiBatchBackupDir("/Data/eapim/portal/sendmail/bak");
prop.setUmsEaiBatchErrorDir("/Data/eapim/portal/sendmail/error");
prop.setUmsEaiBatchInterfaceId("UAGFF00001UIE");
System.getProperties().keySet().forEach(key -> {
log.info("{}: {}", key, System.getProperty((String) key));
});