feat(core): add MciTemplate to simplify MCI communication boilerplate
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
package io.shinhanlife.axhub.biz.mcp.adapter.exception;
|
||||
|
||||
/**
|
||||
* @package io.shinhanlife.axhub.biz.mcp.adapter.exception
|
||||
* @className MciCommunicationException
|
||||
* @description AX HUB 시스템 처리 클래스
|
||||
* @author 김형식
|
||||
* @create 2026.09.01
|
||||
* <pre>
|
||||
* ---------- 개정이력 ----------
|
||||
* 수정일 수정자 수정내용
|
||||
* ---------- -------- ---------------------------
|
||||
* 2026.09.01 김형식 최초생성
|
||||
*
|
||||
* </pre>
|
||||
*/
|
||||
public class MciCommunicationException extends RuntimeException {
|
||||
|
||||
public MciCommunicationException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public MciCommunicationException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package io.shinhanlife.axhub.biz.mcp.adapter.support;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.shinhanlife.axhub.biz.mcp.adapter.exception.MciCommunicationException;
|
||||
import io.shinhanlife.axhub.biz.mcp.adapter.sender.EimsSender;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @package io.shinhanlife.axhub.biz.mcp.adapter.support
|
||||
* @className MciTemplate
|
||||
* @description AX HUB 시스템 처리 클래스
|
||||
* @author 김형식
|
||||
* @create 2026.09.01
|
||||
* <pre>
|
||||
* ---------- 개정이력 ----------
|
||||
* 수정일 수정자 수정내용
|
||||
* ---------- -------- ---------------------------
|
||||
* 2026.09.01 김형식 최초생성
|
||||
*
|
||||
* </pre>
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class MciTemplate {
|
||||
|
||||
private final EimsSender eimsSender;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
/**
|
||||
* MCI 인터페이스를 호출하고 결과를 지정된 타입으로 반환합니다.
|
||||
*
|
||||
* @param interfaceId 호출할 MCI 인터페이스 ID
|
||||
* @param requestDto 요청 데이터 객체
|
||||
* @param responseType 응답을 매핑할 클래스 타입
|
||||
* @param <T> 요청 객체 타입
|
||||
* @param <R> 응답 객체 타입
|
||||
* @return 매핑된 응답 객체
|
||||
* @throws MciCommunicationException 통신 또는 파싱 실패 시 예외 발생
|
||||
*/
|
||||
public <T, R> R call(String interfaceId, T requestDto, Class<R> responseType) {
|
||||
log.info("📞 [MciTemplate] 시작 - Interface ID: {}", interfaceId);
|
||||
|
||||
try {
|
||||
String payload = objectMapper.writeValueAsString(requestDto);
|
||||
log.debug("📤 [MciTemplate] 전송 페이로드: {}", payload);
|
||||
|
||||
String responseJson = eimsSender.send(interfaceId, payload);
|
||||
log.debug("📥 [MciTemplate] 수신 응답 JSON: {}", responseJson);
|
||||
|
||||
R response = objectMapper.readValue(responseJson, responseType);
|
||||
log.info("✅ [MciTemplate] 완료 - Interface ID: {}", interfaceId);
|
||||
|
||||
return response;
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("❌ [MciTemplate] JSON 변환 중 오류 발생", e);
|
||||
throw new MciCommunicationException("MCI 통신 중 JSON 파싱 오류", e);
|
||||
} catch (Exception e) {
|
||||
log.error("❌ [MciTemplate] 통신 중 오류 발생", e);
|
||||
throw new MciCommunicationException("MCI 통신 실패", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
package io.shinhanlife.axhub.biz.mcp.sample.service;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.shinhanlife.axhub.biz.mcp.adapter.sender.EimsSender;
|
||||
import io.shinhanlife.axhub.biz.mcp.adapter.support.MciTemplate;
|
||||
import io.shinhanlife.axhub.biz.mcp.sample.dto.SampleMciReqDto;
|
||||
import io.shinhanlife.axhub.biz.mcp.sample.dto.SampleMciResDto;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -28,39 +26,16 @@ import org.springframework.stereotype.Service;
|
||||
@RequiredArgsConstructor
|
||||
public class SampleMciCallService {
|
||||
|
||||
private final EimsSender eimsSender;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final MciTemplate mciTemplate;
|
||||
|
||||
/**
|
||||
* MCI 인터페이스를 호출하는 샘플 메서드
|
||||
* MCI 인터페이스를 호출하는 샘플 메서드 (MciTemplate 시범 적용)
|
||||
*
|
||||
* @param request MCI 호출을 위한 요청 데이터
|
||||
* @return MCI 서버의 응답 데이터
|
||||
*/
|
||||
public SampleMciResDto callSampleMci(SampleMciReqDto request) {
|
||||
log.info("📞 [MciCall] 시작 - Interface ID: {}", request.getInterfaceId());
|
||||
|
||||
try {
|
||||
// 1. 요청 객체(DTO)를 JSON 문자열로 변환 (직렬화)
|
||||
String payload = objectMapper.writeValueAsString(request);
|
||||
log.debug("📤 [MciCall] 전송 페이로드: {}", payload);
|
||||
|
||||
// 2. HTTP/2 기반의 EimsSender를 통해 실제 MCI 호출
|
||||
String responseJson = eimsSender.send(request.getInterfaceId(), payload);
|
||||
log.debug("📥 [MciCall] 수신 응답 JSON: {}", responseJson);
|
||||
|
||||
// 3. 수신받은 JSON 문자열을 응답 객체(DTO)로 역직렬화 후 반환
|
||||
SampleMciResDto response = objectMapper.readValue(responseJson, SampleMciResDto.class);
|
||||
log.info("✅ [MciCall] 완료 - 응답 코드: {}", response.getResCode());
|
||||
|
||||
return response;
|
||||
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("❌ [MciCall] JSON 변환 중 오류 발생", e);
|
||||
throw new RuntimeException("MCI 통신 중 JSON 파싱 오류", e);
|
||||
} catch (Exception e) {
|
||||
log.error("❌ [MciCall] 통신 중 오류 발생", e);
|
||||
throw new RuntimeException("MCI 통신 실패", e);
|
||||
}
|
||||
// 기존 20줄 이상의 통신 로직이 단 1줄로 완벽하게 은닉화 되었습니다.
|
||||
return mciTemplate.call(request.getInterfaceId(), request, SampleMciResDto.class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user