Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 24

Thread: Itemreader that does nothing

  1. #11
    Join Date
    Feb 2008
    Posts
    488

    Default

    What version of spring batch are you using?

    You'll need to move it from the step's ExecutionContext to the job's. This can be done in an afterStep method:

    Code:
    public ExitStatus afterStep(StepExecution stepExecution) {
    	ExecutionContext stepContext = stepExecution.getExecutionContext();
    	ExecutionContext jobContext = stepExecution.getJobExecution().getExecutionContext();
    	String exitCode = stepExecution.getExitStatus().getExitCode();
    	for (String key : keys) {
    		jobContext.put(key, stepContext.get(key));
    	}
    	return null;
    }
    If you're using Spring Batch 2.0, there is a listener included in the framework to do just this: ExecutionContextPromotionListener

  2. #12
    Join Date
    Mar 2009
    Posts
    11

    Default

    Version 2.0.0.M3.

    Its already in Jobs execution context. Even i am getting

    public RepeatStatus execute(StepContribution stepContribution, AttributeAccessor attributeAccessor)

    for tasklet interface.

    Please suggest how do i proceed.
    Last edited by puneetswarup; Mar 13th, 2009 at 11:00 AM.

  3. #13
    Join Date
    Feb 2008
    Posts
    488

    Default

    You should use 2.0.0.RC1.

    Chunk-oriented steps should not store anything to the Job's ExecutionContext until after the step is complete. This is because the only the Step's ExecutionContext is persisted during step execution. Or in other words, if your step fails half way through step1, you'll lose any information you've saved. Always store to the Step's ExecutionContext during processing and use the promotion listener to move the content afterward.

    For the tasklet:
    Code:
    public RepeatStatus execute(StepContribution stepContribution, AttributeAccessor attributeAccessor) {
        ExecutionContext jobContext = this.stepExecution.getJobExecution().getExecutionContext();
        this.staxEventItemWriter.open(0, false);
        for(Object obj : (List)jobContext.get(MY_KEY)) {
            this.staxEventItemWriter.write(obj)
        }
        this.staxEventItemWriter.close()
    }
    
    @BeforeStep
    public void storeStepExecution(StepExecution stepExecution) {
        this.stepExecution = stepExecution;
    }
    At this point you're making Step2 awfully chunk-oriented-looking, but without the benefits like restartability. Also it's pretty strange to generate a huge object of all your step1 data to be passed to step2. Why not just use a StaxEventItemWriter in step1 to write it out at the same time? Then it would be restartable, cleaner, faster, and use less memory.

  4. #14
    Join Date
    Mar 2009
    Posts
    11

    Default

    Well, the data that i want to write to xml is summary of the processing of 1st step ie only data such as count of records processed and number of failed and as such, thus i moved that part out of step 1 as the writer would be called each time a record is processed there by always overwriting the xml output file.

    In below code snippet how do i inject the stepExecution into the taskLet bean?

  5. #15
    Join Date
    Feb 2008
    Posts
    488

    Default

    Ok, i see now.

    The stepExecution is being injected via the @BeforeStep method as now seen in the snippet.

  6. #16
    Join Date
    Mar 2009
    Posts
    11

    Default

    Sorry but i am not using annotation based approach.

  7. #17
    Join Date
    Feb 2008
    Posts
    488

    Default

    The @BeforeStep annotation is the same thing as implementing StepExecutionListener and saying:
    Code:
    public void beforeStep(StepExecution stepExecution) {
        this.storeStepExecution(stepExecution);
    }

  8. #18
    Join Date
    Mar 2009
    Posts
    11

    Default

    How to define a listener for a tasklet then? I am very new to spring batch and have not referred much of documentation thats why asking such questions..

    I am attaching some code snippet for my job, please suggest

    <bean id="batchjob" parent="simpleJob">
    <property name="name" value="batch job" />
    <property name="steps">
    <list>
    <bean id="step1" parent="simpleStep">
    <property name="itemReader"
    ref="initialReader" />
    <property name="itemProcessor" ref="verifyProcessor" />
    <property name="itemWriter" ref="dummyItemWriter" />
    <property name="commitInterval" value="1000" />
    <property name="listeners">
    <list>
    <ref bean="verifyProcessor" />
    <ref bean="xmlOutputFile" />
    </list>
    </property>
    </bean>

    <bean id="step2" parent="simpleStep">
    <property name="itemReader" ref="dummyItemReader" />
    <property name="itemProcessor" ref="dummyItemProcessor" />
    <property name="itemWriter" ref="actualWriter" />
    <property name="commitInterval" value="1000" />
    <property name="listeners">
    <list>
    <ref bean="dummyItemReader" />
    <ref bean="actualWriter" />
    <ref bean="xmlOutputFile" />
    </list>
    </property>
    </bean>

    <!--
    <bean id="step2" parent="taskletStep"> <property name="tasklet"
    ref="actualWriter" /> </bean>
    -->
    </list>
    </property>
    </bean>

  9. #19
    Join Date
    Feb 2008
    Posts
    488

    Default

    Code:
    public class MyTasklet implements Tasklet, StepExecutionListener { ... }

  10. #20
    Join Date
    Mar 2009
    Posts
    11

    Default

    i meant in the config, do we have listeners property in tasklet too as found in step

    such as

    <property name="listeners">
    <list>
    <ref bean="verifyProcessor" />
    <ref bean="xmlOutputFile" />
    </list>
    </property>

Posting Permissions

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