#11 spring batch: SimpleJob api - start, next, validator
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package io.springbatch.basic.job;
|
||||
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersInvalidException;
|
||||
import org.springframework.batch.core.JobParametersValidator;
|
||||
|
||||
public class CustomJobParametersValidator implements JobParametersValidator {
|
||||
|
||||
@Override
|
||||
public void validate(JobParameters parameters) throws JobParametersInvalidException {
|
||||
if (parameters == null || parameters.getString("name") == null) {
|
||||
throw new JobParametersInvalidException("name parameters is not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
//@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class SimpleJobConfiguration {
|
||||
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
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 StartNextConfiguration {
|
||||
|
||||
private final JobBuilderFactory jobBuilderFactory;
|
||||
private final StepBuilderFactory stepBuilderFactory;
|
||||
|
||||
@Bean
|
||||
public Job batchJob1() {
|
||||
return jobBuilderFactory.get("batchJob1")
|
||||
.start(step1())
|
||||
.next(step2())
|
||||
.next(step3())
|
||||
.build();
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step step3() {
|
||||
return stepBuilderFactory.get("step3")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("step3 was executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
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.core.job.DefaultJobParametersValidator;
|
||||
import org.springframework.batch.repeat.RepeatStatus;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class ValidatorConfiguration {
|
||||
|
||||
private final JobBuilderFactory jobBuilderFactory;
|
||||
private final StepBuilderFactory stepBuilderFactory;
|
||||
|
||||
/*
|
||||
validator()
|
||||
|
||||
- Job 실행에 꼭 필요한 파라미터를 검증하는 용도
|
||||
- DefaultJobParametersValidator 구현체 지원, 인터페이스를 직접 구현 가능
|
||||
|
||||
DefaultJobParametersValidator 흐름
|
||||
SimpleJob -> JobParametersValidator -> JobParameters 검증 실패 -> JobParametersInvalidException
|
||||
-> JobParameters 검증 성공
|
||||
*/
|
||||
@Bean
|
||||
public Job batchJob1() {
|
||||
return jobBuilderFactory.get("batchJob1")
|
||||
.start(step1())
|
||||
.next(step2())
|
||||
.next(step3())
|
||||
// .validator(new CustomJobParametersValidator())
|
||||
.validator(new DefaultJobParametersValidator(new String[]{"name", "date"}, new String[]{"count"}))
|
||||
.build();
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Step step3() {
|
||||
return stepBuilderFactory.get("step3")
|
||||
.tasklet((contribution, chunkContext) -> {
|
||||
System.out.println("step3 was executed");
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user