Announcement Announcement Module
No announcement yet.
Accesing JobParameter from an ItemReader Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Accesing JobParameter from an ItemReader


    It is not really clear in the doc how JobParameter can be accessed from the various components of the framework.
    For instance, I need to access a From/To date in JobParameter from my ItemReader. How would you do that?



  • #2
    There should definitely be a section in the reference documentation about this. Jira appears to be in the middle of crashing right now (or at least I can't access it) once it comes back up I'll make an issue to add it.

    Right now the approach is to make your ItemReader a StepListener, which will give you access to the StepExecution. The StepExecution has the JobParameters. As long as you use one o the step factory beans, this should work for you.

    We've debated about adding something a little more specific, but settled on leaving as is for release 1.0.


    • #3
      Thanks. It works.
      One more thing, how can I chain steps in a job and pass output params from one to the next step?


      • #4
        Originally posted by xnaud View Post
        Thanks. It works.
        One more thing, how can I chain steps in a job and pass output params from one to the next step?
        Easiest way is probably define a bean in your app context and inject it into the artifacts for each step. There is some talk of creating a job-level ExecutionContext object, but that doesn't seem like it'll make it in before 1.0.

        <bean id="jobExecutionContext" class="(blahblah).ExecutionContext" />

        Then you can either:
        a) inject it directly from within your context, e.g.

        <bean id="myItemReader" class="MyCustomItemReader">
        <property name="jobExecutionContext" ref="jobExecutionContext" />
        (...other properties...)

        b) have each collaborator pull it from the application context manually if it needs it, e.g.

        public class MyItemReader extends StepListener, ApplicationContextAware {
        private ApplicationContext appContext;
        private ExecutionContext jobExecutionContext;

        public void beforeStep(StepExecution stepExecution) {
        jobExecutionContext = appContext.getBean("jobExecutionContext");

        (...other methods, including setApplicationContext, etc...)

        As for a more user-friendly solution -- although I thought this already was in JIRA, I couldn't find it, so I created an issue for it -

        If this gets marked as a duplicate, check the comments to find the original request.


        • #5
          Additionally, I encourage all of you who read these forums to browse the open issues on the JIRA site for spring batch and comment on and vote (if you comment in favor of something, don't forget to also vote) for issues that you would like to see resolved. This is the surest way that community feedback will get to the ears of the developers.


          • #6
            Btw, if you follow the below link to BATCH-505, Dave Syer mentions another workaround.

            Namely, you can copy the information from step to step by registering the same StepListener with multiple steps.

            I suppose it's a matter of taste how you want to go about doing it, but Dave is one of the committers so I'd say his method is probably best practice for the time being.


            • #7
              Keep in mind that anything you put in an ExecutionContext will be stored in the database. If it's just a simple file name, it's not too big of a deal, but if there is a lot more data, you may not want to go down that route, and use one of the approaches mentioned by Doug above.