简体   繁体   中英

spring batch separate the steps in different classes

Hello how to separate the steps in different classes.

 @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();
}

Here is an example. You create a class where you define your steps:

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();
    }
}

Then import that class in your job configuration:

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();
    }

}

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM