Results 1 to 6 of 6

Thread: Forward value from Job to Step

  1. #1
    Join Date
    Feb 2009
    Location
    Paris
    Posts
    30

    Default 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. #2
    Join Date
    Feb 2009
    Location
    Paris
    Posts
    30

    Default

    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.

  3. #3
    Join Date
    Feb 2008
    Posts
    488

    Default

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

  4. #4
    Join Date
    Feb 2009
    Location
    Paris
    Posts
    30

    Default

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

  5. #5
    Join Date
    Feb 2008
    Posts
    488

    Default

    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

  6. #6
    Join Date
    Feb 2009
    Location
    Paris
    Posts
    30

    Default

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •