refactor: AOP 제외 및 adapt.history 내부를 100% 이벤트 리스너 아키텍처로 통일 (12/12 테스트 통과)
This commit is contained in:
parent
c17ee780e9
commit
d2f70e4ebc
5
pom.xml
5
pom.xml
@ -30,11 +30,6 @@
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-tx</artifactId>
|
||||
|
||||
@ -1,20 +1,25 @@
|
||||
package com.bccard.tsp.adapt.bxi.mci.impl;
|
||||
|
||||
import com.bccard.tsp.adapt.bxi.mci.MciClient;
|
||||
import com.bccard.tsp.adapt.history.trans.event.CommLogEvent;
|
||||
import com.bccard.tsp.adapt.history.trans.vo.CommLogVo;
|
||||
import com.bccard.tsp.common.Result;
|
||||
import com.bccard.tsp.common.mci.BxiHeaderIn;
|
||||
import com.bccard.tsp.common.mci.BxiMessage;
|
||||
import com.bccard.tsp.common.mci.TspRequestComponent;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* [BXI 기반 MCI 통신 클라이언트 구현체]
|
||||
* MciClient 인터페이스의 BXI 프로토콜 구현체로서,
|
||||
* TspRequestComponent를 통해 실제 BXI 전문 패킷 생성 및 송수신을 수행한다.
|
||||
* 통신 수행 후 CommLogEvent를 발행하여 통신 이력을 이벤트 리스너를 통해 비동기/동기로 적재한다.
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@ -22,6 +27,7 @@ import java.util.Map;
|
||||
public class BxiMciClient implements MciClient {
|
||||
|
||||
private final TspRequestComponent tspRequestComponent;
|
||||
private final ApplicationEventPublisher eventPublisher;
|
||||
|
||||
/**
|
||||
* BXI 표준 헤더 조립, 직렬화, 네트워크 전송, 응답 역직렬화(toVo)까지를 일괄 수행한다.
|
||||
@ -37,30 +43,75 @@ public class BxiMciClient implements MciClient {
|
||||
if (reqVo == null) {
|
||||
return Result.fail("BXI_REQ_NULL", intfId + " 요청 데이터가 null입니다.");
|
||||
}
|
||||
|
||||
LocalDateTime reqDtim = LocalDateTime.now();
|
||||
long startTime = System.currentTimeMillis();
|
||||
String reqJson = null;
|
||||
String rspJson = null;
|
||||
String status = "SUCCESS";
|
||||
String resCode = "0000";
|
||||
String resMsg = null;
|
||||
|
||||
try {
|
||||
// 1. BXI 헤더 조립 및 JSON 직렬화
|
||||
BxiHeaderIn headerIn = BxiHeaderIn.builder()
|
||||
.ifId(intfId)
|
||||
.usrId(usrId)
|
||||
.build();
|
||||
String reqJson = tspRequestComponent.makeBxiJson(headerIn, reqVo);
|
||||
reqJson = tspRequestComponent.makeBxiJson(headerIn, reqVo);
|
||||
if (reqJson == null || reqJson.trim().isEmpty()) {
|
||||
return Result.fail("BXI_REQ_EMPTY", "요청 JSON 데이터가 비어있습니다.");
|
||||
status = "FAIL";
|
||||
resCode = "BXI_REQ_EMPTY";
|
||||
resMsg = "요청 JSON 데이터가 비어있습니다.";
|
||||
return Result.fail(resCode, resMsg);
|
||||
}
|
||||
|
||||
// 2. 네트워크 송수신
|
||||
String rspJson = tspRequestComponent.sendMci(reqJson);
|
||||
rspJson = tspRequestComponent.sendMci(reqJson);
|
||||
|
||||
// 3. 응답 역직렬화
|
||||
Map<String, Object> parsedMap = BxiMessage.parseMessage(rspJson);
|
||||
RES out = BxiMessage.toVo(parsedMap, resClass);
|
||||
if (out == null) {
|
||||
return Result.fail("MCI_COMM_ERR", intfId + " 응답 데이터 변환 결과가 null입니다.");
|
||||
status = "FAIL";
|
||||
resCode = "MCI_COMM_ERR";
|
||||
resMsg = intfId + " 응답 데이터 변환 결과가 null입니다.";
|
||||
return Result.fail(resCode, resMsg);
|
||||
}
|
||||
return Result.ok(out);
|
||||
} catch (Exception e) {
|
||||
status = "FAIL";
|
||||
resCode = "MCI_COMM_ERR";
|
||||
resMsg = intfId + " 통신/파싱 실패: " + e.getMessage();
|
||||
log.error("[BxiMciClient] BXI 대외 통신 처리 실패 - intfId={}", intfId, e);
|
||||
return Result.fail("MCI_COMM_ERR", intfId + " 통신/파싱 실패: " + e.getMessage(), e);
|
||||
return Result.fail(resCode, resMsg, e);
|
||||
} finally {
|
||||
long elapsedTimeMs = System.currentTimeMillis() - startTime;
|
||||
LocalDateTime resDtim = LocalDateTime.now();
|
||||
publishCommLog(intfId, usrId, reqJson, rspJson, status, resCode, resMsg, elapsedTimeMs, reqDtim, resDtim);
|
||||
}
|
||||
}
|
||||
|
||||
private void publishCommLog(String intfId, String usrId, Object reqPayload, Object resPayload,
|
||||
String status, String resCode, String resMessage,
|
||||
long elapsedTimeMs, LocalDateTime reqDtim, LocalDateTime resDtim) {
|
||||
try {
|
||||
CommLogVo logVo = CommLogVo.builder()
|
||||
.intfId(intfId)
|
||||
.usrId(usrId)
|
||||
.reqPayload(reqPayload)
|
||||
.resPayload(resPayload)
|
||||
.status(status)
|
||||
.resCode(resCode)
|
||||
.resMessage(resMessage)
|
||||
.elapsedTimeMs(elapsedTimeMs)
|
||||
.reqDtim(reqDtim)
|
||||
.resDtim(resDtim)
|
||||
.build();
|
||||
|
||||
eventPublisher.publishEvent(new CommLogEvent(logVo));
|
||||
} catch (Exception e) {
|
||||
log.warn("[BxiMciClient] 통신 로그 이벤트 발행 실패 - intfId={}", intfId, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,92 +0,0 @@
|
||||
package com.bccard.tsp.adapt.history.trans.aspect;
|
||||
|
||||
import com.bccard.tsp.adapt.history.trans.event.CommLogEvent;
|
||||
import com.bccard.tsp.adapt.history.trans.vo.CommLogVo;
|
||||
import com.bccard.tsp.common.Result;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* [대외 통신 비침습적 AOP 로깅 Aspect]
|
||||
* - 기존 BXI 등 통신 어댑터 패키지 코드를 전혀 오염시키지 않고,
|
||||
* MciClient.call(..) 메서드 호출을 가로채 통신 시간, 요청/응답 전문, 결과를 CommLogEvent로 발행한다.
|
||||
*/
|
||||
@Slf4j
|
||||
@Aspect
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class CommLogAspect {
|
||||
|
||||
private final ApplicationEventPublisher eventPublisher;
|
||||
|
||||
/**
|
||||
* MciClient 구현체의 call(..) 메서드 실행 전후를 가로채 통신 로그를 비동기/동기로 발행한다.
|
||||
*/
|
||||
@Around("execution(* com.bccard.tsp.adapt.bxi..*Client.call(..)) && args(intfId, usrId, reqVo, resClass)")
|
||||
public Object logMciCommunication(ProceedingJoinPoint joinPoint, String intfId, String usrId, Object reqVo, Class<?> resClass) throws Throwable {
|
||||
LocalDateTime reqDtim = LocalDateTime.now();
|
||||
long startTime = System.currentTimeMillis();
|
||||
Object result = null;
|
||||
Throwable thrownException = null;
|
||||
|
||||
try {
|
||||
result = joinPoint.proceed();
|
||||
return result;
|
||||
} catch (Throwable t) {
|
||||
thrownException = t;
|
||||
throw t;
|
||||
} finally {
|
||||
long elapsedTimeMs = System.currentTimeMillis() - startTime;
|
||||
LocalDateTime resDtim = LocalDateTime.now();
|
||||
|
||||
publishLog(intfId, usrId, reqVo, result, thrownException, elapsedTimeMs, reqDtim, resDtim);
|
||||
}
|
||||
}
|
||||
|
||||
private void publishLog(String intfId, String usrId, Object reqVo, Object result,
|
||||
Throwable thrownException, long elapsedTimeMs,
|
||||
LocalDateTime reqDtim, LocalDateTime resDtim) {
|
||||
try {
|
||||
String status = "SUCCESS";
|
||||
String resCode = "0000";
|
||||
String resMessage = null;
|
||||
|
||||
if (thrownException != null) {
|
||||
status = "FAIL";
|
||||
resCode = "EXC_ERR";
|
||||
resMessage = thrownException.getMessage();
|
||||
} else if (result instanceof Result) {
|
||||
Result<?> res = (Result<?>) result;
|
||||
if (!res.isSuccess()) {
|
||||
status = "FAIL";
|
||||
resCode = res.getErrorCode();
|
||||
resMessage = res.getErrorMessage();
|
||||
}
|
||||
}
|
||||
|
||||
CommLogVo logVo = CommLogVo.builder()
|
||||
.intfId(intfId)
|
||||
.usrId(usrId)
|
||||
.reqPayload(reqVo)
|
||||
.resPayload(result)
|
||||
.status(status)
|
||||
.resCode(resCode)
|
||||
.resMessage(resMessage)
|
||||
.elapsedTimeMs(elapsedTimeMs)
|
||||
.reqDtim(reqDtim)
|
||||
.resDtim(resDtim)
|
||||
.build();
|
||||
|
||||
eventPublisher.publishEvent(new CommLogEvent(logVo));
|
||||
} catch (Exception e) {
|
||||
log.warn("[CommLogAspect] 통신 로그 이벤트 발행 실패 - intfId={}", intfId, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user