✨ Event Range 세팅방식 변경
- API_KET 세팅방식 변경
This commit is contained in:
부모
630c366a06
커밋
b2167d4ec7
@ -6,6 +6,7 @@ import com.snp.batch.jobs.event.batch.entity.EventEntity;
|
|||||||
import com.snp.batch.jobs.event.batch.processor.EventDataProcessor;
|
import com.snp.batch.jobs.event.batch.processor.EventDataProcessor;
|
||||||
import com.snp.batch.jobs.event.batch.reader.EventDataReader;
|
import com.snp.batch.jobs.event.batch.reader.EventDataReader;
|
||||||
import com.snp.batch.jobs.event.batch.writer.EventDataWriter;
|
import com.snp.batch.jobs.event.batch.writer.EventDataWriter;
|
||||||
|
import com.snp.batch.service.BatchDateService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.batch.core.Job;
|
import org.springframework.batch.core.Job;
|
||||||
import org.springframework.batch.core.Step;
|
import org.springframework.batch.core.Step;
|
||||||
@ -29,6 +30,7 @@ public class EventImportJobConfig extends BaseJobConfig<EventDto, EventEntity> {
|
|||||||
private final EventDataProcessor eventDataProcessor;
|
private final EventDataProcessor eventDataProcessor;
|
||||||
|
|
||||||
private final EventDataWriter eventDataWriter;
|
private final EventDataWriter eventDataWriter;
|
||||||
|
private final BatchDateService batchDateService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int getChunkSize() {
|
protected int getChunkSize() {
|
||||||
@ -40,12 +42,14 @@ public class EventImportJobConfig extends BaseJobConfig<EventDto, EventEntity> {
|
|||||||
EventDataProcessor eventDataProcessor,
|
EventDataProcessor eventDataProcessor,
|
||||||
EventDataWriter eventDataWriter,
|
EventDataWriter eventDataWriter,
|
||||||
JdbcTemplate jdbcTemplate,
|
JdbcTemplate jdbcTemplate,
|
||||||
@Qualifier("maritimeApiWebClient")WebClient maritimeApiWebClient) {
|
@Qualifier("maritimeApiWebClient")WebClient maritimeApiWebClient,
|
||||||
|
BatchDateService batchDateService) {
|
||||||
super(jobRepository, transactionManager);
|
super(jobRepository, transactionManager);
|
||||||
this.jdbcTemplate = jdbcTemplate;
|
this.jdbcTemplate = jdbcTemplate;
|
||||||
this.maritimeApiWebClient = maritimeApiWebClient;
|
this.maritimeApiWebClient = maritimeApiWebClient;
|
||||||
this.eventDataProcessor = eventDataProcessor;
|
this.eventDataProcessor = eventDataProcessor;
|
||||||
this.eventDataWriter = eventDataWriter;
|
this.eventDataWriter = eventDataWriter;
|
||||||
|
this.batchDateService = batchDateService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -60,7 +64,7 @@ public class EventImportJobConfig extends BaseJobConfig<EventDto, EventEntity> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected ItemReader<EventDto> createReader() {
|
protected ItemReader<EventDto> createReader() {
|
||||||
return new EventDataReader(maritimeApiWebClient, jdbcTemplate);
|
return new EventDataReader(maritimeApiWebClient, jdbcTemplate, batchDateService);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package com.snp.batch.jobs.event.batch.reader;
|
|||||||
import com.snp.batch.common.batch.reader.BaseApiReader;
|
import com.snp.batch.common.batch.reader.BaseApiReader;
|
||||||
import com.snp.batch.jobs.event.batch.dto.EventDto;
|
import com.snp.batch.jobs.event.batch.dto.EventDto;
|
||||||
import com.snp.batch.jobs.event.batch.dto.EventResponse;
|
import com.snp.batch.jobs.event.batch.dto.EventResponse;
|
||||||
|
import com.snp.batch.service.BatchDateService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.core.ParameterizedTypeReference;
|
import org.springframework.core.ParameterizedTypeReference;
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||||||
@ -10,14 +11,17 @@ import org.springframework.web.reactive.function.client.WebClient;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class EventDataReader extends BaseApiReader<EventDto> {
|
public class EventDataReader extends BaseApiReader<EventDto> {
|
||||||
private final JdbcTemplate jdbcTemplate;
|
private final JdbcTemplate jdbcTemplate;
|
||||||
|
private final BatchDateService batchDateService; // ✨ BatchDateService 필드 추가
|
||||||
|
|
||||||
public EventDataReader(WebClient webClient, JdbcTemplate jdbcTemplate) {
|
public EventDataReader(WebClient webClient, JdbcTemplate jdbcTemplate, BatchDateService batchDateService) {
|
||||||
super(webClient);
|
super(webClient);
|
||||||
this.jdbcTemplate = jdbcTemplate;
|
this.jdbcTemplate = jdbcTemplate;
|
||||||
|
this.batchDateService = batchDateService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -27,8 +31,9 @@ public class EventDataReader extends BaseApiReader<EventDto> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getApiPath() {
|
protected String getApiPath() {
|
||||||
return "MaritimeWCF/MaritimeAndTradeEventsService.svc/RESTFul/GetEventListByEventChangeDateRange?fromYear=2025&fromMonth=01&fromDay=01&fromHour=00&fromMinute=00&toYear=2025&toMonth=12&toDay=31&toHour=00&toMinute=00";
|
return "/MaritimeWCF/MaritimeAndTradeEventsService.svc/RESTFul/GetEventListByEventChangeDateRange";
|
||||||
}
|
}
|
||||||
|
protected String getApiKey() {return "EVENT_IMPORT_JOB";}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected List<EventDto> fetchDataFromApi() {
|
protected List<EventDto> fetchDataFromApi() {
|
||||||
@ -52,10 +57,21 @@ public class EventDataReader extends BaseApiReader<EventDto> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private EventResponse callEventApiWithBatch() {
|
private EventResponse callEventApiWithBatch() {
|
||||||
|
Map<String, String> params = batchDateService.getShipUpdateApiDateParams(getApiKey());
|
||||||
|
|
||||||
String url = getApiPath();
|
String url = getApiPath();
|
||||||
log.debug("[{}] API 호출: {}", getReaderName(), url);
|
log.info("[{}] API 호출: {}", getReaderName(), url);
|
||||||
|
|
||||||
return webClient.get()
|
return webClient.get()
|
||||||
.uri(url)
|
.uri(url, uriBuilder -> uriBuilder
|
||||||
|
// 맵에서 파라미터 값을 동적으로 가져와 세팅
|
||||||
|
.queryParam("fromYear", params.get("fromYear"))
|
||||||
|
.queryParam("fromMonth", params.get("fromMonth"))
|
||||||
|
.queryParam("fromDay", params.get("fromDay"))
|
||||||
|
.queryParam("toYear", params.get("toYear"))
|
||||||
|
.queryParam("toMonth", params.get("toMonth"))
|
||||||
|
.queryParam("toDay", params.get("toDay"))
|
||||||
|
.build())
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(EventResponse.class)
|
.bodyToMono(EventResponse.class)
|
||||||
.block();
|
.block();
|
||||||
|
|||||||
@ -3,24 +3,33 @@ package com.snp.batch.jobs.event.batch.writer;
|
|||||||
import com.snp.batch.common.batch.writer.BaseWriter;
|
import com.snp.batch.common.batch.writer.BaseWriter;
|
||||||
import com.snp.batch.jobs.event.batch.entity.EventEntity;
|
import com.snp.batch.jobs.event.batch.entity.EventEntity;
|
||||||
import com.snp.batch.jobs.event.batch.repository.EventRepository;
|
import com.snp.batch.jobs.event.batch.repository.EventRepository;
|
||||||
|
import com.snp.batch.service.BatchDateService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
public class EventDataWriter extends BaseWriter<EventEntity> {
|
public class EventDataWriter extends BaseWriter<EventEntity> {
|
||||||
|
|
||||||
private final EventRepository eventRepository;
|
private final EventRepository eventRepository;
|
||||||
public EventDataWriter(EventRepository eventRepository) {
|
private final BatchDateService batchDateService; // ✨ BatchDateService 필드 추가
|
||||||
|
protected String getApiKey() {return "EVENT_IMPORT_JOB";}
|
||||||
|
public EventDataWriter(EventRepository eventRepository, BatchDateService batchDateService) {
|
||||||
super("EventRepository");
|
super("EventRepository");
|
||||||
this.eventRepository = eventRepository;
|
this.eventRepository = eventRepository;
|
||||||
|
this.batchDateService = batchDateService;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void writeItems(List<EventEntity> items) throws Exception {
|
protected void writeItems(List<EventEntity> items) throws Exception {
|
||||||
eventRepository.saveEventAll(items);
|
eventRepository.saveEventAll(items);
|
||||||
log.info("Event 저장 완료: 수정={} 건", items.size());
|
log.info("Event 저장 완료: 수정={} 건", items.size());
|
||||||
|
|
||||||
|
// ✨ 배치 성공 시 상태 업데이트 (트랜잭션 커밋 직전에 실행)
|
||||||
|
LocalDate successDate = LocalDate.now();
|
||||||
|
batchDateService.updateLastSuccessDate(getApiKey(), successDate);
|
||||||
|
log.info("batch_last_execution update 완료 : {}", getApiKey());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -40,6 +40,7 @@ public class ShipDetailUpdateDataReader extends BaseApiReader<ShipDetailComparis
|
|||||||
private final JdbcTemplate jdbcTemplate;
|
private final JdbcTemplate jdbcTemplate;
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
private final BatchDateService batchDateService; // ✨ BatchDateService 필드 추가
|
private final BatchDateService batchDateService; // ✨ BatchDateService 필드 추가
|
||||||
|
protected String getApiKey() {return "SHIP_DETAIL_UPDATE_API";}
|
||||||
|
|
||||||
// 배치 처리 상태
|
// 배치 처리 상태
|
||||||
private List<String> allImoNumbers;
|
private List<String> allImoNumbers;
|
||||||
@ -251,7 +252,7 @@ public class ShipDetailUpdateDataReader extends BaseApiReader<ShipDetailComparis
|
|||||||
|
|
||||||
private ShipUpdateApiResponse callShipUpdateApi(){
|
private ShipUpdateApiResponse callShipUpdateApi(){
|
||||||
// 1. BatchDateService를 통해 동적 날짜 파라미터 맵 조회
|
// 1. BatchDateService를 통해 동적 날짜 파라미터 맵 조회
|
||||||
Map<String, String> params = batchDateService.getShipUpdateApiDateParams();
|
Map<String, String> params = batchDateService.getShipUpdateApiDateParams(getApiKey());
|
||||||
|
|
||||||
String url = getShipUpdateApiPath();
|
String url = getShipUpdateApiPath();
|
||||||
log.info("[{}] API 호출: {}", getReaderName(), url);
|
log.info("[{}] API 호출: {}", getReaderName(), url);
|
||||||
@ -292,8 +293,8 @@ public class ShipDetailUpdateDataReader extends BaseApiReader<ShipDetailComparis
|
|||||||
if (data == null) {
|
if (data == null) {
|
||||||
// 3. ✨ 배치 성공 시 상태 업데이트 (트랜잭션 커밋 직전에 실행)
|
// 3. ✨ 배치 성공 시 상태 업데이트 (트랜잭션 커밋 직전에 실행)
|
||||||
LocalDate successDate = LocalDate.now(); // 현재 배치 실행 시점의 날짜 (Reader의 toDay와 동일한 값)
|
LocalDate successDate = LocalDate.now(); // 현재 배치 실행 시점의 날짜 (Reader의 toDay와 동일한 값)
|
||||||
batchDateService.updateLastSuccessDate(successDate);
|
batchDateService.updateLastSuccessDate(getApiKey(), successDate);
|
||||||
log.info("batch_last_execution update 완료");
|
log.info("batch_last_execution update 완료 : {}", getApiKey());
|
||||||
|
|
||||||
log.info("[{}] 전체 {} 개 배치 처리 완료", getReaderName(), totalBatches);
|
log.info("[{}] 전체 {} 개 배치 처리 완료", getReaderName(), totalBatches);
|
||||||
log.info("[{}] 총 {} 개의 IMO 번호에 대한 API 호출 종료",
|
log.info("[{}] 총 {} 개의 IMO 번호에 대한 API 호출 종료",
|
||||||
|
|||||||
@ -9,7 +9,6 @@ import java.util.*;
|
|||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class BatchDateService {
|
public class BatchDateService {
|
||||||
private static final String API_KEY = "SHIP_UPDATE_API";
|
|
||||||
private final BatchLastExecutionRepository repository;
|
private final BatchLastExecutionRepository repository;
|
||||||
|
|
||||||
public BatchDateService(BatchLastExecutionRepository repository) {
|
public BatchDateService(BatchLastExecutionRepository repository) {
|
||||||
@ -19,10 +18,10 @@ public class BatchDateService {
|
|||||||
/**
|
/**
|
||||||
* API 호출에 필요한 from/to 날짜 파라미터를 계산하고 반환합니다.
|
* API 호출에 필요한 from/to 날짜 파라미터를 계산하고 반환합니다.
|
||||||
*/
|
*/
|
||||||
public Map<String, String> getShipUpdateApiDateParams() {
|
public Map<String, String> getShipUpdateApiDateParams(String apiKey) {
|
||||||
// 1. 마지막 성공 일자 (FROM 날짜)를 DB에서 조회
|
// 1. 마지막 성공 일자 (FROM 날짜)를 DB에서 조회
|
||||||
// 조회된 값이 없으면 (최초 실행), API 호출 시점의 하루 전 날짜를 사용합니다.
|
// 조회된 값이 없으면 (최초 실행), API 호출 시점의 하루 전 날짜를 사용합니다.
|
||||||
LocalDate lastDate = repository.findLastSuccessDate(API_KEY)
|
LocalDate lastDate = repository.findLastSuccessDate(apiKey)
|
||||||
.orElse(LocalDate.now().minusDays(1));
|
.orElse(LocalDate.now().minusDays(1));
|
||||||
|
|
||||||
// 2. 현재 실행 시점의 일자 (TO 날짜) 계산
|
// 2. 현재 실행 시점의 일자 (TO 날짜) 계산
|
||||||
@ -52,14 +51,14 @@ public class BatchDateService {
|
|||||||
* @param successDate API 호출 성공 시 사용된 to 날짜
|
* @param successDate API 호출 성공 시 사용된 to 날짜
|
||||||
*/
|
*/
|
||||||
@Transactional // UPDATE 쿼리를 사용하므로 트랜잭션 필요
|
@Transactional // UPDATE 쿼리를 사용하므로 트랜잭션 필요
|
||||||
public void updateLastSuccessDate(LocalDate successDate) {
|
public void updateLastSuccessDate(String apiKey, LocalDate successDate) { // ✨ apiKey 추가
|
||||||
|
|
||||||
// 1. UPDATE 시도
|
// 1. UPDATE 시도
|
||||||
int updatedRows = repository.updateLastSuccessDate(API_KEY, successDate);
|
int updatedRows = repository.updateLastSuccessDate(apiKey, successDate);
|
||||||
|
|
||||||
// 2. 업데이트된 레코드가 없다면 (최초 실행), INSERT 수행
|
// 2. 업데이트된 레코드가 없다면 (최초 실행), INSERT 수행
|
||||||
if (updatedRows == 0) {
|
if (updatedRows == 0) {
|
||||||
BatchLastExecution entity = new BatchLastExecution(API_KEY, successDate);
|
BatchLastExecution entity = new BatchLastExecution(apiKey, successDate);
|
||||||
repository.save(entity);
|
repository.save(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
불러오는 중...
Reference in New Issue
Block a user