Spring Batch 입문: 3시간 만에 끝내는 대용량 처리의 기초
configuring-spring-batch-scheduler
✅ 1. 스케줄러 클래스 구현
JobScheduler.java
package com.batch.settlement.scheduler;
@Slf4j
@Component
@RequiredArgsConstructor
public class JobScheduler {
// 스프링 배치 6버전 변경 사항
private final JobOperator jobOperator;
private final Job settlementJob;
//@Scheduled(cron = "0 0 4 * * *")
//@Scheduled(cron = "0/10 * * * * *")
@Scheduled(cron = "0 30 13 * * *")
public void runJob(){
String jobName = "settlementJob";
try{
JobParameters jobParameters = new JobParametersBuilder()
.addString("targetDate", LocalDate.now().minusDays(7).toString())
.addLong("time", System.currentTimeMillis()) // 중복 방지
.toJobParameters();
log.info("스케줄러 작동! 배치를 실행합니다.");
jobOperator.start(settlementJob, jobParameters);
} catch (JobInstanceAlreadyCompleteException e) {
log.error("배치 실행 중 예외가 발생했습니다.", e);
} catch (InvalidJobParametersException e) {
log.error("배치 실행 중 예외가 발생했습니다.", e);
} catch (JobExecutionAlreadyRunningException e) {
log.error("배치 실행 중 예외가 발생했습니다.", e);
} catch (JobRestartException e) {
log.error("배치 실행 중 예외가 발생했습니다.", e);
} catch (Exception e){
log.error("알 수 없는 에러 발생");
}
}
}
✅ 2. 메인 메소드 수정
SettlementApplication.java
package com.batch.settlement;
@EnableScheduling
@SpringBootApplication
public class SettlementApplication {
public static void main(String[] args) {
SpringApplication.run(SettlementApplication.class, args);
}
}
✅ 3. 실행 확인