Skip to content

Commit

Permalink
Feat: 스프링 배치 스케줄링 적용
Browse files Browse the repository at this point in the history
- BatchApplication.java: 배치 스케줄링 및 @EnableBatchProcessing 어노테이션 이동.
- DeleteExpiredEbookJobConfiguration.java: @EnableBatchProcessing  어노테이션 이동 및 쿼리문 내 별명 수정. 리턴 구문 단순화.
- BatchScheduler.java: 만료된 전자책 삭제 Job이 매일 0시 0분 0초에 수행되도록 스케줄링 설정.
- app-batch\src\main\resources\application.yaml: 배치 앱 실행 시 최초 수행을 막도록 batch.job.enabled: false 적용. 잘못된 프로파일명 수정.
  • Loading branch information
shacomiro committed Apr 18, 2024
1 parent 9c7e541 commit a533a28
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +26,6 @@

@Slf4j
@Configuration
@EnableBatchProcessing
@RequiredArgsConstructor
public class DeleteExpiredEbookJobConfiguration {
private static final int CHUNK_SIZE = 10;
Expand Down Expand Up @@ -62,7 +60,7 @@ public JpaPagingItemReader<Ebook> ebookReader() {
.name("expiredEbookReader")
.entityManagerFactory(emf)
.pageSize(CHUNK_SIZE)
.queryString("SELECT ef FROM Ebook ef")
.queryString("SELECT e FROM Ebook e")
.build();
}

Expand All @@ -88,9 +86,9 @@ public ItemProcessor<Ebook, Ebook> deleteExpiredEbookProcessor() {
ebook.getName(), ebook.getUuid(), ebook.isExist(), ebook.isExpired(), isDeleted);

return ebook;
} else {
return null;
}

return null;
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
6 changes: 2 additions & 4 deletions app-batch/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ spring:
group:
prod:
- batch-app-prod
- domains-rds-prod
- domain-rds-prod
dev:
- batch-app-dev
- domain-rds-dev
Expand All @@ -18,9 +18,7 @@ spring:
active: ${profile}
batch:
job:
names: ${job.name:NONE}
main:
web-application-type: none
enabled: false
---
spring:
config:
Expand Down

0 comments on commit a533a28

Please sign in to comment.