combine CompositeItemProcessor and CompositeItemWriter in spring batch - spring

I have a scenario that following below steps:
1. The reader will get a list A from table A.
2. The processor processes list A to return object A and MimeMessage.
3. The writer writes object A into table B and use MimeMessage to send mail.
Could I use CompositeItemProcessor and CompositeItemWriter for my scenario above? If yes, how can I set the output object in Step configuration and how to define which ItemWriter for send mail will take MimeMessage and ItemWriter for insert database will take object A.
Are there any better solutions for my problem?

how to define which ItemWriter for send mail will take MimeMessage or ItemWriter for insert database will take object A.
You can use a ClassifierCompositeItemWriter. This composite writer uses a Classifier to classify items (Type A or MimeMessage in your case) and calls one of the delegate writers for each type.

Related

How to call procedure from Spring Batch itemwritter and i need capture out put params data from procedute to call one more procedure

I have one item writer in spring batch application to call a stored procedure with 4 input params and two output params. Now I am calling procedure to provide 4 input params data and it is executing fine but I need to capture output params of data. Again I need to call one procedure bypassing these two params of data.
Spring Batch does not provide an item writer that calls a stored procedure. So you need to write a custom writer that does it and handle the output parameter as needed.

How to use Spring batch CompositeItemWriter with different data and having two JdbcBatchItemWritter

Need a solution to write a compositer writer with two JdbcBatchItemWriter and also differ data sets
You can find an example in spring-batch-samples repository. This sample shows how to use a composite item writer with two flat file item writers, but you can adapt it with two jdbc batch item writers.

Spring Batch: Writer is not being called if Reader reads nothing

I have a Step in my job that reads from database A and then writes to database B & C.
If the select statement yields no results i want to expect it to continue to the processor and writer as usual. However, the writer() is not called!
This is because my writer is a Composite item writer which has a writer that updates a control table (database C) to say the reader read no results.
I would obviously have a new Tasklet Step to follow this Step in question, but its a partitioned step.
Is there a configuration property for the Job that allows empty reads to not be marked as 'NOOP' or similar, but as successful?
You should be able to use a StepExecutionListener for this use case instead of an ItemWriter. Within that StepExecutionListner#afterStep you can look at the items read count and if it's 0, do that db update. The writer piece is an ItemWriter, meaning it is intended to be used to write items that have been read.
Create a custom ItemReader that return a sentinel item if no items are read.
Add a custom ItemWriter mapped to sentinel item class where you update the control table.

Spring Batch - Item Writer to update an item/record

I am working on a spring batch application where I read from a stored procedure from database and write it to an xml file.
My writer is a org.springframework.batch.item.xml.StaxEventItemWriter
I am trying to implement a situation in which I find duplicates using this method - Spring Batch how to filter duplicated items before send it to ItemWriter
However, in my situation I don't want to skip a duplicate but override an existing record written to XML by my ItemWriter.
How can I achieve it ?

How to implement Update if Exist else INSERT in Spring Batch

My Batch reads data from one table, processes and writes to another table.
I have MyBatisPagingITemReader, Custom Processor and Writer.
Currently Custom Writer INSERTS the data which is converted in processor and Writer does a BATCH INSERT to the other table.
Now, Reader will read some rows which has to be updated in the other table.In this case my writer should also be capable of batch updating those records.
What is the best way to implement it?
Here is where iam stuck
My Writer
public void write(final List unmodifiableItems) throws Exception {
// unmodifiable items will be a list of row to be inserted.....
}
How will i access the list of records which needs to be UPDATED?

Resources