Multiple itemwriters in Spring batch

spring batch compositeitemwriter example
multiresourceitemwriter example in spring batch
spring batch custom flatfileitemreader example
spring batch, jpa reader example
spring batch write to database
spring batch example
spring batch flatfileitemwriter xml example
spring batch listitemreader example

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

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

ItemReaders and ItemWriters, For example, if writing to a Hibernate DAO, multiple calls to write can be For this scenario, Spring Batch provides the ItemProcessor interface,  Spring Batch includes the following decorators: SynchronizedItemStreamReader – When using an ItemReader that is not thread safe, it can be used to make the ItemReader thread safe. Spring Batch provides a SynchronizedItemStreamReaderBuilder to construct an instance of the SynchronizedItemStreamReader

You don't necessarily have to use xml like the example. If the rest of your code uses annotation, you could simply do the following.

public ItemWriter<T> writerOne(){
    ItemWriter<T> writer = new ItemWriter<T>();
    //your logic here
    return writer;
}

public ItemWriter<T> writerTwo(){
    ItemWriter<T> writer = new ItemWriter<T>();
    //your logic here
    return writer;
}

public CompositeItemWriter<T> compositeItemWriter(){
    CompositeItemWriter writer = new CompositeItemWriter();
    writer.setDelegates(Arrays.asList(writerOne(),writerTwo()));
    return writer;
}

Spring Batch, Spring Batch offers some out of the box decorators that can add additional behavior to your ItemReader and ItemWriter implementations. The destination could  I need to read two different value CSV files and insert them to two different tables in DB simultaniously. I have tried with a single file it works fine for me using spring batch. But, When I use two files.. It throws invalid number of tokens exception. Please let me know how to achieve this using spring batch since I am new to spring and

You were right. SB is heavly based on delegation so using a CompositeItemWriter is the right choice for your needs.

Spring Batch, In this article, take a look at Spring Batch and how to write to multiple public CompositeItemWriter<Customer> itemWriter() throws Exception{. ItemReaders and ItemWriters are constructed before the step starts, so I doubt that Spring Batch can handle this. – Serkan Arıkuşu Sep 18 '12 at 8:13 add a comment |

Java Config way SpringBatch4

@Bean
    public Step step1() {
            return this.stepBuilderFactory.get("step1")
                                    .<String, String>chunk(2)
                                    .reader(itemReader())
                                    .writer(compositeItemWriter())
                                    .stream(fileItemWriter1())
                                    .stream(fileItemWriter2())
                                    .build();
    }

    /**
     * In Spring Batch 4, the CompositeItemWriter implements ItemStream so this isn't
     * necessary, but used for an example.
     */
    @Bean
    public CompositeItemWriter compositeItemWriter() {
            List<ItemWriter> writers = new ArrayList<>(2);
            writers.add(fileItemWriter1());
            writers.add(fileItemWriter2());

            CompositeItemWriter itemWriter = new CompositeItemWriter();

            itemWriter.setDelegates(writers);

            return itemWriter;
    }

Item Writers - Spring Batch, lets a step write items to multiple ItemWriters. Like most things in Spring Batch, the ability to call multiple ItemWriters for each item you process is. quite easy. Is it possible in spring batch to have one reader read the data and that data being split to multiple writers for processing running parallel? Steps: Reader : JdbcCursorItemReader reads 100 records 10 Parallel Writers: Each ItemWriter gets 10 records to process.

Depending on your need, another option is to extend the Writer class and add functionality there. For example, I have a project where I am extending HibernateItemWriter and then overriding write(List items). I then send the objects I am writing along with my sessionFactory to the doWrite method of the Writer: doWrite(sessionFactory, filteredRecords).

So in the example above, I could write to the csv file in my extended class and then the HibernateItemWriter would write to the database. Obviously this might not be ideal for this example, but for certain scenarios it is a nice option.

Spring Batch: Multiple Format Output Writer, A tutorial for solving a business problem with Spring Batch: Spring Batch: Multiple Format Output Writer It does almost the exact same thing as I have done here, only it takes a list of ItemWriters and loops through them in  Spring Batch comes with a simple utility class called CommandLineJobRunner which has a main () method which accepts two arguments. First argument is the spring application context file containing job definition and the second is the name of the job to be executed. Now run as a java application with both two arguments.

spring-batch compositeWriter sample · GitHub, Spring Batch provides ways to write to multiple systems as well as structure a single ItemWriter as a collaborative effort of multiple ItemWriters. This section looks  Multi-itemwriters in Spring batch j'écris actuellement un lot de printemps où je suis en train de lire un morceau de données, de les traiter et puis je souhaite transmettre ces données à 2 rédacteurs.

Item Writers, about the various Item Readers and Item Writers in Spring Batch. This will help Spring to differentiate multiple executions of the same job  For this scenario, Spring Batch provides the ItemProcessor interface: public interface ItemProcessor<I, O> { O process(I item) throws Exception; } An ItemProcessor is very simple; given one object, transform it and return another.

Spring Batch ItemReaders and ItemWriters Example, 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  By Lokesh Gupta | Filed Under: Spring Batch Learn to use ItemProcessor to add business logic after reading the input and before passing it to writer for writing to the file/database. It should be noted that while it’s possible to return a different datatype than the one provided as input, it’s not necessary.

Comments
  • The example was really helpful.
  • Hi Pratik - Could you please share sample code? I'm exhausted in getting solution for this issue.
  • This line broken :-(
  • @Andy here is the new url: github.com/spring-projects/spring-batch/blob/master/…. i updated the answer
  • do you know how to achieve this by multi threading the writers?