diff --git a/src/main/java/com/example/springbatch/SpringBatchApplication.java b/src/main/java/com/example/springbatch/SpringBatchApplication.java index e73fb09..536c503 100644 --- a/src/main/java/com/example/springbatch/SpringBatchApplication.java +++ b/src/main/java/com/example/springbatch/SpringBatchApplication.java @@ -2,12 +2,13 @@ package com.example.springbatch; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication +@EnableScheduling public class SpringBatchApplication { public static void main(String[] args) { SpringApplication.run(SpringBatchApplication.class, args); } - } diff --git a/src/main/java/com/example/springbatch/batch/ThirdBatch.java b/src/main/java/com/example/springbatch/batch/ThirdBatch.java new file mode 100644 index 0000000..a61b2fd --- /dev/null +++ b/src/main/java/com/example/springbatch/batch/ThirdBatch.java @@ -0,0 +1,69 @@ +package com.example.springbatch.batch; + +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.job.builder.JobBuilder; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.step.builder.StepBuilder; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.item.ItemReader; +import org.springframework.batch.item.ItemWriter; +import org.springframework.batch.item.support.ListItemReader; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.transaction.PlatformTransactionManager; + +import java.util.Arrays; + +@Configuration +public class ThirdBatch { + + private final JobRepository jobRepository; + private final PlatformTransactionManager platformTransactionManager; + + public ThirdBatch(JobRepository jobRepository, PlatformTransactionManager platformTransactionManager) { + this.jobRepository = jobRepository; + this.platformTransactionManager = platformTransactionManager; + } + + @Bean + public Job thirdJob() { + + System.out.println("third job"); + + return new JobBuilder("thirdJob", jobRepository) + .start(thirdStep()) + .build(); + } + + @Bean + public Step thirdStep() { + + System.out.println("third step"); + + return new StepBuilder("thirdStep", jobRepository) + . chunk(3, platformTransactionManager) + .reader(thirdReader()) + .processor(upperProcessor()) + .writer(thirdWriter()) + .build(); + } + + @Bean + public ItemReader thirdReader() { + + return new ListItemReader<>(Arrays.asList("kim", "lee", "park", "choi", "jeong", "ha", "jo")); + } + + @Bean + public ItemProcessor upperProcessor() { + + return item -> item.toUpperCase(); + } + + @Bean + public ItemWriter thirdWriter() { + + return item -> item.forEach(System.out::println); + } +} diff --git a/src/main/java/com/example/springbatch/controller/MainController.java b/src/main/java/com/example/springbatch/controller/MainController.java index fa71c0f..055de40 100644 --- a/src/main/java/com/example/springbatch/controller/MainController.java +++ b/src/main/java/com/example/springbatch/controller/MainController.java @@ -3,6 +3,7 @@ package com.example.springbatch.controller; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; +import org.springframework.batch.core.configuration.JobRegistry; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Controller; @@ -16,23 +17,42 @@ public class MainController { private final JobLauncher jobLauncher; private final ApplicationContext applicationContext; + private final JobRegistry jobRegistry; - public MainController(JobLauncher jobLauncher, ApplicationContext applicationContext) { + public MainController(JobLauncher jobLauncher, ApplicationContext applicationContext, JobRegistry jobRegistry) { this.jobLauncher = jobLauncher; this.applicationContext = applicationContext; + this.jobRegistry = jobRegistry; } @GetMapping("/first") - public String mainApi(@RequestParam("value") String value) { + public String firstApi(@RequestParam("value") String value) { JobParameters jobParameters = new JobParametersBuilder() .addString("date", value) .toJobParameters(); - Job jobFirst = applicationContext.getBean("firstJob", Job.class); + try { + jobLauncher.run(jobRegistry.getJob("firstJob"), jobParameters); + } catch (Exception e) { + throw new RuntimeException(e); + } + + return "ok"; + } + + @GetMapping("/third") + public String thirdApi(@RequestParam("value") String value) { + + System.out.println(value); + + JobParameters jobParameters = new JobParametersBuilder() + .addString("date", value) +// .addLong() + .toJobParameters(); try { - jobLauncher.run(jobFirst, jobParameters); + jobLauncher.run(jobRegistry.getJob("thirdJob"), jobParameters); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/src/main/java/com/example/springbatch/schedule/FirstSchedule.java b/src/main/java/com/example/springbatch/schedule/FirstSchedule.java index 260ad38..8456703 100644 --- a/src/main/java/com/example/springbatch/schedule/FirstSchedule.java +++ b/src/main/java/com/example/springbatch/schedule/FirstSchedule.java @@ -1,14 +1,9 @@ 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.configuration.JobRegistry; 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; @@ -19,18 +14,18 @@ import java.util.Date; public class FirstSchedule { private final JobLauncher jobLauncher; - private final ApplicationContext applicationContext; + private final JobRegistry jobRegistry; + //private final ApplicationContext applicationContext; - public FirstSchedule(JobLauncher jobLauncher, ApplicationContext applicationContext) { + public FirstSchedule(JobLauncher jobLauncher, JobRegistry jobRegistry) { this.jobLauncher = jobLauncher; - this.applicationContext = applicationContext; + this.jobRegistry = jobRegistry; } @Scheduled(cron = "10 * * * * *", zone = "Asia/Seoul") - public void runFirstJob() throws JobInstanceAlreadyCompleteException, - JobExecutionAlreadyRunningException, - JobParametersInvalidException, - JobRestartException { + public void runFirstJob() throws Exception { + + System.out.println("first schedule start"); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss"); String date = dateFormat.format(new Date()); @@ -39,8 +34,6 @@ public class FirstSchedule { .addString("date", date) .toJobParameters(); - Job firstJob = applicationContext.getBean("firstJob", Job.class); - - jobLauncher.run(firstJob, jobParameters); + jobLauncher.run(jobRegistry.getJob("firstJob"), jobParameters); } }