I'd like to make a new project with Spring Batch 5.
There is already existing project with Spring Web based on @Scheduled
annotation, and I'd like to convert that business logics to Spring Boot 5.
Here's my fundamental question
I've tried to search tutorials for this task for a week, but I couldn't find what I want.
I attach the source code of the OpenApiJob.class
(Please ignore the Korean letters)
@Configuration
@RequiredArgsConstructor
public class OpenApiJob {
// Job build 및 순서 정의
@Bean
public Job hospitalDataJob(Step step, JobRepository jobRepository) {
return new JobBuilder("myJob",jobRepository)
.incrementer(new RunIdIncrementer())
.flow(step)
.end()
.build();
}
@Bean
@JobScope // JobParameter를 보내므로 설정
public Step openApiFristStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
return new StepBuilder("myStep", jobRepository)
.<HospitalFullDataResponse.HospitalItem[], HospitalFullDataResponse.HospitalItem[]>chunk(1, transactionManager) // Input, Output, chunk 사이즈
.reader(openApiReader())
.processor(dataEditProcessor())
.writer(dataInsertWrite())
.build();
}
// 데이터를 읽어오는 ItemReader 인터페이스의 커스텀 구현체
@Bean
@StepScope
public OpenApiReader openApiReader(){
return new OpenApiReader();
}
// 읽어온 데이터를 가공 후 반환하는 ItemProcessor 인터페이스의 커스텀 구현체
@Bean
@StepScope
public OpenApiProcessor dataEditProcessor() {
return new OpenApiProcessor();
}
// 가공 되어진 데이터들(Chunk)를 DB 혹은 특정 파일에 작성하는 ItemWriter 인터페이스의 커스텀 구현체
@Bean
@StepScope
public OpenApiWriter dataInsertWrite() {
return new OpenApiWriter();
}
}
How can I run hospitalDataJob
with scheduling? As far as I searched, I may need to implement the JobLauncher, but I really cannot find how to do that.
Any recommendations and answers will be helpful.
There is already existing project with Spring Web based on @Scheduled
annotation, and I'd like to convert that business logics to Spring Boot 5. But I don't know how to launch the job periodically.
#1 update
I attach further more information about the project.
application.yml
spring:
batch:
jdbc:
schema: classpath:org/springframework/batch/core/schema-mysql.sql
initialize-schema: always
job:
enabled: false
BatchApplication.java
@SpringBootApplication
@EnableBatchProcessing
@EnableScheduling
public class BatchApplication {
public static void main(String[] args) {
SpringApplication.run(BatchApplication.class, args);
}
}
There is no need to implement a JobLauncher
. You can define a scheduled method that runs periodically according to the required schedule and launch the job in it using the built-in JobLauncher
. Here is an example:
@SpringBootApplication
@EnableBatchProcessing
@EnableScheduling
public class BatchApplication {
public static void main(String[] args) {
SpringApplication.run(BatchApplication.class, args);
}
@Scheduled(cron = "* 10 * * * *") // define schedule as needed
public void runJob(@Autowired JobLauncher jobLauncher, @Autowired Job job) throws Exception {
JobParameters parameters = new JobParameters();
// add parameters as needed
jobLauncher.run(job, parameters);
}
}