春季批处理将不同类别的步骤分开

时间:2019-04-03 22:28:09

标签: java spring-batch javabeans devops

您好如何在不同的类中分离步骤。

 @Configuration
public class JobBatch {
 @Bean
public Job jobCC5(JobBuilderFactory jobBuilders, StepBuilderFactory stepBuilders) throws IOException {
    return jobBuilders.get("jobCC5").start(chStep1(stepBuilders)).build();              }
  }

 @Configuration
public class Step1{
@Bean
public Step Step1(StepBuilderFactory stepBuilders) throws IOException {return stepBuilders.get("step1").<CSCiviqueDTO, String>chunk(100)
    .reader(readerStep1VerifLenghtNir(null)).processor(processorStep1())        .writer(writerStep1(null)).faultTolerant().skipLimit(9).skip(Exception.class).build();
}

1 个答案:

答案 0 :(得分:0)

这里是一个例子。您可以在其中定义步骤的类中创建一个类:

import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class StepConfig {

    @Autowired
    private StepBuilderFactory steps;

    @Bean
    public Step step() {
        return steps.get("step")
                .tasklet((contribution, chunkContext) -> {
                    System.out.println("hello world");
                    return RepeatStatus.FINISHED;
                })
                .build();
    }
}

然后在您的作业配置中导入该类:

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@EnableBatchProcessing
@Configuration
@Import(StepConfig.class)
public class JobConfig {

    @Autowired
    private JobBuilderFactory jobs;

    @Bean
    public Job job(Step step) {
        return jobs.get("job")
                .start(step)
                .build();
    }

}

希望这会有所帮助。