store 배치 최종본 커밋

This commit is contained in:
DESKTOP-6ARNG1Q\dlsgk 2024-11-08 15:28:05 +09:00
parent 3088641e84
commit 3a2b958335

View File

@ -9,11 +9,14 @@ import com.interplug.qcast.biz.user.dto.UserRequest;
import com.interplug.qcast.util.InterfaceQsp;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.beans.factory.annotation.Value;
@ -23,6 +26,7 @@ import org.springframework.http.HttpMethod;
import org.springframework.transaction.PlatformTransactionManager;
@Configuration
@Slf4j
public class StoreJobConfiguration implements JobExecutionListener {
private final InterfaceQsp interfaceQsp;
@ -43,12 +47,14 @@ public class StoreJobConfiguration implements JobExecutionListener {
@Override
public void beforeJob(JobExecution jobExecution) {
log.info("Job 시작: 초기화 메서드 호출 중...");
try {
this.storeSyncResponse =
interfaceQsp.callApiData(
HttpMethod.GET, qspMasterStoreBatchUrl, null, StoreSyncResponse.class);
log.info("API 호출 완료, 항목 수: {}", this.storeSyncResponse.getStoreList().size());
} catch (Exception e) {
System.err.println("storeSyncResponse beforeJob 오류 발생: " + e.getMessage());
log.error("storeSyncResponse 갱신 중 오류: {}", e.getMessage());
}
}
@ -63,81 +69,119 @@ public class StoreJobConfiguration implements JobExecutionListener {
.build();
}
@Bean
public Step storeStep(
JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new StepBuilder("storeStep", jobRepository)
.<StoreRequest, StoreRequest>chunk(10, transactionManager)
.reader(storeListReader())
.writer(storeWriter())
private <T> Step buildStep(
String stepName,
JobRepository jobRepository,
PlatformTransactionManager transactionManager,
ItemReader<T> reader,
ItemWriter<T> writer) {
return new StepBuilder(stepName, jobRepository)
.<T, T>chunk(10, transactionManager)
.reader(reader)
.writer(writer)
.build();
}
@Bean
public Step storeStep(
JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return buildStep(
"storeStep",
jobRepository,
transactionManager,
storeListReader(),
createWriter(
(items) -> {
try {
log.debug("Store batch processing {} items", items.size());
userService.setStoreBatch(items);
log.debug("Successfully processed store batch");
} catch (Exception e) {
log.error("Error processing store batch: {}", e.getMessage(), e);
throw new RuntimeException("Failed to process store batch", e);
}
},
"store"));
}
@Bean
public Step userStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new StepBuilder("userStep", jobRepository)
.<UserRequest, UserRequest>chunk(10, transactionManager)
.reader(userListReader())
.writer(userWriter())
.build();
return buildStep(
"userStep",
jobRepository,
transactionManager,
userListReader(),
createWriter(
(items) -> {
try {
log.debug("User batch processing {} items", items.size());
userService.setUserBatch(items);
log.debug("Successfully processed user batch");
} catch (Exception e) {
log.error("Error processing user batch: {}", e.getMessage(), e);
throw new RuntimeException("Failed to process user batch", e);
}
},
"user"));
}
@Bean
public Step favoriteStep(
JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new StepBuilder("favoriteStep", jobRepository)
.<StoreFavoriteRequest, StoreFavoriteRequest>chunk(10, transactionManager)
.reader(storeFavListReader())
.writer(favoriteWriter())
.build();
return buildStep(
"favoriteStep",
jobRepository,
transactionManager,
storeFavListReader(),
createWriter(
(items) -> {
try {
log.debug("Favorite batch processing {} items", items.size());
storeFavService.setStoreFavoriteBatch(items);
log.debug("Successfully processed favorite batch");
} catch (Exception e) {
log.error("Error processing favorite batch: {}", e.getMessage(), e);
throw new RuntimeException("Failed to process favorite batch", e);
}
},
"favorite"));
}
private <T> ListItemReader<T> createReader(List<T> items, String readerName) {
log.info("{}Reader 호출됨...", readerName);
return new ListItemReader<>(items != null ? items : Collections.emptyList());
}
@Bean
@StepScope
public ListItemReader<StoreRequest> storeListReader() {
return (storeSyncResponse != null && storeSyncResponse.getStoreList() != null)
? new ListItemReader<>(storeSyncResponse.getStoreList())
: new ListItemReader<>(Collections.emptyList());
return createReader(
storeSyncResponse != null ? storeSyncResponse.getStoreList() : null, "store");
}
@Bean
@StepScope
public ListItemReader<UserRequest> userListReader() {
return (storeSyncResponse != null && storeSyncResponse.getUserList() != null)
? new ListItemReader<>(storeSyncResponse.getUserList())
: new ListItemReader<>(Collections.emptyList());
return createReader(storeSyncResponse != null ? storeSyncResponse.getUserList() : null, "user");
}
@Bean
@StepScope
public ListItemReader<StoreFavoriteRequest> storeFavListReader() {
return (storeSyncResponse != null && storeSyncResponse.getStoreFavList() != null)
? new ListItemReader<>(storeSyncResponse.getStoreFavList())
: new ListItemReader<>(Collections.emptyList());
return createReader(
storeSyncResponse != null ? storeSyncResponse.getStoreFavList() : null, "storeFav");
}
@Bean
public ItemWriter<StoreRequest> storeWriter() {
private <T> ItemWriter<T> createWriter(Consumer<List<T>> processor, String writerName) {
return items -> {
List<StoreRequest> storeRequestList =
(List<StoreRequest>) items.getItems(); // Chunk에서 List로 추출
userService.setStoreBatch(storeRequestList); // 서비스에 List를 번에 전달
};
}
@Bean
public ItemWriter<UserRequest> userWriter() {
return items -> {
List<UserRequest> userRequestList = (List<UserRequest>) items.getItems();
userService.setUserBatch(userRequestList);
};
}
@Bean
public ItemWriter<StoreFavoriteRequest> favoriteWriter() {
return items -> {
List<StoreFavoriteRequest> storeFavoriteList = (List<StoreFavoriteRequest>) items.getItems();
storeFavService.setStoreFavoriteBatch(storeFavoriteList);
try {
List<T> itemList = (List<T>) items.getItems();
processor.accept(itemList);
log.info("{}Writer: {} items 처리 완료", writerName, items.size());
} catch (Exception e) {
log.error("{}Writer 오류: {}", writerName, e.getMessage());
throw e;
}
};
}
}