简体   繁体   English

在春季批处理中有条件地将数据发送给多个作者

[英]Conditionally send data to multiple writers in spring batch

I have some logic in my processor And based on that I need to decide if I need to write items in TableA (WriterA) or TableB(writer) 我的处理器中有一些逻辑,因此我需要决定是否需要在TableA(WriterA)或TableB(writer)中编写项目

eg Item has filed type and type can have value as A or B and based on value in type filed I need to decide in which table I need to write the data. 例如, Item具有归档type并且类型可以具有A or B值,并且基于归档type值,我需要确定我需要在哪个表中写入数据。

This can be achieved by using Classifier . 这可以通过使用Classifier来实现。 Below are the configurations: 以下是配置:

Writer - Writer will set the Classifer to decide which writer we need to use. 作家 -作家将设置Classifer来决定我们需要使用哪个作家。 Based on classfiter output writer will be decided. 基于classfiter的输出将决定作者。

@Bean
public ItemWriter<Pojo> itemWriter() {
    BackToBackPatternClassifier classifier = new BackToBackPatternClassifier();
    classifier.setRouterDelegate(new MyClassifier());
    classifier.setMatcherMap(new HashMap<String, ItemWriter<? extends Pojo>>() {
        {
            put("A", WriterA);
            put("B", WriterB);

        }
    });
    ClassifierCompositeItemWriter<Pojo> writer = new ClassifierCompositeItemWriter<Pojo>();
    writer.setClassifier(classifier);
    return writer;      
}

Classifier 分类

public class MyClassifier {

    @Classifier
    public String classify(Pojo Pojo) {
        return Pojo.getType();
    }
}

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

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