Spring Batch 5与调度器

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

Spring Batch 5 With Scheduler

问题

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

这是我的基本问题

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

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

@Configuration
@RequiredArgsConstructor
public class OpenApiJob {

    // 构建作业并定义顺序
    @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) // 输入,输出,块大小
                .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();
    }

    // 自定义实现ItemWriter接口的将处理后的数据(Chunk)写入到数据库或特定文件的写入器
    @Bean
    @StepScope
    public OpenApiWriter dataInsertWrite() {
        return new OpenApiWriter();
    }
}

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

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

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

#1 更新

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

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);
    }
}
英文:

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)

@Configuration
@RequiredArgsConstructor
public class OpenApiJob {

    // Job build 및 순서 정의
    @Bean
    public Job hospitalDataJob(Step step, JobRepository jobRepository) {
        return new JobBuilder(&quot;myJob&quot;,jobRepository)
                .incrementer(new RunIdIncrementer())
                .flow(step)
                .end()
                .build();
    }

    @Bean
    @JobScope    // JobParameter를 보내므로 설정
    public Step openApiFristStep(JobRepository jobRepository, PlatformTransactionManager transactionManager) {
        return new StepBuilder(&quot;myStep&quot;, jobRepository)
                .&lt;HospitalFullDataResponse.HospitalItem[], HospitalFullDataResponse.HospitalItem[]&gt;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);

	}
}

答案1

得分: 0

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

@SpringBootApplication
@EnableBatchProcessing
@EnableScheduling
public class BatchApplication {

    public static void main(String[] args) {
        SpringApplication.run(BatchApplication.class, args);
    }

    @Scheduled(cron = "0 10 * * * *") // 根据需要定义计划
    public void runJob(@Autowired JobLauncher jobLauncher, @Autowired Job job) throws Exception {
        JobParameters parameters = new JobParameters();
        // 根据需要添加参数
        jobLauncher.run(job, parameters);
    }

}
英文:

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 = &quot;* 10 * * * *&quot;) // 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);
    }

}

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:

确定