Spring batch FlatFileItemWriter consume a list<object> - spring-batch

I need to write a Step with :
reader JpaPagingItemReader
processor that return a List
writer that writer foreach student that returned from processor a line in csv file.
How it is possible? how write a costum "FlatFileItemWriter" that consume a list and for each object write a line into FileResource?
Thanks

Related

spring batch using ClassifierCompositeItemwriter and additionally write all items to database

I have a project in spring batch, in which I read from a txt file (input data) and according to a validation of the item I read, it should be written in a txt file (output 1) or in another txt file (output 2) I think for this i should use a ClassifierCompositeItemwriter, how i can do to additionally write all items that I read in a database (output 3)?
I must keep in mind that the three outputs have different formats
Thanks!
You can use CompositeItemWriter which will delegate writing to a list of ItemWriter . The order of the ItemWriter list is important as it will call the ItemWriter in order. So make sure ClassifierCompositeItemwriter is before the ItemWriter for writing to database. :
#Bean
public ItemWriter itemWriter(ClassifierCompositeItemwriter classiferWriter ,JdbcBatchItemWriter jdbcWriter){
CompositeItemWriter writer = new CompositeItemWriter();
writer.setDelegates(List.of(classiferWriter,jdbcWriter));
return writer;
}

How to call a rest service in listitemreader then write those list items into a flat file

How to call a rest service in listitemreader then write those list items into a flat file using spring batch.
You batch job steps needs to have the following itemReader, itemProcesor and itemWriter.
listitemreader - read the element of the list
itemProcessor - create an item processor that processes the item read - by making a test call
FlatFileItemWriter - writes the processor record to the file
The Spring batch guide explains the user of reader,writer and processor in good detail - http://docs.spring.io/spring-batch/reference/html/readersAndWriters.html

How to read multiple items in ItemReader

Following is my use case for spring batch.
Reads the input from web service. Web service will return all records.
Process the records.
Write the processed records one by one.
I'm clear about step 2 and 3 but not able to figure out how to implement a reader which can read all the records in one go. How to pass the records one by one to item processor /writer?
Should I be using tasklet instead of reader/writer?
What will your WebService Returns? A collection of object i guess!
Your ItemReader need to loop on this collection and remove items one-by-one then return null when they are all processed.
What #Kik was saying is the rest is handled by Spring batch based on your commit-interval. if you have a commit0interval of 10 for example, your reader will read 10 items, passed those 10 items to the ItemProc. then pass them again after to the writer.
Hope it clarify
EDIT: 1) In Spring Batch you have more than one option to do what you need.
Easy Option, create a custom MyWsItemReader that implements the ItemReader interface.
-Define a method init() in this class that will call your webService and put the results in a collection attribute of MyWsItemReader.
-Implements the method read() from the interface. (read carfully the contact in the doc - you must return null when you passed all the elements of the collection)
-Then, configure a stepListener around the step and implement the beforeStep() method to call the init() of your MyWsItemReader. You can autowire the reader in the listener to accomplish this.
Alternatively, your MyWsItemReader could also implements the InitializingBean. then you would have to implement the afterPropertySet() where you could call the ws and store the result in a private attribute of MyWsItemReader
regards

Executing Spring Batch ItemReader in a loop and pass IN parameters

I am new to Spring Batch. I have following question.
I am using Spring Batch for developing a batch process.
I have a java array with some 'process_id' values in it. What I want to do is for each 'process_id' I need to call database stored procedure using a ItemReader. Can anyone help me to write ItemReader to achieve this?
Thanks for your help.
You should think about your reader as designed :
The reader should provide the ID collection.
It will pass each ID to a writer that will call you stored procedure.
The goal of the reader is to find data to process.
Each data is then send to a writer (or a processor, depends on your batch design).

Passing an Object from LineCallbackHandler to ItemWriter

I am writing a batch application which reads line-by-line from a file, process the content and write to database. Am using FlatFileItemReader for reading from file.
The first line in the file is special (header) which is skipped using linesToSkip and processed using a LineCallbackHandler (HeaderHandler). The HeaderHandler builds a cache using the header information.
Now I want to make use of this cache within my ItemWriter. Am not sure how to pass the cache object I build within HeaderHandler to my ItemWriter. Is there a clean way of doing this?
you have at least 2 possibilities:
use the StepExecutionContext as temporary memory for your data, take a look at the spring batch doc:
passing data to future steps (just ignore the promotionListener
part) your headerHandler writes the cache into the context and the
writer pulls the cache from the context
use a spring bean with either a custom DataObjectClass or String, Map, etc. which will be injected in the headerHandler and the (custom)itemWriter both use it accordingly