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

前端 未结 2 1684
生来不讨喜
生来不讨喜 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 19:58

    I couldn't find a way to make Luca's solution to work, so I extended MultiResourceItemWriter this way:

    public class MyMultipleItemWriter extends MultiResourceItemWriter<MyBeanXml>
    {
        @Override
        public void write(List<? extends MyBeanXml> items) throws Exception
        {
            this.setResourceSuffixCreator(new MySuffixCreator(items.get(0).getField()));
            super.write(items);
        }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题