2

Is it possible to use and chain multiple ItemReaders in spring-batch?

I have a large file that contains several XML records. I now want to use the FlatFileItemReader to read the lines and SeparationPolicy to detect start and end of a xml record.

Then I'd like to use StaxEventItemReader to convert the xml to java classes.

So is it possible to chain both readers?

membersound
  • 81,582
  • 193
  • 585
  • 1,120

2 Answers2

2

You might be able to accomplish this by rolling your own ItemReader which is composed of the other two readers, but i'm not sure this will work with how the stax events are processed (I suspect not). Something like this:

http://forum.spring.io/forum/spring-projects/batch/51940-composite-reader

Another suggestion you might want to consider is to convert this into two steps:

  1. use the FlatFileItemReader to pre-process the file into proper XML and write it out to another file
  2. Read in the XML file from step 1 using StaxEvenItemReader

The upshot of this approach is that if something goes wrong on the stax side, you still have the work from the flat file parsing saved in an intermediate file and you could retry with it.

leeor
  • 17,041
  • 6
  • 34
  • 60
  • The question then is: how can I feed `StaxEventItemReader` by multiple resources? Because it only has on for `setRecouces(res)`. I could then read the xml string proprocessed with `FlatFileItemReader`, write it to `ListItemWriter`, and pass these resources from `listItemWriter.getWrittenItems()` to the stax. But how? – membersound Jan 19 '16 at 14:43
  • @membersound To feed the `StaxEventItemReader` multiple resources, use a `MultiResourceItemReader` : https://docs.spring.io/spring-batch/apidocs/org/springframework/batch/item/file/MultiResourceItemReader.html – Thrax Jan 19 '16 at 15:43
2

Do something like a JsonLineMapper
Write a custom LineMapper; inject a JAXB context and unmarshal line to create your domain object.

Luca Basso Ricci
  • 17,829
  • 2
  • 47
  • 69