-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#23 - Feat: @EnableScheduling, @scheduled 를 이용해 매달 15일 오전 4시에 배치 실행되도…
…록 Scheduler 생성
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
3Week_Mission/mutbooks/src/main/java/com/example/mutbooks/scheduler/JobScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |