Specify Header and Footer for all file created by MultiResourceItemWriter when writing in multiples files -spring batch
How could I add the header and the footer for all files created by MultiResourceItemWriter When writing.
You need to set the header/footer callbacks on your delegate writer. Here is a quick example:
import java.io.IOException;import java.io.Writer;import java.util.Arrays;import org.springframework.batch.core.Job;import org.springframework.batch.core.JobParameters;import org.springframework.batch.core.Step;import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;import org.springframework.batch.core.launch.JobLauncher;import org.springframework.batch.item.ItemReader;import org.springframework.batch.item.ItemWriter;import org.springframework.batch.item.file.FlatFileFooterCallback;import org.springframework.batch.item.file.FlatFileHeaderCallback;import org.springframework.batch.item.file.FlatFileItemWriter;import org.springframework.batch.item.file.ResourceSuffixCreator;import org.springframework.batch.item.file.builder.FlatFileItemWriterBuilder;import org.springframework.batch.item.file.builder.MultiResourceItemWriterBuilder;import org.springframework.batch.item.file.transform.PassThroughLineAggregator;import org.springframework.batch.item.support.ListItemReader;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.FileSystemResource;@Configuration@EnableBatchProcessingpublic class MyJob { @Bean public ItemReader<Integer> itemReader() { return new ListItemReader<>(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); } @Bean public ItemWriter<Integer> itemWriter() { FlatFileItemWriter<Integer> flatFileItemWriter = new FlatFileItemWriterBuilder<Integer>() .lineAggregator(new PassThroughLineAggregator<>()) .name("itemsWriter") .headerCallback(writer -> writer.write("header")) .footerCallback(writer -> writer.write("footer")) .build(); return new MultiResourceItemWriterBuilder<Integer>() .delegate(flatFileItemWriter) .resource(new FileSystemResource("items")) .itemCountLimitPerResource(5) .resourceSuffixCreator(index -> "-" + index + ".txt") .name("multiResourcesWriter") .build(); } @Bean public Job job(JobBuilderFactory jobs, StepBuilderFactory steps) { return jobs.get("job") .start(steps.get("step") .<Integer, Integer>chunk(5) .reader(itemReader()) .writer(itemWriter()) .build()) .build(); } public static void main(String[] args) throws Exception { ApplicationContext context = new AnnotationConfigApplicationContext(MyJob.class); JobLauncher jobLauncher = context.getBean(JobLauncher.class); Job job = context.getBean(Job.class); jobLauncher.run(job, new JobParameters()); }}
This generates two files items-1.txt
and items-2.txt
with the following content:
header12345footer
and
header678910footer
I use spring batch version 3.0.10.RELEASE
This version is not maintained anymore, so I encourage you to upgrade to the latest and greatest v4.3.1. The example above uses Spring Batch v4.3.1 and works as expected.