Announcement Announcement Module
Collapse
No announcement yet.
Dynamic file name for FileItemRead Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamic file name for FileItemRead

    I am looking for a way to kick off a batch which has a filename as an input parameter.
    the filename would be used by the FileItemReader to open the file and process it.

    I have not found any good (for me) explanations about how to do it. I know that I should set the resource parameter of my file reader as follows:
    <property name="resource"value="/data/input/${file.name}" />
    but I still don't get how that works. Do I need to create a variable file.name in my parameter list?

    Thanks

    Xav

  • #2
    It's covered in Chapter 3 of the reference documentation:

    http://static.springframework.org/sp...e.html#d0e1694

    Please let me know if after reading the docs you are still having issues. We're always trying to improve them.

    Comment


    • #3
      Thanks.

      I see the doc has improved a lot recently.

      Xav

      Comment


      • #4
        Hello.

        The link is broken, and I couldn't find an explanation of how to use a batch parameter as a resource path (I end up using a custom laucher registering the
        parameter as a System property, combined with a PropertyPlaceholderConfigurer).

        Comment


        • #5
          You'll want to use late-binding:
          http://static.springframework.org/sp...l#late-binding

          Comment


          • #6
            Thank you.

            I already try that syntax :

            Code:
            <property name="resource" value="#{jobParameters[input.file.name]}" />
            But I must be missing something, the el expression is not interpreted and I end up with something like :

            Code:
            at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:207)
            	at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:254)
            Caused by: java.lang.IllegalStateException: Input resource must exist (reader is in 'strict' mode): class path resource [#{jobParameters[input.file.name]}]
            	at org.springframework.batch.item.file.FlatFileItemReader.doOpen(FlatFileItemReader.java:245)
            	at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:108)
            	... 18 more
            Maybe there's an additional configuration required to use this feature ?

            Comment


            • #7
              Make sure that your <bean/> has scope="step"

              Comment


              • #8
                It's working now, thank you very much !

                Here's the final config :

                Code:
                <bean id="theReader" class="org.springframework.batch.item.file.FlatFileItemReader">
                        <property name="lineMapper">
                            ...
                        </property>        
                        <property name="resource">
                            <bean class="org.springframework.core.io.FileSystemResource" scope="step">
                                <constructor-arg value="#{jobParameters[inputfilename]}" />
                            </bean>
                        </property>
                    </bean>

                Comment


                • #9
                  It might be useful to add this tip in the reference documentation.

                  Is there a process to submit a documentation update ?

                  Comment


                  • #10
                    The subsequent section in the document is about StepScope, but I agree it might be nice to put a note so that readers are aware that the next section applies to them.

                    All issues (including Documentation issues) should be logged in JIRA: http://jira.springframework.org/browse/BATCH

                    Comment

                    Working...
                    X