#11 spring batch: SimpleJob api - preventRestart, incrementer
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
package io.springbatch.basic.job;
|
||||||
|
|
||||||
|
import org.springframework.batch.core.JobParameters;
|
||||||
|
import org.springframework.batch.core.JobParametersBuilder;
|
||||||
|
import org.springframework.batch.core.JobParametersIncrementer;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class CustomJobParametersIncrementer implements JobParametersIncrementer {
|
||||||
|
|
||||||
|
static final SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd-hhmmss");
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JobParameters getNext(JobParameters parameters) {
|
||||||
|
|
||||||
|
String id = format.format(new Date());
|
||||||
|
|
||||||
|
return new JobParametersBuilder().addString("run.id", id).toJobParameters();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
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 IncrementerConfiguration {
|
||||||
|
|
||||||
|
private final JobBuilderFactory jobBuilderFactory;
|
||||||
|
private final StepBuilderFactory stepBuilderFactory;
|
||||||
|
|
||||||
|
/*
|
||||||
|
incrementer()
|
||||||
|
|
||||||
|
JobParameters 에서 필요한 값을 증가시켜 다음에 사용될 JobParameters 객체 리턴
|
||||||
|
기존의 JobParameter 변경 없이 Job 을 여러번 시작이 필요할 경우
|
||||||
|
RunIdIncrementer 구현체 지원, 직접 구현 가능
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public Job batchJob1() {
|
||||||
|
return jobBuilderFactory.get("batchJob1")
|
||||||
|
.start(step1())
|
||||||
|
.next(step2())
|
||||||
|
.next(step3())
|
||||||
|
.incrementer(new CustomJobParametersIncrementer())
|
||||||
|
.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,69 @@
|
|||||||
|
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 PreventRestartConfiguration {
|
||||||
|
|
||||||
|
private final JobBuilderFactory jobBuilderFactory;
|
||||||
|
private final StepBuilderFactory stepBuilderFactory;
|
||||||
|
|
||||||
|
/*
|
||||||
|
preventRestart()
|
||||||
|
|
||||||
|
Job 의 재시작 여부 설정 기본값은 true
|
||||||
|
-> true 일 경우 Job 이 실패하면 재시작이 가능
|
||||||
|
-> false 일 경우 Job 이 실패해도 재시작 불가능 (JobRestartException 발생)
|
||||||
|
|
||||||
|
Job 의 실행이 처음이 아닌 경우는 Job 의 성공여부와 관계없이 preventRestart 설정 값으로 실행여부 판단
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public Job batchJob1() {
|
||||||
|
return jobBuilderFactory.get("batchJob1")
|
||||||
|
.start(step1())
|
||||||
|
.next(step2())
|
||||||
|
.next(step3())
|
||||||
|
.preventRestart() // 설정 할 경우 restartable 속성이 true -> false 로 변경
|
||||||
|
.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) -> {
|
||||||
|
// throw new RuntimeException("step3 was failed");
|
||||||
|
System.out.println("step3 was executed");
|
||||||
|
return RepeatStatus.FINISHED;
|
||||||
|
})
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@ import org.springframework.batch.repeat.RepeatStatus;
|
|||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
@Configuration
|
//@Configuration
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class ValidatorConfiguration {
|
public class ValidatorConfiguration {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user