简体   繁体   English

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

[英]Spring batch Step Partitionning with PagingAndSortingRepository

I am trying to configure Spring Batch Steps for partitioning. 我正在尝试配置Spring Batch Steps进行分区。 The nice sample found here shows a partition about "id range", but I don't know where to start for a "data page" range. 这里找到的漂亮示例显示了有关“ id range”的分区,但我不知道从何处开始“ data page”范围。

In my sequential step, I have : 在我的顺序步骤中,我有:

  • reader : a RepositoryItemReader using a PagingAndSortingRepository reader:使用PagingAndSortingRepository的RepositoryItemReader
  • processor : a data converter 处理器:数据转换器
  • writer : a RepositoryItemWriter using a CrudRepository writer:使用CrudRepository的RepositoryItemWriter
  • chunck : 5 块:5
  • listener : a StepListener 侦听器:StepListener

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

As I have understood, for partitionning, I have to create a partitioner, then I have a "parent" step that tells to use the partitioner with the child step, then the "child" step with a reader aware of the "pagination" parameters. 据我了解,对于分区,我必须创建一个分区程序,然后执行“父级”步骤,该步骤告诉将分区程序与子级步骤一起使用,然后让“子级”步骤与读者一起了解“分页”参数。

For the TaskExecutor, I think that the ThreadPoolTaskExecutor will fit. 对于TaskExecutor,我认为ThreadPoolTaskExecutor适合。

What is the good way to implement/configure a paritioning based on data "pages" ? 实现/配置基于数据“页面”的分区的好方法是什么? And what are the threading caveeats I should check ? 我应该检查哪些线程警告?

Thanks :) 谢谢 :)

Each partition has its own item reader and item writer instances. 每个分区都有自己的项目读取器和项目写入器实例。 Your partition implementation will find min max values of a data load. 您的分区实现将找到数据加载的最小最大值。 Using your own logic you can create min and max values in the execution context. 您可以使用自己的逻辑在执行上下文中创建最小值和最大值。 While querying the data base you can make use of these to handle specific slice of the data so that no concurrency issues takes place. 在查询数据库时,您可以利用它们来处理数据的特定部分,从而不会发生并发问题。

@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