diff --git a/src/main/java/com/example/springbatch/batch/SecondBatch.java b/src/main/java/com/example/springbatch/batch/SecondBatch.java index 78d76b4..6d66f63 100644 --- a/src/main/java/com/example/springbatch/batch/SecondBatch.java +++ b/src/main/java/com/example/springbatch/batch/SecondBatch.java @@ -20,18 +20,18 @@ public class SecondBatch { this.platformTransactionManager = platformTransactionManager; } - @Bean - public Job secondJob() { - - return new JobBuilder("secondJob", jobRepository) - .start() - .build(); - } - - @Bean - public Step secondStep() { - - return new StepBuilder("secondStep", jobRepository) - .build(); - } +// @Bean +// public Job secondJob() { +// +// return new JobBuilder("secondJob", jobRepository) +// .start() +// .build(); +// } +// +// @Bean +// public Step secondStep() { +// +// return new StepBuilder("secondStep", jobRepository) +// .build(); +// } } diff --git a/src/main/java/com/example/springbatch/controller/MainController.java b/src/main/java/com/example/springbatch/controller/MainController.java index 8fb7239..fa71c0f 100644 --- a/src/main/java/com/example/springbatch/controller/MainController.java +++ b/src/main/java/com/example/springbatch/controller/MainController.java @@ -4,6 +4,7 @@ import org.springframework.batch.core.Job; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -14,20 +15,22 @@ import org.springframework.web.bind.annotation.ResponseBody; public class MainController { private final JobLauncher jobLauncher; - private final Job jobFirst; + private final ApplicationContext applicationContext; - public MainController(JobLauncher jobLauncher, Job jobFirst) { + public MainController(JobLauncher jobLauncher, ApplicationContext applicationContext) { this.jobLauncher = jobLauncher; - this.jobFirst = jobFirst; + this.applicationContext = applicationContext; } - @GetMapping("/") + @GetMapping("/first") public String mainApi(@RequestParam("value") String value) { JobParameters jobParameters = new JobParametersBuilder() - .addString("value", value) + .addString("date", value) .toJobParameters(); + Job jobFirst = applicationContext.getBean("firstJob", Job.class); + try { jobLauncher.run(jobFirst, jobParameters); } catch (Exception e) { diff --git a/src/main/java/com/example/springbatch/schedule/FirstSchedule.java b/src/main/java/com/example/springbatch/schedule/FirstSchedule.java new file mode 100644 index 0000000..8e7ff9a --- /dev/null +++ b/src/main/java/com/example/springbatch/schedule/FirstSchedule.java @@ -0,0 +1,46 @@ +package com.example.springbatch.schedule; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobParameters; +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.context.ApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.Scheduled; + +import java.text.SimpleDateFormat; +import java.util.Date; + +@Configuration +public class FirstSchedule { + + private final JobLauncher jobLauncher; + private final ApplicationContext applicationContext; + + public FirstSchedule(JobLauncher jobLauncher, ApplicationContext applicationContext) { + this.jobLauncher = jobLauncher; + this.applicationContext = applicationContext; + } + + @Scheduled(cron = "1 * * * * *", zone = "Asia/Seoul") + public void runFirstJob() throws JobInstanceAlreadyCompleteException, + JobExecutionAlreadyRunningException, + JobParametersInvalidException, + JobRestartException { + + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss"); + String date = dateFormat.format(new Date()); + + JobParameters jobParameters = new JobParametersBuilder() + .addString("date", date) + .toJobParameters(); + + Job firstJob = applicationContext.getBean("FirstJob", Job.class); + + jobLauncher.run(firstJob, jobParameters); + } +}