Spring Batch 5与调度器

huangapple go评论113阅读模式
英文:

Spring Batch 5 With Scheduler

问题

我想使用Spring Batch 5创建一个新项目。
已经有一个基于Spring Web的现有项目,使用@Scheduled注解,我想将业务逻辑转换为Spring Boot 5。

这是我的基本问题

  • 是否可以定期启动作业,例如每天凌晨0点?
  • 是的,根据我的搜索结果,但我在构建带有调度的Spring Batch 5项目方面遇到了困难。

我已经尝试搜索了一周关于这个任务的教程,但我找不到我想要的东西。
我附上了OpenApiJob.class的源代码(请忽略韩文字母)

  1. @Configuration
  2. @RequiredArgsConstructor
  3. public class OpenApiJob {
  4. // 构建作业并定义顺序
  5. @Bean
  6. public Job hospitalDataJob(Step step, JobRepository jobRepository) {
  7. return new JobBuilder("myJob", jobRepository)
  8. .incrementer(new RunIdIncrementer())
  9. .flow(step)
  10. .end()
  11. .build();
  12. }
  13. @Bean
  14. @JobScope // 由于发送JobParameter,因此需要配置
  15. public Step openApiFristStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
  16. return new StepBuilder("myStep", jobRepository)
  17. .<HospitalFullDataResponse.HospitalItem[], HospitalFullDataResponse.HospitalItem[]>chunk(1, transactionManager) // 输入,输出,块大小
  18. .reader(openApiReader())
  19. .processor(dataEditProcessor())
  20. .writer(dataInsertWrite())
  21. .build();
  22. }
  23. // 自定义实现ItemReader接口的数据读取器
  24. @Bean
  25. @StepScope
  26. public OpenApiReader openApiReader() {
  27. return new OpenApiReader();
  28. }
  29. // 自定义实现ItemProcessor接口的读取数据并进行处理的处理器
  30. @Bean
  31. @StepScope
  32. public OpenApiProcessor dataEditProcessor() {
  33. return new OpenApiProcessor();
  34. }
  35. // 自定义实现ItemWriter接口的将处理后的数据(Chunk)写入到数据库或特定文件的写入器
  36. @Bean
  37. @StepScope
  38. public OpenApiWriter dataInsertWrite() {
  39. return new OpenApiWriter();
  40. }
  41. }

我如何使用调度运行hospitalDataJob?根据我的搜索结果,我可能需要实现JobLauncher,但我真的找不到如何做到这一点的方法。

任何建议和答案都将有助于解决问题。

已经有一个基于@Scheduled注解的现有项目,我想将业务逻辑转换为Spring Boot 5。但我不知道如何定期启动作业。

#1 更新

我附上了关于项目的更多信息。

application.yml

  1. spring:
  2. batch:
  3. jdbc:
  4. schema: classpath:org/springframework/batch/core/schema-mysql.sql
  5. initialize-schema: always
  6. job:
  7. enabled: false

BatchApplication.java

  1. @SpringBootApplication
  2. @EnableBatchProcessing
  3. @EnableScheduling
  4. public class BatchApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(BatchApplication.class, args);
  7. }
  8. }
英文:

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

  • Is it possible to launch the job periodically like once a day at 0:00 am?
    -> Yes, as I've searched, but I'm struggling with building a Spring Batch 5 project with Scheduling.

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)

  1. @Configuration
  2. @RequiredArgsConstructor
  3. public class OpenApiJob {
  4. // Job build 및 순서 정의
  5. @Bean
  6. public Job hospitalDataJob(Step step, JobRepository jobRepository) {
  7. return new JobBuilder(&quot;myJob&quot;,jobRepository)
  8. .incrementer(new RunIdIncrementer())
  9. .flow(step)
  10. .end()
  11. .build();
  12. }
  13. @Bean
  14. @JobScope // JobParameter를 보내므로 설정
  15. public Step openApiFristStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
  16. return new StepBuilder(&quot;myStep&quot;, jobRepository)
  17. .&lt;HospitalFullDataResponse.HospitalItem[], HospitalFullDataResponse.HospitalItem[]&gt;chunk(1, transactionManager) // Input, Output, chunk 사이즈
  18. .reader(openApiReader())
  19. .processor(dataEditProcessor())
  20. .writer(dataInsertWrite())
  21. .build();
  22. }
  23. // 데이터를 읽어오는 ItemReader 인터페이스의 커스텀 구현체
  24. @Bean
  25. @StepScope
  26. public OpenApiReader openApiReader(){
  27. return new OpenApiReader();
  28. }
  29. // 읽어온 데이터를 가공 후 반환하는 ItemProcessor 인터페이스의 커스텀 구현체
  30. @Bean
  31. @StepScope
  32. public OpenApiProcessor dataEditProcessor() {
  33. return new OpenApiProcessor();
  34. }
  35. // 가공 되어진 데이터들(Chunk)를 DB 혹은 특정 파일에 작성하는 ItemWriter 인터페이스의 커스텀 구현체
  36. @Bean
  37. @StepScope
  38. public OpenApiWriter dataInsertWrite() {
  39. return new OpenApiWriter();
  40. }
  41. }

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

  1. spring:
  2. batch:
  3. jdbc:
  4. schema: classpath:org/springframework/batch/core/schema-mysql.sql
  5. initialize-schema: always
  6. job:
  7. enabled: false

BatchApplication.java

  1. @SpringBootApplication
  2. @EnableBatchProcessing
  3. @EnableScheduling
  4. public class BatchApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(BatchApplication.class, args);
  7. }
  8. }

答案1

得分: 0

不需要实现JobLauncher。您可以定义一个按照所需计划定期运行的调度方法,并在其中使用内置的JobLauncher启动作业。以下是一个示例:

  1. @SpringBootApplication
  2. @EnableBatchProcessing
  3. @EnableScheduling
  4. public class BatchApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(BatchApplication.class, args);
  7. }
  8. @Scheduled(cron = "0 10 * * * *") // 根据需要定义计划
  9. public void runJob(@Autowired JobLauncher jobLauncher, @Autowired Job job) throws Exception {
  10. JobParameters parameters = new JobParameters();
  11. // 根据需要添加参数
  12. jobLauncher.run(job, parameters);
  13. }
  14. }
英文:

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:

  1. @SpringBootApplication
  2. @EnableBatchProcessing
  3. @EnableScheduling
  4. public class BatchApplication {
  5. public static void main(String[] args) {
  6. SpringApplication.run(BatchApplication.class, args);
  7. }
  8. @Scheduled(cron = &quot;* 10 * * * *&quot;) // define schedule as needed
  9. public void runJob(@Autowired JobLauncher jobLauncher, @Autowired Job job) throws Exception {
  10. JobParameters parameters = new JobParameters();
  11. // add parameters as needed
  12. jobLauncher.run(job, parameters);
  13. }
  14. }

huangapple
  • 本文由 发表于 2023年7月11日 09:58:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定