#11 spring batch: batch initialize setting
This commit is contained in:
@@ -14,7 +14,7 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-batch'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
// implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'mysql:mysql-connector-java:8.0.29'
|
||||
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
|
||||
@@ -1,13 +1,11 @@
|
||||
package io.springbatch.basic.executioncontext;
|
||||
package io.springbatch.basic.domain.executioncontext;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
|
||||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/*
|
||||
JobExecution, StepExecution 객체의 상태를 저장하는 공유 객체
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.executioncontext;
|
||||
package io.springbatch.basic.domain.executioncontext;
|
||||
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.executioncontext;
|
||||
package io.springbatch.basic.domain.executioncontext;
|
||||
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.executioncontext;
|
||||
package io.springbatch.basic.domain.executioncontext;
|
||||
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.executioncontext;
|
||||
package io.springbatch.basic.domain.executioncontext;
|
||||
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.hello;
|
||||
package io.springbatch.basic.domain.hello;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.hello;
|
||||
package io.springbatch.basic.domain.hello;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
@@ -0,0 +1,47 @@
|
||||
package io.springbatch.basic.domain.job;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
|
||||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
//@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class JobConfiguration {
|
||||
|
||||
private final JobBuilderFactory jobBuilderFactory;
|
||||
private final StepBuilderFactory stepBuilderFactory;
|
||||
|
||||
@Bean
|
||||
public Job job() {
|
||||
return jobBuilderFactory.get("job")
|
||||
.start(step1())// SimpleJobBuilder 클래스에 step 저장
|
||||
.next(step2())
|
||||
.build(); // SimpleJob 생성
|
||||
// (스프링부트에서는 JobLauncher 가 생성된 Job 을 자동으로 실행 시킨다)
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step step1() {
|
||||
return stepBuilderFactory.get("step1")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("step1 was executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step step2() {
|
||||
return stepBuilderFactory.get("step2")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("step2 was executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.job.jobexecution;
|
||||
package io.springbatch.basic.domain.job.jobexecution;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.job.jobinstance;
|
||||
package io.springbatch.basic.domain.job.jobinstance;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.job.jobinstance;
|
||||
package io.springbatch.basic.domain.job.jobinstance;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.job.jobparameter;
|
||||
package io.springbatch.basic.domain.job.jobparameter;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.job.jobparameter;
|
||||
package io.springbatch.basic.domain.job.jobparameter;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.joblauncher;
|
||||
package io.springbatch.basic.domain.joblauncher;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
@@ -27,7 +27,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
(ExitStatus.UNKNOWN)
|
||||
- Http 요청에 의한 배치처리에 적합합
|
||||
*/
|
||||
@Configuration
|
||||
//@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class JobLauncherConfiguration {
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
//package io.springbatch.basic.domain.joblauncher;
|
||||
//
|
||||
//import lombok.RequiredArgsConstructor;
|
||||
//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.launch.support.SimpleJobLauncher;
|
||||
//import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
|
||||
//import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
|
||||
//import org.springframework.batch.core.repository.JobRestartException;
|
||||
//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
|
||||
//@RequiredArgsConstructor
|
||||
//public class JobLauncherController {
|
||||
//
|
||||
// private final Job job;
|
||||
// private final JobLauncher jobLauncher;
|
||||
//
|
||||
// private final BasicBatchConfigurer basicBatchConfigurer;
|
||||
//
|
||||
// @PostMapping("/batch/sync")
|
||||
// public String launch(@RequestBody Member member) throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
|
||||
//
|
||||
// JobParameters jobParameters = new JobParametersBuilder()
|
||||
// .addString("id", member.getId())
|
||||
// .addDate("date", new Date())
|
||||
// .toJobParameters();
|
||||
//
|
||||
// jobLauncher.run(job, jobParameters);
|
||||
//
|
||||
// return "sync batch completed";
|
||||
// }
|
||||
//
|
||||
// @PostMapping("/batch/async")
|
||||
// public String asyncLaunch(@RequestBody Member member) throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
|
||||
//
|
||||
// JobParameters jobParameters = new JobParametersBuilder()
|
||||
// .addString("id", member.getId())
|
||||
// .addDate("date", new Date())
|
||||
// .toJobParameters();
|
||||
//
|
||||
// // 비동기식
|
||||
// SimpleJobLauncher jobLauncher = (SimpleJobLauncher) basicBatchConfigurer.getJobLauncher();
|
||||
// jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
|
||||
//
|
||||
// jobLauncher.run(job, jobParameters);
|
||||
//
|
||||
// return "async batch completed";
|
||||
// }
|
||||
//}
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.joblauncher;
|
||||
package io.springbatch.basic.domain.joblauncher;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.jobrepository;
|
||||
package io.springbatch.basic.domain.jobrepository;
|
||||
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.jobrepository;
|
||||
package io.springbatch.basic.domain.jobrepository;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.jobrepository;
|
||||
package io.springbatch.basic.domain.jobrepository;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.*;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.step;
|
||||
package io.springbatch.basic.domain.step;
|
||||
|
||||
import org.springframework.batch.core.StepContribution;
|
||||
import org.springframework.batch.core.scope.context.ChunkContext;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.step;
|
||||
package io.springbatch.basic.domain.step;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
@@ -7,7 +7,6 @@ import org.springframework.batch.core.configuration.annotation.JobBuilderFactory
|
||||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
//@Configuration
|
||||
@RequiredArgsConstructor
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.step.stepcontribution;
|
||||
package io.springbatch.basic.domain.step.stepcontribution;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
@@ -1,4 +1,4 @@
|
||||
package io.springbatch.basic.step.stepexecution;
|
||||
package io.springbatch.basic.domain.step.stepexecution;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
@@ -9,7 +9,7 @@ import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
//@Configuration
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class JobConfiguration {
|
||||
|
||||
@@ -17,12 +17,11 @@ public class JobConfiguration {
|
||||
private final StepBuilderFactory stepBuilderFactory;
|
||||
|
||||
@Bean
|
||||
public Job job() {
|
||||
return jobBuilderFactory.get("job")
|
||||
.start(step1())// SimpleJobBuilder 클래스에 step 저장
|
||||
public Job batchJob1() {
|
||||
return jobBuilderFactory.get("batchJob1")
|
||||
.start(step1())
|
||||
.next(step2())
|
||||
.build(); // SimpleJob 생성
|
||||
// (스프링부트에서는 JobLauncher 가 생성된 Job 을 자동으로 실행 시킨다)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package io.springbatch.basic.job;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.Step;
|
||||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
|
||||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class JobConfiguration2 {
|
||||
|
||||
private final JobBuilderFactory jobBuilderFactory;
|
||||
private final StepBuilderFactory stepBuilderFactory;
|
||||
|
||||
@Bean
|
||||
public Job batchJob2() {
|
||||
return jobBuilderFactory.get("batchJob2")
|
||||
.start(step3())
|
||||
.next(step4())
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step step3() {
|
||||
return stepBuilderFactory.get("step3")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("step3 was executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step step4() {
|
||||
return stepBuilderFactory.get("step4")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("step4 was executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package io.springbatch.basic.joblauncher;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
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.launch.support.SimpleJobLauncher;
|
||||
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
|
||||
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
|
||||
import org.springframework.batch.core.repository.JobRestartException;
|
||||
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
|
||||
@RequiredArgsConstructor
|
||||
public class JobLauncherController {
|
||||
|
||||
private final Job job;
|
||||
private final JobLauncher jobLauncher;
|
||||
|
||||
private final BasicBatchConfigurer basicBatchConfigurer;
|
||||
|
||||
@PostMapping("/batch/sync")
|
||||
public String launch(@RequestBody Member member) throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
|
||||
|
||||
JobParameters jobParameters = new JobParametersBuilder()
|
||||
.addString("id", member.getId())
|
||||
.addDate("date", new Date())
|
||||
.toJobParameters();
|
||||
|
||||
jobLauncher.run(job, jobParameters);
|
||||
|
||||
return "sync batch completed";
|
||||
}
|
||||
|
||||
@PostMapping("/batch/async")
|
||||
public String asyncLaunch(@RequestBody Member member) throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
|
||||
|
||||
JobParameters jobParameters = new JobParametersBuilder()
|
||||
.addString("id", member.getId())
|
||||
.addDate("date", new Date())
|
||||
.toJobParameters();
|
||||
|
||||
// 비동기식
|
||||
SimpleJobLauncher jobLauncher = (SimpleJobLauncher) basicBatchConfigurer.getJobLauncher();
|
||||
jobLauncher.setTaskExecutor(new SimpleAsyncTaskExecutor());
|
||||
|
||||
jobLauncher.run(job, jobParameters);
|
||||
|
||||
return "async batch completed";
|
||||
}
|
||||
}
|
||||
@@ -33,5 +33,7 @@ spring:
|
||||
batch:
|
||||
jdbc:
|
||||
initialize-schema: always
|
||||
# table-prefix: TEST_ # 테이블 prefix
|
||||
job:
|
||||
enabled: false
|
||||
enabled: true # 실행 시점에 job 자동 실행 여부
|
||||
names: ${job.name:NONE} # 설정한 job 만 실행 (실행시점에 바인딩 하여 실행 --job.name=batchJob1)
|
||||
|
||||
Reference in New Issue
Block a user