简体   繁体   中英

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)

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.

This can be achieved by using Classifier . Below are the configurations:

Writer - Writer will set the Classifer to decide which writer we need to use. Based on classfiter output writer will be decided.

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

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