#11 spring batch: job, jobInstance
This commit is contained in:
@@ -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 DBJobConfiguration {
|
||||
|
||||
|
||||
@@ -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 HelloJobConfiguration {
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
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 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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package io.springbatch.basic.job.jobinstance;
|
||||
|
||||
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 JobInstanceConfiguration {
|
||||
|
||||
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");
|
||||
return RepeatStatus.FINISHED;
|
||||
})
|
||||
.build();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package io.springbatch.basic.job.jobinstance;
|
||||
|
||||
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;
|
||||
|
||||
//@Component
|
||||
@RequiredArgsConstructor
|
||||
public class JobInstanceTest implements ApplicationRunner {
|
||||
|
||||
private final JobLauncher jobLauncher;
|
||||
private final Job job;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
|
||||
/*
|
||||
Job + JobParameters 를 가지고 JobInstance 를 생성
|
||||
Job + JobParameters 가 같을 경우 에러 발생
|
||||
|
||||
BATCH_JOB_INSTANCE 테이블에 매핑 된다.
|
||||
JOB_NAME(job), JOB_KEY(jobParameter 해시값) 이 저장(중복 불가)
|
||||
*/
|
||||
JobParameters jobParameters = new JobParametersBuilder()
|
||||
.addString("name", "user2")
|
||||
.toJobParameters();
|
||||
|
||||
jobLauncher.run(job, jobParameters);
|
||||
}
|
||||
}
|
||||
@@ -33,3 +33,5 @@ spring:
|
||||
batch:
|
||||
jdbc:
|
||||
initialize-schema: always
|
||||
job:
|
||||
enabled: false
|
||||
Reference in New Issue
Block a user