#11 spring batch: jobParameter, jobExecution
This commit is contained in:
@@ -0,0 +1,57 @@
|
|||||||
|
package io.springbatch.basic.job.jobexecution;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.batch.core.Job;
|
||||||
|
import org.springframework.batch.core.JobParameters;
|
||||||
|
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;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class JobExecutionConfiguration {
|
||||||
|
|
||||||
|
private final JobBuilderFactory jobBuilderFactory;
|
||||||
|
private final StepBuilderFactory stepBuilderFactory;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Job job() {
|
||||||
|
return jobBuilderFactory.get("job")
|
||||||
|
.start(step1())
|
||||||
|
.next(step2())
|
||||||
|
.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");
|
||||||
|
|
||||||
|
/*
|
||||||
|
jobExecution 은 jobInstance 에 대한 한 번의 시도를 의미
|
||||||
|
BATCH_JOB_EXECUTION 테이블에 실행 정보가 저장
|
||||||
|
만약 실행 도중 에러가 발생하면 STATUS 가 FAILED 로 저장되고
|
||||||
|
같은 jobInstance 라도 상태가 COMPLETED 가 없다면 계속 생성
|
||||||
|
*/
|
||||||
|
// throw new RuntimeException("step2 has failed");
|
||||||
|
return RepeatStatus.FINISHED;
|
||||||
|
})
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package io.springbatch.basic.job.jobparameter;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.batch.core.Job;
|
||||||
|
import org.springframework.batch.core.JobParameters;
|
||||||
|
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;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
//@Configuration
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class JobParameterConfiguration {
|
||||||
|
|
||||||
|
private final JobBuilderFactory jobBuilderFactory;
|
||||||
|
private final StepBuilderFactory stepBuilderFactory;
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Job job() {
|
||||||
|
return jobBuilderFactory.get("job")
|
||||||
|
.start(step1())
|
||||||
|
.next(step2())
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Step step1() {
|
||||||
|
return stepBuilderFactory.get("step1")
|
||||||
|
.tasklet((contribution, chunkContext) -> {
|
||||||
|
// jobParameter 값은 contribution, chunkContext 에서 사용 가능
|
||||||
|
JobParameters jobParameters = contribution.getStepExecution().getJobExecution().getJobParameters();
|
||||||
|
jobParameters.getString("name");
|
||||||
|
jobParameters.getLong("seq");
|
||||||
|
jobParameters.getDate("date");
|
||||||
|
jobParameters.getDouble("age");
|
||||||
|
|
||||||
|
Map<String, Object> jobParameters1 = chunkContext.getStepContext().getJobParameters();
|
||||||
|
System.out.println("jobParameters1 = " + jobParameters1);
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package io.springbatch.basic.job.jobparameter;
|
||||||
|
|
||||||
|
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.launch.JobLauncher;
|
||||||
|
import org.springframework.boot.ApplicationArguments;
|
||||||
|
import org.springframework.boot.ApplicationRunner;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
//@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class JobParameterTest implements ApplicationRunner {
|
||||||
|
|
||||||
|
private final JobLauncher jobLauncher;
|
||||||
|
private final Job job;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(ApplicationArguments args) throws Exception {
|
||||||
|
/*
|
||||||
|
ParameterType : date, string, long, double
|
||||||
|
*/
|
||||||
|
JobParameters jobParameters = new JobParametersBuilder()
|
||||||
|
.addString("name", "user1")
|
||||||
|
.addLong("seq", 2L)
|
||||||
|
.addDate("date", new Date())
|
||||||
|
.addDouble("age", 16.5)
|
||||||
|
.toJobParameters();
|
||||||
|
|
||||||
|
jobLauncher.run(job, jobParameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -34,4 +34,4 @@ spring:
|
|||||||
jdbc:
|
jdbc:
|
||||||
initialize-schema: always
|
initialize-schema: always
|
||||||
job:
|
job:
|
||||||
enabled: false
|
enabled: true
|
||||||
Reference in New Issue
Block a user