이메일 발송 테스트 코드

This commit is contained in:
Rinjae
2025-11-10 11:00:37 +09:00
parent 4283c8b3fc
commit 5b354cffb1
9 changed files with 284 additions and 95 deletions
@@ -3,9 +3,13 @@ package com.eactive.ext.kjb.ums;
import com.eactive.apim.portal.portaluser.entity.PortalUser;
import com.eactive.apim.portal.template.entity.MessageRequest;
import com.eactive.apim.portal.template.repository.MessageRequestRepository;
import com.eactive.ext.kjb.common.KjbEaiBatchException;
import com.eactive.ext.kjb.common.KjbProperty;
import com.eactive.ext.kjb.eai.EaiBatchMessage;
import com.eactive.ext.kjb.eai.EaiBatchInterface;
import com.eactive.ext.kjb.eai.KjbEaiBatchModule;
import com.eactive.ext.kjb.safedb.KjbSafedbWrapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
@@ -28,6 +32,10 @@ public class EmailJob {
private final KjbProperty prop;
private final MessageRequest messageRequest;
private static final int READY = 1; // Temp 파일 생성 단계
private static final int BEFORE_SEND = 2;
private static final int SEND = 3;
private final Path tempFilePath;
private final Path eaiFilePath;
private final Path backupFilePath;
@@ -35,13 +43,15 @@ public class EmailJob {
private final MessageRequestRepository messageRequestRepository;
private final LocalDateTime now = LocalDateTime.now();
private int eaiPhase = 0;
private EmailJob(KjbProperty prop, MessageRequest messageRequest, MessageRequestRepository messageRequestRepository) {
this.messageRequest = messageRequest;
this.prop = prop;
String filename = messageRequest.getServiceId() + "_" + DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS").format(now);
String filename = messageRequest.getServiceId() + "_" + DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(now);
this.tempFilePath = Paths.get(prop.getUmsEaiBatchSendDir(), filename + ".tmp");
this.eaiFilePath = Paths.get(prop.getUmsEaiBatchSendDir(), filename);
this.backupFilePath = Paths.get(prop.getUmsEaiBatchBackupDir(), filename);
@@ -112,7 +122,7 @@ public class EmailJob {
@Transactional
public void sendEmail() {
public void sendEmail() throws KjbEaiBatchException {
if (messageRequestRepository != null) {
PortalUser user = messageRequest.getUser();
if (user != null) {
@@ -130,9 +140,11 @@ public class EmailJob {
log.debug("발송 성공 처리");
doSuccess();
} catch (KjbEaiBatchException e) {
doFail(e);
} catch (Exception e) {
log.error("이메일 발송 실패", e);
doFail();
log.error(e.getMessage(), e);
doFail(e);
} finally {
doDeleteEaiFile();
}
@@ -142,13 +154,24 @@ public class EmailJob {
// TODO: EAI 발송용 파일 삭제
}
private void doFail() {
private void doFail(Exception e) throws KjbEaiBatchException {
// TODO: EAI 발송 실패 처리
if (e instanceof KjbEaiBatchException) {
throw (KjbEaiBatchException) e;
} else {
throw new KjbEaiBatchException("이메일 발송 실패", e);
}
}
private void eaiBatchCall() throws IOException {
private void eaiBatchCall() throws KjbEaiBatchException {
KjbEaiBatchModule eaiBatchModule = new KjbEaiBatchModule(prop);
EaiBatchMessage result = eaiBatchModule.call(prop.getUmsEaiBatchInterfaceId(), eaiFilePath.getFileName().toString());
EaiBatchInterface eaiBatchInterface = EaiBatchInterface.builder()
.id(prop.getUmsEaiBatchInterfaceId())
.sendDir(prop.getUmsEaiBatchSendDir())
.backupDir(prop.getUmsEaiBatchBackupDir())
.errorDir(prop.getUmsEaiBatchErrorDir())
.build();
EaiBatchMessage result = eaiBatchModule.call(eaiBatchInterface, eaiFilePath.getFileName().toString());
if (!"1".equals(result.getResultCode())) {
log.error("EAI Batch call failed: {}", result);
@@ -159,19 +182,11 @@ public class EmailJob {
}
private void doSuccess() {
// 백업 디렉토리로 복사
try {
Files.copy(eaiFilePath, backupFilePath);
log.debug("Copied EAI file to backup: {} -> {}", eaiFilePath.toString(), backupFilePath.toString());
} catch (IOException e) {
log.error("Cannot copy EAI file to backup: {} -> {}", eaiFilePath.toString(), backupFilePath.toString(), e);
}
// 딱히 뭐 할 거 없음.
}
public void doCreateNewFile() {
final String filename = "EMAIL_" + messageRequest.getId() + ".json";
if (eaiFilePath.toFile().exists()) {
log.error("Mail File exists for ID: {}", messageRequest.getId());
throw new IllegalStateException("Mail File exists for ID: " + messageRequest.getId());
@@ -210,29 +225,48 @@ public class EmailJob {
} catch (IOException e) {
log.error("Cannot write JSON to file: {}", eaiFilePath.toString(), e);
throw new RuntimeException(e);
} finally {
// 임시 파일이 남아 있다면 오류 상황이므로 에러 경로로 이동
if (Files.exists(tempFilePath)) {
try {
Files.move(tempFilePath, Paths.get(prop.getUmsEaiBatchErrorDir(), tempFilePath.getFileName().toString()));
} catch (IOException e) {
log.warn("임시 파일 저장시 실패 하여 에러 경로로 옮기는 도중 에러 발생", e);
}
}
}
}
public String generateJSON(boolean isPretty) {
validateMessageRequest(messageRequest, true, true);
KjbSafedbWrapper safedb = KjbSafedbWrapper.getInstance();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("MAKE_DATE", DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDateTime.now()));
jsonObject.addProperty("MAKE_SEQNO", "0001");
jsonObject.addProperty("CAMP_ID", messageRequest.getServiceId()); // 전자금융 고객이메일 발송 업무ID (SERVICE_ID)
jsonObject.addProperty("ID", messageRequest.getUmsUid()); // 고객 ID
jsonObject.addProperty("EMAIL", messageRequest.getEmail());
jsonObject.addProperty("EMAIL", safedb.encryptNotRnnoString(messageRequest.getEmail()));
jsonObject.addProperty("NAME", messageRequest.getUsername());
jsonObject.addProperty("ENCKEY", ""); // 보안메일용으로 기입 불필요
Gson gson = new Gson();
log.debug("Template string to json: {}", messageRequest.getMessage());
JsonObject contentJSon = gson.fromJson(messageRequest.getMessage(), JsonObject.class);
contentJSon.entrySet().forEach(entry -> {
jsonObject.add(entry.getKey(), entry.getValue());
});
jsonObject.addProperty("SUBJECT", messageRequest.getSubject());
jsonObject.addProperty("MESSAGE", messageRequest.getMessage());
// jsonObject.addProperty("SUBJECT", messageRequest.getSubject());
// jsonObject.addProperty("MESSAGE", messageRequest.getMessage());
String jsonString;
if (isPretty) {
return new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
jsonString = new GsonBuilder().setPrettyPrinting().create().toJson(jsonObject);
} else {
return jsonObject.toString();
jsonString = jsonObject.toString();
}
return jsonString;
}
}