store 배치 최종본 커밋
This commit is contained in:
parent
3088641e84
commit
3a2b958335
@ -9,11 +9,14 @@ import com.interplug.qcast.biz.user.dto.UserRequest;
|
|||||||
import com.interplug.qcast.util.InterfaceQsp;
|
import com.interplug.qcast.util.InterfaceQsp;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.batch.core.*;
|
import org.springframework.batch.core.*;
|
||||||
import org.springframework.batch.core.configuration.annotation.StepScope;
|
import org.springframework.batch.core.configuration.annotation.StepScope;
|
||||||
import org.springframework.batch.core.job.builder.JobBuilder;
|
import org.springframework.batch.core.job.builder.JobBuilder;
|
||||||
import org.springframework.batch.core.repository.JobRepository;
|
import org.springframework.batch.core.repository.JobRepository;
|
||||||
import org.springframework.batch.core.step.builder.StepBuilder;
|
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.ItemWriter;
|
||||||
import org.springframework.batch.item.support.ListItemReader;
|
import org.springframework.batch.item.support.ListItemReader;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@ -23,6 +26,7 @@ import org.springframework.http.HttpMethod;
|
|||||||
import org.springframework.transaction.PlatformTransactionManager;
|
import org.springframework.transaction.PlatformTransactionManager;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@Slf4j
|
||||||
public class StoreJobConfiguration implements JobExecutionListener {
|
public class StoreJobConfiguration implements JobExecutionListener {
|
||||||
|
|
||||||
private final InterfaceQsp interfaceQsp;
|
private final InterfaceQsp interfaceQsp;
|
||||||
@ -43,12 +47,14 @@ public class StoreJobConfiguration implements JobExecutionListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void beforeJob(JobExecution jobExecution) {
|
public void beforeJob(JobExecution jobExecution) {
|
||||||
|
log.info("Job 시작: 초기화 메서드 호출 중...");
|
||||||
try {
|
try {
|
||||||
this.storeSyncResponse =
|
this.storeSyncResponse =
|
||||||
interfaceQsp.callApiData(
|
interfaceQsp.callApiData(
|
||||||
HttpMethod.GET, qspMasterStoreBatchUrl, null, StoreSyncResponse.class);
|
HttpMethod.GET, qspMasterStoreBatchUrl, null, StoreSyncResponse.class);
|
||||||
|
log.info("API 호출 완료, 항목 수: {}", this.storeSyncResponse.getStoreList().size());
|
||||||
} catch (Exception e) {
|
} 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();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
private <T> Step buildStep(
|
||||||
public Step storeStep(
|
String stepName,
|
||||||
JobRepository jobRepository, PlatformTransactionManager transactionManager) {
|
JobRepository jobRepository,
|
||||||
return new StepBuilder("storeStep", jobRepository)
|
PlatformTransactionManager transactionManager,
|
||||||
.<StoreRequest, StoreRequest>chunk(10, transactionManager)
|
ItemReader<T> reader,
|
||||||
.reader(storeListReader())
|
ItemWriter<T> writer) {
|
||||||
.writer(storeWriter())
|
return new StepBuilder(stepName, jobRepository)
|
||||||
|
.<T, T>chunk(10, transactionManager)
|
||||||
|
.reader(reader)
|
||||||
|
.writer(writer)
|
||||||
.build();
|
.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
|
@Bean
|
||||||
public Step userStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
|
public Step userStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
|
||||||
return new StepBuilder("userStep", jobRepository)
|
return buildStep(
|
||||||
.<UserRequest, UserRequest>chunk(10, transactionManager)
|
"userStep",
|
||||||
.reader(userListReader())
|
jobRepository,
|
||||||
.writer(userWriter())
|
transactionManager,
|
||||||
.build();
|
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
|
@Bean
|
||||||
public Step favoriteStep(
|
public Step favoriteStep(
|
||||||
JobRepository jobRepository, PlatformTransactionManager transactionManager) {
|
JobRepository jobRepository, PlatformTransactionManager transactionManager) {
|
||||||
return new StepBuilder("favoriteStep", jobRepository)
|
return buildStep(
|
||||||
.<StoreFavoriteRequest, StoreFavoriteRequest>chunk(10, transactionManager)
|
"favoriteStep",
|
||||||
.reader(storeFavListReader())
|
jobRepository,
|
||||||
.writer(favoriteWriter())
|
transactionManager,
|
||||||
.build();
|
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
|
@Bean
|
||||||
@StepScope
|
@StepScope
|
||||||
public ListItemReader<StoreRequest> storeListReader() {
|
public ListItemReader<StoreRequest> storeListReader() {
|
||||||
return (storeSyncResponse != null && storeSyncResponse.getStoreList() != null)
|
return createReader(
|
||||||
? new ListItemReader<>(storeSyncResponse.getStoreList())
|
storeSyncResponse != null ? storeSyncResponse.getStoreList() : null, "store");
|
||||||
: new ListItemReader<>(Collections.emptyList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@StepScope
|
@StepScope
|
||||||
public ListItemReader<UserRequest> userListReader() {
|
public ListItemReader<UserRequest> userListReader() {
|
||||||
return (storeSyncResponse != null && storeSyncResponse.getUserList() != null)
|
return createReader(storeSyncResponse != null ? storeSyncResponse.getUserList() : null, "user");
|
||||||
? new ListItemReader<>(storeSyncResponse.getUserList())
|
|
||||||
: new ListItemReader<>(Collections.emptyList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@StepScope
|
@StepScope
|
||||||
public ListItemReader<StoreFavoriteRequest> storeFavListReader() {
|
public ListItemReader<StoreFavoriteRequest> storeFavListReader() {
|
||||||
return (storeSyncResponse != null && storeSyncResponse.getStoreFavList() != null)
|
return createReader(
|
||||||
? new ListItemReader<>(storeSyncResponse.getStoreFavList())
|
storeSyncResponse != null ? storeSyncResponse.getStoreFavList() : null, "storeFav");
|
||||||
: new ListItemReader<>(Collections.emptyList());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
private <T> ItemWriter<T> createWriter(Consumer<List<T>> processor, String writerName) {
|
||||||
public ItemWriter<StoreRequest> storeWriter() {
|
|
||||||
return items -> {
|
return items -> {
|
||||||
List<StoreRequest> storeRequestList =
|
try {
|
||||||
(List<StoreRequest>) items.getItems(); // Chunk에서 List로 추출
|
List<T> itemList = (List<T>) items.getItems();
|
||||||
userService.setStoreBatch(storeRequestList); // 서비스에 List를 한 번에 전달
|
processor.accept(itemList);
|
||||||
};
|
log.info("{}Writer: {} items 처리 완료", writerName, items.size());
|
||||||
}
|
} catch (Exception e) {
|
||||||
|
log.error("{}Writer 오류: {}", writerName, e.getMessage());
|
||||||
@Bean
|
throw e;
|
||||||
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);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user