From 093604318138f34840a356306b82313a87711375 Mon Sep 17 00:00:00 2001 From: namhyeop Date: Sat, 23 Jul 2022 15:19:20 +0900 Subject: [PATCH] feat : add spring batch file --- 8.Spring_Batch/.idea/8.Spring_Batch.iml | 9 +++ 8.Spring_Batch/.idea/compiler.xml | 19 ++++++ 8.Spring_Batch/.idea/encodings.xml | 6 ++ 8.Spring_Batch/.idea/modules.xml | 8 +++ 8.Spring_Batch/.idea/vcs.xml | 6 ++ .../src/batch.http | 7 ++ .../JobLauncherController.java | 47 ++++++++++++++ .../springbatch_3_1_3_instance/Member.java | 8 +++ .../JobParameterTest.java | 32 +++++++++ .../ExecutionContextTasklet1.java | 37 +++++++++++ .../ExecutionContextTasklet2.java | 30 +++++++++ .../ExecutionContextTasklet3.java | 21 ++++++ .../ExecutionContextTasklet4.java | 18 +++++ .../CustomBatchConfigurer.java | 33 ++++++++++ .../JobRepositoryListener.java | 40 ++++++++++++ 8.Spring_Batch/spring-batch/src/DB/Query.SQL | 2 + .../DBJobConfiguration.java | 61 +++++++++++++++++ .../HelloJobConfiguration.java | 65 +++++++++++++++++++ .../JobInstance/JobConfiguration.java | 57 ++++++++++++++++ 19 files changed, 506 insertions(+) create mode 100644 8.Spring_Batch/.idea/8.Spring_Batch.iml create mode 100644 8.Spring_Batch/.idea/compiler.xml create mode 100644 8.Spring_Batch/.idea/encodings.xml create mode 100644 8.Spring_Batch/.idea/modules.xml create mode 100644 8.Spring_Batch/.idea/vcs.xml create mode 100644 8.Spring_Batch/SpringBatch_3_1_10_JobLauncher/src/batch.http create mode 100644 8.Spring_Batch/SpringBatch_3_1_10_JobLauncher/src/main/java/com/example/springbatch_3_1_3_instance/JobLauncherController.java create mode 100644 8.Spring_Batch/SpringBatch_3_1_10_JobLauncher/src/main/java/com/example/springbatch_3_1_3_instance/Member.java create mode 100644 8.Spring_Batch/SpringBatch_3_1_3_JobParameter/src/main/java/com/example/springbatch_3_1_3_instance/JobParameterTest.java create mode 100644 8.Spring_Batch/SpringBatch_3_1_8 ExecutionContext/src/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet1.java create mode 100644 8.Spring_Batch/SpringBatch_3_1_8 ExecutionContext/src/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet2.java create mode 100644 8.Spring_Batch/SpringBatch_3_1_8 ExecutionContext/src/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet3.java create mode 100644 8.Spring_Batch/SpringBatch_3_1_8 ExecutionContext/src/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet4.java create mode 100644 8.Spring_Batch/SpringBatch_3_1_9_JobRepository/src/main/java/com/example/springbatch_3_1_3_instance/CustomBatchConfigurer.java create mode 100644 8.Spring_Batch/SpringBatch_3_1_9_JobRepository/src/main/java/com/example/springbatch_3_1_3_instance/JobRepositoryListener.java create mode 100644 8.Spring_Batch/spring-batch/src/DB/Query.SQL create mode 100644 8.Spring_Batch/spring-batch/src/main/java/com/example/springbatch/springbatchlecture/DBJobConfiguration.java create mode 100644 8.Spring_Batch/spring-batch/src/main/java/com/example/springbatch/springbatchlecture/HelloJobConfiguration.java create mode 100644 8.Spring_Batch/spring-batch/src/main/java/com/example/springbatch/springbatchlecture/JobInstance/JobConfiguration.java diff --git a/8.Spring_Batch/.idea/8.Spring_Batch.iml b/8.Spring_Batch/.idea/8.Spring_Batch.iml new file mode 100644 index 0000000..d6ebd48 --- /dev/null +++ b/8.Spring_Batch/.idea/8.Spring_Batch.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/8.Spring_Batch/.idea/compiler.xml b/8.Spring_Batch/.idea/compiler.xml new file mode 100644 index 0000000..9da55f3 --- /dev/null +++ b/8.Spring_Batch/.idea/compiler.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/8.Spring_Batch/.idea/encodings.xml b/8.Spring_Batch/.idea/encodings.xml new file mode 100644 index 0000000..7e278c3 --- /dev/null +++ b/8.Spring_Batch/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/8.Spring_Batch/.idea/modules.xml b/8.Spring_Batch/.idea/modules.xml new file mode 100644 index 0000000..3149240 --- /dev/null +++ b/8.Spring_Batch/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/8.Spring_Batch/.idea/vcs.xml b/8.Spring_Batch/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/8.Spring_Batch/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/8.Spring_Batch/SpringBatch_3_1_10_JobLauncher/src/batch.http b/8.Spring_Batch/SpringBatch_3_1_10_JobLauncher/src/batch.http new file mode 100644 index 0000000..87f39cb --- /dev/null +++ b/8.Spring_Batch/SpringBatch_3_1_10_JobLauncher/src/batch.http @@ -0,0 +1,7 @@ +### Send POST request with json body +POST http://localhost:8080/batch +Content-Type: application/json + +{ +"id":"leaven" +} \ No newline at end of file diff --git a/8.Spring_Batch/SpringBatch_3_1_10_JobLauncher/src/main/java/com/example/springbatch_3_1_3_instance/JobLauncherController.java b/8.Spring_Batch/SpringBatch_3_1_10_JobLauncher/src/main/java/com/example/springbatch_3_1_3_instance/JobLauncherController.java new file mode 100644 index 0000000..f6e3e87 --- /dev/null +++ b/8.Spring_Batch/SpringBatch_3_1_10_JobLauncher/src/main/java/com/example/springbatch_3_1_3_instance/JobLauncherController.java @@ -0,0 +1,47 @@ +package com.example.springbatch_3_1_3_instance; + +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.batch.core.launch.support.SimpleJobLauncher; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.batch.BasicBatchConfigurer; +import org.springframework.core.task.SimpleAsyncTaskExecutor; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Date; + +@RestController +public class JobLauncherController { + @Autowired + private Job job; + + @Autowired + private JobLauncher jobLauncher; + + @Autowired + private BasicBatchConfigurer basicBatchConfigurer; + + @PostMapping("/batch") + public String launch(@RequestBody Member member) throws Exception { + JobParameters jobParameters = new JobParametersBuilder() + .addString("id", member.getId()) + .addDate("date", new Date()) + .toJobParameters(); + + SimpleJobLauncher jobLauncher = (SimpleJobLauncher) basicBatchConfigurer.getJobLauncher(); + /** + * 아래 주석처리한 방식은 불가능함. Spring이 proxy객체로 생성하기 때문에 타입캐스팅이 불가능함 + */ +// SimpleJobLauncher jobLauncher = (SimpleJobLauncher) basicBatchConfigurer; + /** + * 비동기 설정 옵션. 기본 값은 동기로 실행된다. + */ +// jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor()); + jobLauncher.run(job, jobParameters); + return "batch completed"; + } +} diff --git a/8.Spring_Batch/SpringBatch_3_1_10_JobLauncher/src/main/java/com/example/springbatch_3_1_3_instance/Member.java b/8.Spring_Batch/SpringBatch_3_1_10_JobLauncher/src/main/java/com/example/springbatch_3_1_3_instance/Member.java new file mode 100644 index 0000000..3741734 --- /dev/null +++ b/8.Spring_Batch/SpringBatch_3_1_10_JobLauncher/src/main/java/com/example/springbatch_3_1_3_instance/Member.java @@ -0,0 +1,8 @@ +package com.example.springbatch_3_1_3_instance; + +import lombok.Data; + +@Data +public class Member { + private String id; +} diff --git a/8.Spring_Batch/SpringBatch_3_1_3_JobParameter/src/main/java/com/example/springbatch_3_1_3_instance/JobParameterTest.java b/8.Spring_Batch/SpringBatch_3_1_3_JobParameter/src/main/java/com/example/springbatch_3_1_3_instance/JobParameterTest.java new file mode 100644 index 0000000..337ab59 --- /dev/null +++ b/8.Spring_Batch/SpringBatch_3_1_3_JobParameter/src/main/java/com/example/springbatch_3_1_3_instance/JobParameterTest.java @@ -0,0 +1,32 @@ +package com.example.springbatch_3_1_3_instance; + +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.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; + +import java.util.Date; + +@Component +public class JobParameterTest implements ApplicationRunner { + + @Autowired + JobLauncher jobLauncher; + @Autowired + Job job; + + @Override + public void run(ApplicationArguments args) throws Exception { + JobParameters jobParameters = new JobParametersBuilder() + .addString("name", "user1") + .addLong("seq", 1L) + .addDate("date", new Date()) + .addDouble("age", 16.5) + .toJobParameters(); + jobLauncher.run(job, jobParameters); + } +} diff --git a/8.Spring_Batch/SpringBatch_3_1_8 ExecutionContext/src/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet1.java b/8.Spring_Batch/SpringBatch_3_1_8 ExecutionContext/src/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet1.java new file mode 100644 index 0000000..3dea3ec --- /dev/null +++ b/8.Spring_Batch/SpringBatch_3_1_8 ExecutionContext/src/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet1.java @@ -0,0 +1,37 @@ +package com.example.springbatch_3_1_8_ExecutionContext; + +import org.springframework.batch.core.JobInstance; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.repeat.RepeatStatus; +import org.springframework.stereotype.Component; + +@Component +public class ExecutionContextTasklet1 implements Tasklet { + @Override + public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { + + System.out.println("Step1 was executed"); + ExecutionContext jobExecutionContext = contribution.getStepExecution().getJobExecution().getExecutionContext(); + ExecutionContext stepExecutionContext = contribution.getStepExecution().getExecutionContext(); + + String jobName = chunkContext.getStepContext().getStepExecution().getJobExecution().getJobInstance().getJobName(); + String stepName = chunkContext.getStepContext().getStepExecution().getStepName(); + + //한번도 실행되지 않은 경우 + if (jobExecutionContext.get("jobName") == null) { + jobExecutionContext.put("jobName", jobName); + } + + if (stepExecutionContext.get("stepName") == null) { + stepExecutionContext.put("stepName", stepName); + } + + System.out.println("jobName : " + jobExecutionContext.get("jobName")); + System.out.println("stepName : " + stepExecutionContext.get("stepName")); + + return RepeatStatus.FINISHED; + } +} diff --git a/8.Spring_Batch/SpringBatch_3_1_8 ExecutionContext/src/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet2.java b/8.Spring_Batch/SpringBatch_3_1_8 ExecutionContext/src/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet2.java new file mode 100644 index 0000000..8ccd01a --- /dev/null +++ b/8.Spring_Batch/SpringBatch_3_1_8 ExecutionContext/src/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet2.java @@ -0,0 +1,30 @@ +package com.example.springbatch_3_1_8_ExecutionContext; + +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.repeat.RepeatStatus; +import org.springframework.stereotype.Component; + +@Component +public class ExecutionContextTasklet2 implements Tasklet { + @Override + public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { + System.out.println("Step2 was executed"); + + ExecutionContext jobExecutionContext = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext(); + ExecutionContext stepExecutionContext = chunkContext.getStepContext().getStepExecution().getExecutionContext(); + + System.out.println("jobName = " + jobExecutionContext.get("jobName")); + System.out.println("stepName = " + stepExecutionContext.get("stepName")); + + String stepName = chunkContext.getStepContext().getStepExecution().getStepName(); + + //한번도 실행되지 않은 경우 + if (jobExecutionContext.get("stepName") == null) { + jobExecutionContext.put("stepName", stepName); + } + return RepeatStatus.FINISHED; + } +} diff --git a/8.Spring_Batch/SpringBatch_3_1_8 ExecutionContext/src/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet3.java b/8.Spring_Batch/SpringBatch_3_1_8 ExecutionContext/src/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet3.java new file mode 100644 index 0000000..f5bd51e --- /dev/null +++ b/8.Spring_Batch/SpringBatch_3_1_8 ExecutionContext/src/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet3.java @@ -0,0 +1,21 @@ +package com.example.springbatch_3_1_8_ExecutionContext; + +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.RepeatStatus; +import org.springframework.stereotype.Component; + +@Component +public class ExecutionContextTasklet3 implements Tasklet { + @Override + public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { + System.out.println("Step3 was executed"); + Object name = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().get("name"); + if (name == null) { + chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put("name", "user1"); + throw new RuntimeException("step2 was failed"); + } + return RepeatStatus.FINISHED; + } +} diff --git a/8.Spring_Batch/SpringBatch_3_1_8 ExecutionContext/src/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet4.java b/8.Spring_Batch/SpringBatch_3_1_8 ExecutionContext/src/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet4.java new file mode 100644 index 0000000..24200da --- /dev/null +++ b/8.Spring_Batch/SpringBatch_3_1_8 ExecutionContext/src/main/java/com/example/springbatch_3_1_8_ExecutionContext/ExecutionContextTasklet4.java @@ -0,0 +1,18 @@ +package com.example.springbatch_3_1_8_ExecutionContext; + +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.RepeatStatus; +import org.springframework.stereotype.Component; + +@Component +public class ExecutionContextTasklet4 implements Tasklet { + @Override + public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { + + System.out.println("name : " + chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().get("name")); + System.out.println("step4 was executed"); + return RepeatStatus.FINISHED; + } +} diff --git a/8.Spring_Batch/SpringBatch_3_1_9_JobRepository/src/main/java/com/example/springbatch_3_1_3_instance/CustomBatchConfigurer.java b/8.Spring_Batch/SpringBatch_3_1_9_JobRepository/src/main/java/com/example/springbatch_3_1_3_instance/CustomBatchConfigurer.java new file mode 100644 index 0000000..f059bc6 --- /dev/null +++ b/8.Spring_Batch/SpringBatch_3_1_9_JobRepository/src/main/java/com/example/springbatch_3_1_3_instance/CustomBatchConfigurer.java @@ -0,0 +1,33 @@ +package com.example.springbatch_3_1_3_instance; + +import org.springframework.batch.core.configuration.annotation.BatchConfigurer; +import org.springframework.batch.core.launch.JobLauncher; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean; +import org.springframework.boot.autoconfigure.batch.BasicBatchConfigurer; +import org.springframework.boot.autoconfigure.batch.BatchProperties; +import org.springframework.boot.autoconfigure.transaction.TransactionManagerCustomizers; +import org.springframework.context.annotation.Configuration; + +import javax.sql.DataSource; + +@Configuration +public class CustomBatchConfigurer extends BasicBatchConfigurer { + private final DataSource dataSource; + + public CustomBatchConfigurer(BatchProperties properties, DataSource dataSource, TransactionManagerCustomizers transactionManagerCustomizers) { + super(properties, dataSource, transactionManagerCustomizers); + this.dataSource = dataSource; + } + + @Override + protected JobRepository createJobRepository() throws Exception { + JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); + factory.setDataSource(dataSource); + factory.setTransactionManager(getTransactionManager()); + factory.setIsolationLevelForCreate("ISOLATION_READ_COMMITTED"); + factory.setTablePrefix("SYSTEM_"); + + return factory.getObject(); + } +} diff --git a/8.Spring_Batch/SpringBatch_3_1_9_JobRepository/src/main/java/com/example/springbatch_3_1_3_instance/JobRepositoryListener.java b/8.Spring_Batch/SpringBatch_3_1_9_JobRepository/src/main/java/com/example/springbatch_3_1_3_instance/JobRepositoryListener.java new file mode 100644 index 0000000..6b23c4e --- /dev/null +++ b/8.Spring_Batch/SpringBatch_3_1_9_JobRepository/src/main/java/com/example/springbatch_3_1_3_instance/JobRepositoryListener.java @@ -0,0 +1,40 @@ +package com.example.springbatch_3_1_3_instance; + +import org.springframework.batch.core.*; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * Job이 끝난 경우의 값을 확인하기 위해 JobRepository를 커스터마이징함 + */ +@Component +public class JobRepositoryListener implements JobExecutionListener { + @Autowired + JobRepository jobRepository; + + @Override + public void beforeJob(JobExecution jobExecution) { + + } + + @Override + public void afterJob(JobExecution jobExecution) { + String jobName = jobExecution.getJobInstance().getJobName(); + JobParameters jobParameters = new JobParametersBuilder().addString("requestDate", "20220714").toJobParameters(); + + JobExecution lastJobExecution = jobRepository.getLastJobExecution(jobName, jobParameters); + if (lastJobExecution != null) { + for (StepExecution execution : lastJobExecution.getStepExecutions()) { + BatchStatus status = execution.getStatus(); + System.out.println("status = " + status); + + ExitStatus exitStatus = execution.getExitStatus(); + System.out.println("exitStatus = " + exitStatus); + + String stepName = execution.getStepName(); + System.out.println("stepName = " + stepName); + } + } + } +} diff --git a/8.Spring_Batch/spring-batch/src/DB/Query.SQL b/8.Spring_Batch/spring-batch/src/DB/Query.SQL new file mode 100644 index 0000000..761dcfe --- /dev/null +++ b/8.Spring_Batch/spring-batch/src/DB/Query.SQL @@ -0,0 +1,2 @@ +use +springbatch; diff --git a/8.Spring_Batch/spring-batch/src/main/java/com/example/springbatch/springbatchlecture/DBJobConfiguration.java b/8.Spring_Batch/spring-batch/src/main/java/com/example/springbatch/springbatchlecture/DBJobConfiguration.java new file mode 100644 index 0000000..f73b45d --- /dev/null +++ b/8.Spring_Batch/spring-batch/src/main/java/com/example/springbatch/springbatchlecture/DBJobConfiguration.java @@ -0,0 +1,61 @@ +//package com.example.springbatch.springbatchlecture; +// +//import lombok.RequiredArgsConstructor; +//import org.springframework.batch.core.Job; +//import org.springframework.batch.core.Step; +//import org.springframework.batch.core.StepContribution; +//import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +//import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +//import org.springframework.batch.core.scope.context.ChunkContext; +//import org.springframework.batch.core.step.tasklet.Tasklet; +//import org.springframework.batch.repeat.RepeatStatus; +//import org.springframework.context.annotation.Bean; +//import org.springframework.context.annotation.Configuration; +// +//@RequiredArgsConstructor +//@Configuration +//public class DBJobConfiguration { +// private final JobBuilderFactory jobBuilderFactory; +// private final StepBuilderFactory stepBuilderFactory; +// +// @Bean +// public Job helloJob2(){ +// return this.jobBuilderFactory.get("step2.2job") +// .start(step1()) +// .next(step2()) +// .next(step3()) +// .build(); +// } +// +// @Bean +// public Step step1(){ +// return stepBuilderFactory.get("step1") +// .tasklet(new Tasklet() { +// @Override +// public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { +// System.out.println("step1 has executed"); +// return RepeatStatus.FINISHED; +// } +// }).build(); +// } +// +// @Bean +// public Step step2(){ +// return stepBuilderFactory.get("step2") +// .tasklet((contribution, chunkContext) -> { +// System.out.println("step2 has executed"); +// return RepeatStatus.FINISHED; +// }) +// .build(); +// } +// +// @Bean +// public Step step3(){ +// return stepBuilderFactory.get("step3") +// .tasklet((Contribution, chunkContext) -> { +// System.out.println("step3 has executed"); +// return RepeatStatus.FINISHED; +// }) +// .build(); +// } +//} diff --git a/8.Spring_Batch/spring-batch/src/main/java/com/example/springbatch/springbatchlecture/HelloJobConfiguration.java b/8.Spring_Batch/spring-batch/src/main/java/com/example/springbatch/springbatchlecture/HelloJobConfiguration.java new file mode 100644 index 0000000..d4b43f0 --- /dev/null +++ b/8.Spring_Batch/spring-batch/src/main/java/com/example/springbatch/springbatchlecture/HelloJobConfiguration.java @@ -0,0 +1,65 @@ +//package com.example.springbatch.springbatchlecture; +// +//import lombok.RequiredArgsConstructor; +//import org.springframework.batch.core.Job; +//import org.springframework.batch.core.Step; +//import org.springframework.batch.core.StepContribution; +//import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +//import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +//import org.springframework.batch.core.scope.context.ChunkContext; +//import org.springframework.batch.core.step.tasklet.Tasklet; +//import org.springframework.batch.repeat.RepeatStatus; +//import org.springframework.context.annotation.Bean; +//import org.springframework.context.annotation.Configuration; +// +//@RequiredArgsConstructor +//@Configuration +//public class HelloJobConfiguration { +// private final JobBuilderFactory jobBuilderFactory; +// private final StepBuilderFactory stepBuilderFactory; +// +// @Bean +// public Job helloJob(){ +// return this.jobBuilderFactory.get("step2.job") +// .start(helloStep1()) +// .next(helloStep2()) +// .build(); +// } +// +// @Bean +// public Step helloStep1(){ +// return stepBuilderFactory.get("helloStep1") +// /** +// * 이론에서 말했던 Tasklet 영역이다. +//* * 기본값 으로는 무한 반복이 기본값이다. +// */ +// .tasklet(new Tasklet() { +// /** +// * RepeatStatus가 +// * 객체의 이름이 RepeatStatus것 만큼 이 안에서 동작할 step이 반복적으로 동작할 지 아니면 한 번만 동작할 지를 결정한다. +// * execute의 반환값이 null이라면 한 번 실행되고 정지된다. +// * RepeatStatus와 같은 의미를 가지는것이 RepeatStatus.FINISHED이다. +// * 반복적으로 값을 동작시키고 싶다면 RepeatStatus.CONTINUABLE로 설정하면 된다. +// */ +// @Override +// public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { +// System.out.println("================================="); +// System.out.println(" >> Step1 has executed"); +//// System.out.println(" >> 실시간 상품 조회중"); +// System.out.println("================================="); +// return RepeatStatus.CONTINUABLE; +// } +// }).build(); +// } +// +// @Bean +// public Step helloStep2(){ +// return stepBuilderFactory.get("helloStep2") +// .tasklet((contribution, chunkContext) -> { +// System.out.println("================================="); +// System.out.println(" >> Step2 has executed"); +// System.out.println("================================="); +// return RepeatStatus.FINISHED; +// }).build(); +// } +//} diff --git a/8.Spring_Batch/spring-batch/src/main/java/com/example/springbatch/springbatchlecture/JobInstance/JobConfiguration.java b/8.Spring_Batch/spring-batch/src/main/java/com/example/springbatch/springbatchlecture/JobInstance/JobConfiguration.java new file mode 100644 index 0000000..495217b --- /dev/null +++ b/8.Spring_Batch/spring-batch/src/main/java/com/example/springbatch/springbatchlecture/JobInstance/JobConfiguration.java @@ -0,0 +1,57 @@ +package com.example.springbatch.springbatchlecture.JobInstance; + +import lombok.RequiredArgsConstructor; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.RepeatStatus; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@RequiredArgsConstructor +@Configuration +public class JobConfiguration { + private final JobBuilderFactory jobBuilderFactory; + private final StepBuilderFactory stepBuilderFactory; + + @Bean + public Job job() { + return this.jobBuilderFactory.get("Job").start(step1()).next(step2()).next(step3()).build(); + } + + @Bean + public Step step1() { + return stepBuilderFactory.get("step1").tasklet(new Tasklet() { + @Override + public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { + System.out.println("step1 has executed"); + return RepeatStatus.FINISHED; + } + }).build(); + } + + @Bean + public Step step2() { + return stepBuilderFactory.get("step2") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("step1 has executed"); + return RepeatStatus.FINISHED; + }) + .build(); + } + + @Bean + public Step step3() { + return stepBuilderFactory.get("step3") + .tasklet((stepContribution, chunkContext) -> { + System.out.println("step3 has executed"); + return RepeatStatus.FINISHED; + }) + .build(); + } +} +