如何使用注释在spring批处理中运行多个作业

Hur*_*rix 5 java spring spring-batch spring-boot

我正在使用Spring Boot + Spring Batch(注释),遇到了我必须运行2个作业的场景.

我有需要使用spring批处理更新的员工和薪资记录.我已经BatchConiguration按照本教程的Spring-batch入​​门教程为Employee和Salary对象配置了类,分别命名为BatchConfigurationEmployee和BatchConfigurationSalary.

我已经定义了ItemReader,ItemProcessor,ItemWriterJob按照这上面已经提到的教程.

当我启动我的Spring Boot应用程序时,我想要运行两个BatchConfigured类.我怎样才能做到这一点

********* BatchConfigurationEmployee.java *************

@Configuration
@EnableBatchProcessing
public class BatchConfigurationEmployee {
    public ItemReader<employee> reader() {
        return new EmployeeItemReader();
    }

    @Bean
    public ItemProcessor<Employee, Employee> processor() {
        return new EmployeeItemProcessor();
    }

    @Bean   
    public Job Employee(JobBuilderFactory jobs, Step s1) {
        return jobs.get("Employee")
                .incrementer(new RunIdIncrementer())
                .flow(s1)
                .end()
                .build();
    }

    @Bean
    public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Employee> reader,
                    ItemProcessor<Employee, Employee> processor) {
        return stepBuilderFactory.get("step1")
                .<Employee, Employee> chunk(1)
                .reader(reader)
                .processor(processor)
                .build();
    }
}
Run Code Online (Sandbox Code Playgroud)

薪资等级在这里

@Configuration
@EnableBatchProcessing
public class BatchConfigurationSalary {
    public ItemReader<Salary> reader() {
        return new SalaryItemReader();
    }

    @Bean
    public ItemProcessor<Salary, Salary> processor() {
        return new SalaryItemProcessor();
    }

    @Bean
    public Job salary(JobBuilderFactory jobs, Step s1) {
        return jobs.get("Salary")
                .incrementer(new RunIdIncrementer())
                .flow(s1)
                .end()
                .build();
    }

    @Bean
    public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<Salary> reader,
                    ItemProcessor<Salary, Salary> processor) {
        return stepBuilderFactory.get("step1")
                .<Salary, Salary> chunk(1)
                .reader(reader)
                .processor(processor)
                .build();
    }
}
Run Code Online (Sandbox Code Playgroud)

Han*_*ier 10

Bean的名称在整个Spring Context中必须是唯一的.

在这两个作业中,您使用相同的方法名实例化读取器,写入器和处理器.methodname是用于在上下文中标识bean的名称.

在两个作业定义中,您都有reader(),writer()和processor().他们会互相覆盖.给他们一些唯一的名称,如readerEmployee(),readerSalary()等.

那应该可以解决你的问题.