繁体   English   中英

使用PagingAndSortingRepository进行Spring批处理步骤分区

[英]Spring batch Step Partitionning with PagingAndSortingRepository

我正在尝试配置Spring Batch Steps进行分区。 这里找到的漂亮示例显示了有关“ id range”的分区,但我不知道从何处开始“ data page”范围。

在我的顺序步骤中,我有:

  • reader:使用PagingAndSortingRepository的RepositoryItemReader
  • 处理器:数据转换器
  • writer:使用CrudRepository的RepositoryItemWriter
  • 块:5
  • 侦听器:StepListener

return stepBuilderFactory.get("stepApplicationForm") .<OldApplicationForm, NewApplicationForm>chunk(5) .reader(reader).processor(processor).writer(writer) .listener(listener).build();

据我了解,对于分区,我必须创建一个分区程序,然后执行“父级”步骤,该步骤告诉将分区程序与子级步骤一起使用,然后让“子级”步骤与读者一起了解“分页”参数。

对于TaskExecutor,我认为ThreadPoolTaskExecutor适合。

实现/配置基于数据“页面”的分区的好方法是什么? 我应该检查哪些线程警告?

谢谢 :)

每个分区都有自己的项目读取器和项目写入器实例。 您的分区实现将找到数据加载的最小最大值。 您可以使用自己的逻辑在执行上下文中创建最小值和最大值。 在查询数据库时,您可以利用它们来处理数据的特定部分,从而不会发生并发问题。

@Bean
public Step myMasterStep() {
    return  stepBuilderFactory.get("myMasterStep")
            .partitioner("mySlaveWorker", myPartitioner())
            .partitionHandler(myPartitionHandler()).build();
}


@Bean
    public Step mySlaveWorker() {
        return stepBuilderFactory
                .get("mySlaveWorker")
                .<OldApplicationForm, NewApplicationForm> chunk(5)
                .faultTolerant()
                .listener(MyStepListener())
                .skip(DataAccessException.class)
                .skip(FatalStepExecutionException.class)
                .skip(Exception.class)
                .skipLimit(75)
                .noRollback(DataAccessException.class)
                .noRollback(FatalStepExecutionException.class)
                .noRollback(Exception.class)
                .reader(myDataItemReader())
                .writer(myDataItemWriter()).build();
    }

@Bean
@StepScope
public MyDataItemReader myDataItemReader(
        @Value("#{stepExecutionContext[minId]}") Long minId,
        @Value("#{stepExecutionContext[maxId]}") Long maxId) {
    MyDataItemReader myDataItemReader = new MyDataItemReader();
    myDataItemReader.setPageSize(100);
    myDataItemReader.setMinId(minId);
    myDataItemReader.setMaxId(maxId);
    return myDataItemReader;
}

@Bean
@StepScope
public MyDataItemWriter myDataItemWriter() {
    return new MyDataItemWriter();
}

@Bean
@StepScope
public MyPartitioner myPartitioner() {
    MyPartitioner myPartitioner = new MyPartitioner();
    myPartitioner.setDataSource(dataSource);
return myPartitioner;
}

public class MyStepListener implements SkipListener<OldApplicationForm, NewApplicationForm> {

private static final Logger LOGGER = LoggerFactory.getLogger(MyStepListener.class);


public void onSkipInProcess(OldApplicationForm item, Throwable t) {
    LOGGER.error("onSkipInProcess" + t.getMessage());
}

public void onSkipInRead(Throwable t) {
    LOGGER.error("onSkipInRead " + t.getMessage());
}


public void onSkipInWrite(NewApplicationForm item, Throwable t) {
    //logs
    LOGGER.error("In MyStepListener --> onSkipInProcess" + t.getMessage());
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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