Announcement Announcement Module
No announcement yet.
Using the current filename in the FieldSetMapper Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using the current filename in the FieldSetMapper

    I have a scenario where I need to extract a date value from the input filename and use it within the FieldSetMapper. I'm using a wildcard value for my input filename and so therefore am using a MultiResourceItemReader to load the input file(s). This means that the resource being used can potentially change within the step execution.

    Essentially, I'm looking for a clean way of saving the filename of the resource in use and making it available to the FieldSetMapper. The way I see it I have two options:

    1) Create a custom ItemReader (and set it as the MultiResourceItemReader delegate) which records the current resource filename by intercepting change to the resource (using the ResourceAwareItemReaderItemStream.setResource(Reso urce)). This custom ItemReader can then inject the filename (or date value) directly into my FieldSetMapper.

    2) Use the ExecutionContext to save the filename by using the update(ExecutionContext) on the MultiResourceItemReader. Then, within the FieldSetMapper extract the value. However to do this I think I'd need to use the ExecutionContextDao directly.

    Option 1 is definitely my preferred approach. But does anyone have any ideas for an alternative approach or improvements to my suggested approaches?

    Thanks for your help.


  • #2
    I usually do option (1): just create a wrapper for the FlatFileItemReader that stores what you need. However, as of Spring Batch 2.0.1, MultiResourceItemReader has a method getCurrentResource() that might be exactly what you need. You can just inject the MultiResourceItemReader into your FieldSetMapper so that the mapper can just call getCurrentResource() whenever it needs to and get the filename. This has the added advantage of working if your MultiResourceItemReader reads from multiple files because getCurrentResource() will always return the current file, not saving the name of the first file.