Announcement Announcement Module
No announcement yet.
How to use ResourceAware interface from FieldSetMapper Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to use ResourceAware interface from FieldSetMapper

    I'm trying to to access the input filename from my class that implements FieldSetMapper. I've seen references to a new feature in which an item is ResourceAware, but I'm not sure how to implement this feature. At the level of FieldSetMapper, I'm working with a fieldSet, not with an Item instance. Can someone shed some light on this?


  • #2
    In the latest Milestone release of 2.2.0 (2.2.0.M1), there is a new interface called ResourceAware. If the item the MultiResourceItemReader implements the ResourceAware interface, the MultiResourceItemReader will automatically inject a reference to the Resource the item came from. No additional work is needed from your application.


    • #3
      Thanks for your reply Michael. Unfortunately, I don't understand it. I apologize, I should have provided more information in my original post. I have a reader setup like the follow

      <bean name="compoundSensitivityEnrichmentReader" class="org.springframework.batch.item.file.MultiResourceItemReader">
      	<property name="resources" value="${data.location}" />
      	<property name="delegate">
      	  <bean class="org.springframework.batch.item.file.FlatFileItemReader">
      		<property name="lineMapper" ref="compoundSensitivityEnrichmentLineMapper" />
      		<property name="linesToSkip" value="1" />
      At some point, execution makes its way into my class which implements FieldSetMapper:

      public class ObservationDataFieldSetMapper implements FieldSetMapper<ObservationData> {
      	public ObservationData mapFieldSet(FieldSet fieldSet) throws BindException {
      I'd like to be able to determine the resource name so I can use it within mapFieldSet(...). Is this possible? If so, how?



      • #4 need that name within the FieldSetMapper...that is not possible with the off the shelf components. The injection of the Resource occurs after the FieldSetMapper call is complete. My next question would be...why do you need it to map the record to the item?


        • #5
          Ok, you confirmed my suspicion after reading your reply. Without getting into too much detail, I need to create some metadata that is shared across a set of files I'm processing and I wanted to use the filename as a key into a map which contains this metadata. I've come up with an alternate solution, but having access to the input filename would have been nice.


          • #6
            Yeah...the only option for what you are describing is to use the ResourceAware and use the ItemProcessor to enhance the item with any other data.


            • #7
              Ok, thanks again for the info and for whats its worth, spring-batch has been a great framework to work with.