使用ClassifierCompositeItemWriter和FlatFileItemWriter写入多个文件

时间:2017-12-12 12:59:23

标签: java spring spring-boot spring-batch

我尝试创建一个弹出批处理作业,该作业将从MySQL数据库中读取,并根据数据库中的值将数据写入不同的文件。我收到一个错误:

org.springframework.batch.item.WriterNotOpenException: Writer must be open before it can be written to
at org.springframework.batch.item.file.FlatFileItemWriter.write(FlatFileItemWriter.java:255)

这是我的ClassifierCompositeItemWriter

ClassifierCompositeItemWriter<WithdrawalTransaction> classifierCompositeItemWriter = new ClassifierCompositeItemWriter<WithdrawalTransaction>();

    classifierCompositeItemWriter.setClassifier(new Classifier<WithdrawalTransaction, 
            ItemWriter<? super WithdrawalTransaction>>() {          
        @Override
        public ItemWriter<? super WithdrawalTransaction> classify(WithdrawalTransaction wt) {
            ItemWriter<? super WithdrawalTransaction> itemWriter = null;
            if(wt.getPaymentMethod().equalsIgnoreCase("PDDTS")) { // condition
                itemWriter = pddtsWriter();                 
            } else {
                itemWriter = swiftWriter();
            }
            return itemWriter;
        }
    });

如您所见,我现在只使用了两个文件编写器。

@Bean("pddtsWriter")
private FlatFileItemWriter<WithdrawalTransaction> pddtsWriter()

@Bean("swiftWriter")
private FlatFileItemWriter<WithdrawalTransaction> swiftWriter()

我还将它们添加为流

@Bean
public Step processWithdrawalTransactions() throws Exception {
    return stepBuilderFactory.get("processWithdrawalTransactions")
            .<WithdrawalTransaction, WithdrawalTransaction> chunk(10)
            .processor(withdrawProcessor())
            .reader(withdrawReader) 
            .writer(withdrawWriter)
            .stream(swiftWriter)
            .stream(pddtsWriter)
            .listener(headerWriter())
            .build();
}

我做错了吗?

0 个答案:

没有答案