More Than Five
More Than Five

Reputation: 10419

Wring to multiple files dynamically in Spring Batch

In Spring batch I configure a file write as such:

@Bean
public FlatFileItemWriter<MyObject> flatFileItemWriter() throws Exception{
    FlatFileItemWriter<MyObject> itemWriter = new FlatFileItemWriter();
    // pass through aggregator just calls toString on any item pass in. 
    itemWriter.setLineAggregator(new PassThroughLineAggregator<>());

    String outputPath = File.createTempFile("output", ".out").getAbsolutePath();
    System.out.println(">>output path=" + outputPath);

    itemWriter.setResource(new FileSystemResource(outputPath));
    itemWriter.afterPropertiesSet();

    return itemWriter;
}

What happens if MyObject is a complex structure that can vary depending on configuration settings etc and I want to generate different parts of that structure to different files.

How do I do this?

Upvotes: 0

Views: 794

Answers (1)

Cummings
Cummings

Reputation: 138

Have you looked at CompositeItemWriter? You may need to have CompositeLineMapper in your reader as well as ClassifierCompositeItemProcessor depending on your needs.

Below is example of a CompositeItemWriter

@Bean
public ItemWriter fileWriter() {
    CompositeItemWriter compWriter = new CompositeItemWriter();

    FlatFileItemWriter<MyObject_data> dataWriter = new FlatFileItemWriter<MyObject_data>();
    FlatFileItemWriter<MyObject_otherdata> otherWriter = new FlatFileItemWriter<MyObject_otherdata>();

    List<ItemWriter> iList = new ArrayList<ItemWriter>();
    iList.add(dataWriter);
    iList.add(otherWriter);
    compWriter.setDelegates(iList);

    return compWriter;
}

Upvotes: 1

Related Questions