Announcement Announcement Module
Collapse
No announcement yet.
Forward value from Job to Step Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Forward value from Job to Step

    Hello,

    I'm starting in "Spring Batch" for a new project, so first, I'd like to apologize if my question could seem a bit stupid...

    I'd like to parse a flat file using the "FlatFileItemReader" in a step. This, I find how to do it.
    But, I'd like to provide the name of file to parse into the Job parameters (an not in the property in the xml conf file.
    How can I forward this parameter, from my Job, as a ressource to my Step ?

    Thanks to help me please...or at least, if somebody has a good tutorial about Spring Batch basics.


  • #2
    Finally, I think I almost find a solution.

    I tried this:
    <bean id="itemReader" class="MyItemReader">
    <property name="resource" value="file:${path}/#{jobParameters[file.name]}" />
    </bean>

    and in my reader I put this:
    public class MyItemReader implements ItemReader {

    private Resource resource;

    public Resource getResource() {
    return resource;
    }

    public void setResource(Resource resource) {
    this.resource= resource;
    }

    @Override
    public Person read() throws Exception, UnexpectedInputException, ParseException {

    System.out.println("Ressource exist: " + resource.exists());
    System.out.println(resource.getFilename());

    return null;
    }
    }


    As you can see, I put as less code as possible. As I want to understand where exactly the problem comes from.
    The result i have in the console is:
    "Ressource exist: false"
    "#{jobParameters[file.name]}"

    I don't manage to get the filename, why ? However, I can see it's good in the log for JobParameter.

    Comment


    • #3
      Which version of Spring Batch are you using? The late-binding syntax ("#{jobParameters[file.name]}") only works in 2.0.

      Comment


      • #4
        Yes I have 2.0 version.
        Could the problem come from the fact I don't extend/use the FlatFileItemReader ?

        Comment


        • #5
          You need to declare that the reader has "step scope".

          Code:
          <bean id="itemReader" class="MyItemReader" scope="step">
              <property name="resource" value="file:${path}/#{jobParameters[file.name]}" />
          </bean>
          If you're not using the batch namespace, you'll also need to declare the StepScope bean:

          Code:
          <bean class="org.springframework.batch.core.scope.StepScope" />
          See: http://static.springframework.org/sp...tml#step-scope

          Comment


          • #6
            Yes, it's working now.
            Thanks a lot for your help

            Comment

            Working...
            X