Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple itemwriters in Spring batch

I am currently writing a Spring batch where I am reading a chunk of data, processing it and then I wish to pass this data to 2 writers. One writer would simply update the database whereas the second writer will write to a csv file.

I am planning to write my own custom writer and inject the two itemWriters in the customItemWriter and call the write methods of both the item writers in the write method of customItemWriter. Is this approach correct? Are there any ItemWriter implementations available which meet my requirements?

Thanks in advance

like image 855
Pratik Shelar Avatar asked Sep 20 '13 11:09

Pratik Shelar


People also ask

How do I read multiple files in Spring Batch?

You need to use MultiResourceItemReader to read lines from CSV file. It reads items from multiple resources sequentially. FlatFileItemReader<Employee> reader = new FlatFileItemReader<Employee>(); //Set number of lines to skips.

What is ItemWriter in Spring Batch?

ItemWriter. It is the element of the step of a batch process which writes data. An ItemWriter writes one item a time. Spring Batch provides an Interface ItemWriter. All the writers implement this interface.

How do I run a Tasklet in Spring Batch?

In Spring batch, the Tasklet is an interface, which will be called to perform a single task only, like clean or set up resources before or after any step execution. In this example, we will show you how to use Tasklet to clean up the resource (folders) after a batch job is completed.


1 Answers

You can use Spring's CompositeItemWriter and delegate to it all your writers.
here is a configuration example.

like image 92
Oussama Zoghlami Avatar answered Oct 02 '22 19:10

Oussama Zoghlami