From a533a28a69fae6a95d5ab2bc06d19c2244559603 Mon Sep 17 00:00:00 2001 From: Sunghyun Lee <98dltjdgus@naver.com> Date: Thu, 18 Apr 2024 19:47:43 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EC=8A=A4=ED=94=84=EB=A7=81=20=EB=B0=B0?= =?UTF-8?q?=EC=B9=98=20=EC=8A=A4=EC=BC=80=EC=A4=84=EB=A7=81=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - BatchApplication.java: 배치 스케줄링 및 @EnableBatchProcessing 어노테이션 이동. - DeleteExpiredEbookJobConfiguration.java: @EnableBatchProcessing 어노테이션 이동 및 쿼리문 내 별명 수정. 리턴 구문 단순화. - BatchScheduler.java: 만료된 전자책 삭제 Job이 매일 0시 0분 0초에 수행되도록 스케줄링 설정. - app-batch\src\main\resources\application.yaml: 배치 앱 실행 시 최초 수행을 막도록 batch.job.enabled: false 적용. 잘못된 프로파일명 수정. --- .../nesonnechek/batch/BatchApplication.java | 4 +++ .../DeleteExpiredEbookJobConfiguration.java | 8 ++--- .../batch/scheduler/BatchScheduler.java | 31 +++++++++++++++++++ app-batch/src/main/resources/application.yaml | 6 ++-- 4 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 app-batch/src/main/java/com/shacomiro/nesonnechek/batch/scheduler/BatchScheduler.java diff --git a/app-batch/src/main/java/com/shacomiro/nesonnechek/batch/BatchApplication.java b/app-batch/src/main/java/com/shacomiro/nesonnechek/batch/BatchApplication.java index d7e4a70..575d865 100644 --- a/app-batch/src/main/java/com/shacomiro/nesonnechek/batch/BatchApplication.java +++ b/app-batch/src/main/java/com/shacomiro/nesonnechek/batch/BatchApplication.java @@ -1,10 +1,14 @@ package com.shacomiro.nesonnechek.batch; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication +@EnableBatchProcessing +@EnableScheduling @ComponentScan({ "com.shacomiro.nesonnechek.batch", "com.shacomiro.nesonnechek.domain" diff --git a/app-batch/src/main/java/com/shacomiro/nesonnechek/batch/job/DeleteExpiredEbookJobConfiguration.java b/app-batch/src/main/java/com/shacomiro/nesonnechek/batch/job/DeleteExpiredEbookJobConfiguration.java index 45fc3e2..ae9e614 100644 --- a/app-batch/src/main/java/com/shacomiro/nesonnechek/batch/job/DeleteExpiredEbookJobConfiguration.java +++ b/app-batch/src/main/java/com/shacomiro/nesonnechek/batch/job/DeleteExpiredEbookJobConfiguration.java @@ -4,7 +4,6 @@ import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; -import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; import org.springframework.batch.core.configuration.annotation.JobScope; import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; @@ -27,7 +26,6 @@ @Slf4j @Configuration -@EnableBatchProcessing @RequiredArgsConstructor public class DeleteExpiredEbookJobConfiguration { private static final int CHUNK_SIZE = 10; @@ -62,7 +60,7 @@ public JpaPagingItemReader ebookReader() { .name("expiredEbookReader") .entityManagerFactory(emf) .pageSize(CHUNK_SIZE) - .queryString("SELECT ef FROM Ebook ef") + .queryString("SELECT e FROM Ebook e") .build(); } @@ -88,9 +86,9 @@ public ItemProcessor deleteExpiredEbookProcessor() { ebook.getName(), ebook.getUuid(), ebook.isExist(), ebook.isExpired(), isDeleted); return ebook; - } else { - return null; } + + return null; }; } diff --git a/app-batch/src/main/java/com/shacomiro/nesonnechek/batch/scheduler/BatchScheduler.java b/app-batch/src/main/java/com/shacomiro/nesonnechek/batch/scheduler/BatchScheduler.java new file mode 100644 index 0000000..fc8e24c --- /dev/null +++ b/app-batch/src/main/java/com/shacomiro/nesonnechek/batch/scheduler/BatchScheduler.java @@ -0,0 +1,31 @@ +package com.shacomiro.nesonnechek.batch.scheduler; + +import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.JobParametersInvalidException; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; +import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; +import org.springframework.batch.core.repository.JobRestartException; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import com.shacomiro.nesonnechek.batch.job.DeleteExpiredEbookJobConfiguration; + +import lombok.RequiredArgsConstructor; + +@Component +@RequiredArgsConstructor +public class BatchScheduler { + private final JobLauncher jobLauncher; + private final DeleteExpiredEbookJobConfiguration deleteExpiredEbookJobConfiguration; + + @Scheduled(cron = "0 0 0 * * *") + public void runDeleteExpiredEbookJob() throws + JobInstanceAlreadyCompleteException, + JobExecutionAlreadyRunningException, + JobParametersInvalidException, + JobRestartException { + jobLauncher.run(deleteExpiredEbookJobConfiguration.deleteExpiredEbookJob(), + new JobParametersBuilder().addLong("time", System.currentTimeMillis()).toJobParameters()); + } +} diff --git a/app-batch/src/main/resources/application.yaml b/app-batch/src/main/resources/application.yaml index 837521a..46c4e5a 100644 --- a/app-batch/src/main/resources/application.yaml +++ b/app-batch/src/main/resources/application.yaml @@ -6,7 +6,7 @@ spring: group: prod: - batch-app-prod - - domains-rds-prod + - domain-rds-prod dev: - batch-app-dev - domain-rds-dev @@ -18,9 +18,7 @@ spring: active: ${profile} batch: job: - names: ${job.name:NONE} - main: - web-application-type: none + enabled: false --- spring: config: