Skip to content

Commit

Permalink
#23 - Feat: @EnableScheduling, @scheduled 를 이용해 매달 15일 오전 4시에 배치 실행되도…
Browse files Browse the repository at this point in the history
…록 Scheduler 생성
  • Loading branch information
ahah525 committed Nov 1, 2022
1 parent ccd16b2 commit b94daa3
Showing 1 changed file with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.example.mutbooks.scheduler;

import com.example.mutbooks.job.makeRebateOrderItem.MakeRebateOrderItemJobConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.Step;
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 java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Component
@RequiredArgsConstructor
@Slf4j
public class JobScheduler {
private final JobLauncher jobLauncher; // Job 실행 객체

private final MakeRebateOrderItemJobConfig makeRebateOrderItemJobConfig;
private final Step makeRebateOrderItemStep1;

// 매 분 00초마다 실행
@Scheduled(cron = "0 * * * * *")

// 매달 15일 오전 4시 0분 0초마다 Job 실행
// @Scheduled(cron = "0 0 4 15 * *")
public void runJob() {
log.info(String.valueOf(LocalDateTime.now()));

Map<String, JobParameter> confMap = new HashMap<>();
confMap.put("time", new JobParameter(System.currentTimeMillis()));
JobParameters jobParameters = new JobParameters(confMap);

try {
jobLauncher.run(makeRebateOrderItemJobConfig.makeRebateOrderItemJob(makeRebateOrderItemStep1), jobParameters);
} catch (JobExecutionAlreadyRunningException e) {
throw new RuntimeException(e);
} catch (JobRestartException e) {
throw new RuntimeException(e);
} catch (JobInstanceAlreadyCompleteException e) {
throw new RuntimeException(e);
} catch (JobParametersInvalidException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit b94daa3

Please sign in to comment.