#11 spring batch: JobLauncher
This commit is contained in:
@@ -14,6 +14,7 @@ repositories {
|
||||
|
||||
dependencies {
|
||||
implementation 'org.springframework.boot:spring-boot-starter-batch'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'mysql:mysql-connector-java:8.0.29'
|
||||
|
||||
compileOnly 'org.projectlombok:lombok'
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package io.springbatch.basic.joblauncher;
|
||||
|
||||
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;
|
||||
|
||||
/*
|
||||
JobLauncher
|
||||
|
||||
배치 Job 을 실행
|
||||
Job, JobParameters 를 인자로 받아 작업 수행 후 JobExecution 을 반환
|
||||
|
||||
동기적 실행
|
||||
- taskExecutor -> SyncTaskExecutor (default)
|
||||
- JobExecution 을 획득하고 배치 처리를 최종 완료한 이후 JobExecution 을 반환
|
||||
(ExitStatus.FINISHED or FAILED )
|
||||
- 스케줄러에 의한 배치처리에 적합
|
||||
|
||||
비동기적 실행
|
||||
- taskExecutor -> SimpleAsyncTaskExecutor
|
||||
- JobExecution 을 획득하고 바로 JobExecution 을 반환 후 배치처리 완료
|
||||
(ExitStatus.UNKNOWN)
|
||||
- Http 요청에 의한 배치처리에 적합합
|
||||
*/
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class JobLauncherConfiguration {
|
||||
|
||||
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) -> {
|
||||
Thread.sleep(10000);
|
||||
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,59 @@
|
||||
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";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package io.springbatch.basic.joblauncher;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Member {
|
||||
|
||||
private String id;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
POST http://localhost:8080/batch/sync
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"id": "test4"
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
POST http://localhost:8080/batch/async
|
||||
Content-Type: application/json
|
||||
|
||||
{
|
||||
"id": "test5"
|
||||
}
|
||||
@@ -34,4 +34,4 @@ spring:
|
||||
jdbc:
|
||||
initialize-schema: always
|
||||
job:
|
||||
enabled: true
|
||||
enabled: false
|
||||
Reference in New Issue
Block a user