Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: How to pass data from one step to next step?

  1. #1
    Join Date
    Sep 2009
    Posts
    17

    Default How to pass data from one step to next step?

    This is another newbie question.

    I need to pass data created in a item writer in one step to a tasklet configured in the next step. How do I do that? Thanks.

  2. #2
    Join Date
    Apr 2008
    Posts
    174

    Default

    Did you try defining a singleton bean in spring?

  3. #3
    Join Date
    Sep 2009
    Posts
    17

    Default

    Thanks. Singleton bean should work, however, this would limit multi-threaded parallel batch processing.

    I found the answer towards the end of the reference documentation, the right way is to use execution context. I'll try it out.

  4. #4
    Join Date
    Sep 2009
    Posts
    17

    Default

    There still is one problem. Looks like @BeforeStep annotation only works for item writers, not for a custom Tasklet. How do I obtain reference to the execution context from within a custom Tasklet? My thinking is if execution context can be injected to item writers, it should also be available to Tasklets because Tasklets is a simpler form that combines reader, processor and writer.

    Advice? Thanks.

  5. #5
    Join Date
    Sep 2009
    Posts
    17

    Default

    Figured out and got it working. From callback public RepeatStatus execute(StepContribution contribution,
    ChunkContext chunkContext) throws Exception {

    via ChunkContext, you can get StepExecution and its context, JobExecution and its context.

  6. #6
    Join Date
    Feb 2008
    Posts
    488

    Default

    For others, the link to the section in the reference docs is: http://static.springsource.org/sprin...aToFutureSteps

    Also, @BeforeStep will work fine on any class. You just have to make sure that you register the bean as a listener in the xml (using the <listener/> element).

    Finally, just using a singleton bean isn't good enough in general because you will have problems if you need to restart. To make the job restartable, you need to use the ExecutionContext to save the data that is being passed so that you don't have to start the job from the very beginning if it fails on the receiving step.

  7. #7

    Post

    Hi, I have a requirement to do the following:
    1) read reference data from a component
    2) read a file which has the first record as the header and the rest as detail recs.
    3) do some processing
    4) write out to another components interface

    I use the ref data to transform header and detail data.

    I wrote my own tasklet to be able to read data from a component. I injected the key name shown below (refDataKey)

    public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
    throws Exception {

    StepContext stepContext = arg1.getStepContext();
    StepExecution stepExecution = stepContext.getStepExecution();
    ExecutionContext executionContext = stepExecution.getExecutionContext();

    executionContext.put(refDataKey, refDataList);

    return RepeatStatus.FINISHED;

    }

    the xml looks like this

    <step id="refdata" next="processFile">
    <tasklet ref="depotReferenceInformationTasklet">
    <listeners>
    <listener ref="promotionListener"/>
    </listeners>
    </tasklet>
    </step>
    <bean id="promotionListener" class="com.tnt.express.dynamicrpp.listener.MyExecu tionContextPromotionListener">
    <property name="keys" value="refDataKey"/>
    </bean>

    I want to use the refDataKey data within LineCallbackHandler when, I'm doing the header processing and within the Item Processor. But, can't find a way of reading the execution context

    I tried extending the ItemStreamSupport and overriding the open, without success

    Any ideas?

  8. #8
    Join Date
    Feb 2008
    Posts
    488

    Default

    First, using the promotion listener is unnecessary on a Tasklet Step since you can just as easily write to the Job's ExecutionContext directly. (This isn't recommended in a chunk-oriented step for other reasons).

    Second, using the open() method won't work because the data you are looking for is in the Job's ExecutionContext (it was put there by the promotion listener), not the Step's. Each Step has its own unique context, so the only way to connect them it through the Job's context. As the documentation shows, this can be accomplished with a BeforeStep method:
    Code:
        @BeforeStep
        public void getMyData(StepExecution stepExecution) {
            JobExecution jobExecution = stepExecution.getJobExecution();
            ExecutionContext jobContext = jobExecution.getExecutionContext();
            this.someObject = jobContext.get("someKey");
        }

  9. #9
    Join Date
    Nov 2009
    Posts
    5

    Default

    I hav tried using the promotion listener but it fails to pass data from one step to another. What could be the problem? I am using springbatch 2.0.3.
    This is my config


    <batch:job id="Job">
    <batch:listeners>
    <batch:listener ref="appJobExecutionListener" />
    </batch:listeners>
    <batch:step id="Step1" next="Step2">
    <batch:tasklet>
    <batch:listeners>
    <batch:listener ref="itemFailureLoggerListener" />
    <batch:listener ref="promotionListener"/>
    </batch:listeners>
    <batch:chunk reader="ItemReader" writer="ItemWriter"
    commit-interval="1000" />
    </batch:tasklet>
    </batch:step>
    <batch:step id="Step2" next="Step3">
    <batch:tasklet ref="callStoredProcTasklet">
    <batch:listeners>
    <batch:listener ref="promotionListener"/>
    </batch:listeners>
    </batch:tasklet>
    </batch:step>
    <batch:step id="Step3">
    <batch:tasklet ref="fileMoveTasklet"/>
    </batch:step>
    </batch:job>

    <bean id="promotionListener" class="org.springframework.batch.core.listener.Exe cutionContextPromotionListener" scope="step">
    <property name="keys" >
    <list>
    <value>current.resource.name</value>
    <value>current.resource.dir.path</value>
    </list>
    </property>
    </bean>
    Last edited by forgetarun; Dec 5th, 2009 at 12:54 AM.

  10. #10
    Join Date
    Feb 2008
    Posts
    488

    Default

    Can you post your code so that we might see what you're doing wrong?

    (Be sure to use CODE tags)

Posting Permissions

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