spring batch处理数据模板(Reader-Processor-Writer模式)

步骤监听器

java 复制代码
@Component
public class StepListener implements StepExecutionListener {

    private StepExecution stepExecution;
    public StepExecution getStepExecution() {
        return this.stepExecution;
    }

    @Override
    public void beforeStep(StepExecution stepExecution) {
        this.stepExecution = stepExecution;
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        return null;
    }
}

java 复制代码
@Component
@StepScope
public class ProductItemReader implements ItemReader<Product> {
    private Iterator<Product> iterator;
    private final StepListener stepListener;
    private final XxxRepository xxxRepository;
    public ProductItemReader(StepListener stepListener,XxxRepository xxxRepository) {
        this.stepListener = stepListener;
        this.xxxRepository = xxxRepository;
    }

    @Override
    public Product read() {
        // 利用repository读取数据
        this.iterator = xxxRepository.xxx();
        // StepExecution 在 Step 执行时才可用,必须懒初始化
        StepExecution stepExecution = stepListener.getStepExecution();
        ExecutionContext executionContext = stepExecution.getExecutionContext();
        // 可设置STEP内上下文
        return iterator.hasNext() ? iterator.next() : null;
    }
}

处理

java 复制代码
@Slf4j
@StepScope
@Component("productProcessor")
public class ProductProcessor implements ItemProcessor<Product, Product> {

    private final StepListener stepListener;

    public ProductProcessor(@Qualifier("stepListener") StepListener stepListener) {
        this.stepListener = stepListener;
    }

    @Override
    public Product process(Product product) {
        // 从步骤域内读取数据或设置数据
        StepExecution stepExecution = productStepListener.getStepExecution();
        ExecutionContext executionContext = stepExecution.getExecutionContext();
        return product;
    }

}

java 复制代码
@Slf4j
@Component("productWriter")
public class ProductWriter implements ItemWriter<Product> {
    private final XxxRepository repository;
    private final StepListener stepListener;

    public ProductWriter(@Qualifier(value = "productRepository") XxxRepository xxxRepository,
                         @Qualifier(value = "stepListener") StepListener stepListener) {
        this.repository = xxxRepository;
        this.stepListener = stepListener;
    }
    @Override
    public void write(List<? extends Product> items){
        StepExecution stepExecution = this.stepListener.getStepExecution();
        ExecutionContext executionContext = stepExecution.getExecutionContext();

        items.forEach( product ->  {
            ...
        });
    }
}

定义步骤

java 复制代码
@Bean
public Step printStep(){
    TaskletStep taskletStep = stepBuilderFactory
            .get("testStep")
            .listener(stepListener)
            .<Product, Product>chunk(10)
            .reader(productItemReader)
            .processor(productProcessor)
            .writer(productWriter)
            .build();
    return taskletStep;
}

使用步骤

java 复制代码
@Bean
public Job testJob(){
    return jobBuilderFactory.get("productJob")
            .start(printStep())
        		...
            .build();
}