Announcement Announcement Module
No announcement yet.
Batch job that requires an input file for configuration of itself. Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Batch job that requires an input file for configuration of itself.

    I'm trying to think of the best way to have a batch job take as a parameter, a file that would contain JobParameters.

    For example, an input file could be an xml file with the values:

    Now the location of this file would be passed as a JobParameter to my batch job.

    <job id="myBatchJob">
        <step id="readFileStep" next="nextStep">
                <chunk reader="itemReader" commit-interval="1"/>
        <step id="nextStep">
                <chunk reader="myReader" processor="myProcessor" writer="myWriter"/>
    <bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
        <property name="resource" value="#{jobParameter[input.file]}"/>
        <property name="lineMapper">
            <bean class="com.mycompany.myLineMapper">
    I would like to have the values in the input file available to the steps that execute later. I think this can be achieved by putting the placing the values of the input file into the JobExecutionContext.

    Supposing that one of the successive readers in a step that occurs farther down the line is a JdbcCursorItemReader, how can I inject those values into a PreparedStatementSetter that would be used by the JdbcCursorItemReader.

    I imagine it would be something like..

    <bean id="myReader" class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step">
        <property name="dataSource" ref="dataSource"/>
        <property name="sql" value="select * from TABLE where username=? and password=? and projectName=?"/>
        <property name="rowMapper">
            <bean class="com.mycompany.MyMapper"/>
        <property name="preparedStatementSetter">
             <bean class="com.mycompany.MyParameterSetter">
                 <property name="username" value="#{jobExecutionContext['username']}"/>
                 <property name="password" value="#{jobExecutionContext['password']}"/>
                 <property name="password" value="#{jobExecutionContext['projectName']}"/>
    I guess I want to know if this approach is correct, and if an easier approach might somehow exist? I apologize for any syntax errors, but I'm just slapping this xml together from what's in my head since I don't have my real code right in front of me.

  • #2
    I think it is a good approach. You have a XML that contains some meta data fro the job. But where are the job parameters you are going to use to run the job. I see that you have some user name password etc are these your job parameters for the job ?
    How are you launching your actually my question ?


    • #3
      I didn't include it in my post, but I'm making use of the spring batch admin web app to upload my "job configuration" file which will start my job. I followed the sample job packaged with the spring batch admin.

      I'll also say, after working through this problem more, I found that I had to make my own file reader class because the standard FlatFileItemReader just isn't robust enough to parse xml. Maybe I'm wrong on this, but personally, I hadn't been able to find any easy way to read in an xml file.

      The passing along of my parameters to other "steps" worked like a charm though.


      • #4
        Now I am lil confused. Why are you using a FlatFileReader to parse XML ?
        Look at
        6.7. XML Item Readers and Writers


        • #5
          You're right. The flatfileitemreader doesn't seem to be the right fit for what I'm looking for. Looking into the user guide more, there seems to be an item reader that's specifically made to read xml files (StaxItemReader or something named along those lines.). I'll try to make use of that and see if it works.