Announcement Announcement Module
Collapse
No announcement yet.
Batch job that requires an input file for configuration of itself. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
    <Config>
        <User>
            <Username>John</UserName>
            <Password>1234</Password>
        </User>
        <ProjectName>myProject</ProjectName>
        <Limit>10</Limit>
    </Config>
    Now the location of this file would be passed as a JobParameter to my batch job.

    Code:
    <job id="myBatchJob">
        <step id="readFileStep" next="nextStep">
            <tasklet>
                <chunk reader="itemReader" commit-interval="1"/>
            </tasklet>
        </step>
        <step id="nextStep">
            <tasklet>
                <chunk reader="myReader" processor="myProcessor" writer="myWriter"/>
            </tasklet>
        </step>
    </job>
    
    <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">
        </property>
    </bean>
    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..

    Code:
    <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>
        <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']}"/>
             </bean>
        </property>
    </bean>
    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 job...is actually my question ?

    Comment


    • #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.

      Comment


      • #4
        Now I am lil confused. Why are you using a FlatFileReader to parse XML ?
        Look at
        http://static.springsource.org/sprin...tml#itemReader
        6.7. XML Item Readers and Writers

        Comment


        • #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.

          Comment

          Working...
          X