How to set the resource of MultiResourceItemWriter dynamically from ItemReader in Spring Batch

前端 未结 2 1688
生来不讨喜
生来不讨喜 2020-12-19 19:18

The batch job is:

  • Reading from a csv file

  • Create an xml file for every record(line) in the csv with name Patent.ID.xml(where ID is a field i

2条回答
  •  无人及你
    2020-12-19 20:02

    Create a ItemWriteListener, inject the multiResourceItemWriter bean and bound the listener to your step.
    In ItemWriteListener.beforeWrite() create a new ResourceSuffixCreator object using the item your are going to write as base to create resource extension (suffix).
    MultiResourceItemWriter.resource probably need to be changed as file:xml/ because 1.xml, 2.xml and so on will be appended using custom ResourceSuffixCreator dinamically created for every item you are writing.

    This solution is dirty and (probably) works due to commit-interval=1; if you change my answer (probably) will fail.

    I hope I was clear, English is not my native language.

提交回复
热议问题