Question

I tried out Spring Batch with the Simple Spring Batch Project provided by STS. I added Spring's scheduling functionality and use PostgreSQL for storing metadata.

I use a simple configuration and an example implementation of ItemReader and ItemWriter, as you can see below. The Job is defined to run every 15 seconds. While everything works well in the first iteration, reading and writing seems to be skipped in the subsequent iterations.

Here is the job-config:

<batch:job id="job1">
    <batch:step id="step1">
        <batch:tasklet start-limit="100" transaction-manager="transactionManager">
            <batch:chunk reader="reader" writer="writer"
                commit-interval="1" />
        </batch:tasklet>
    </batch:step>
</batch:job>

<!-- Run every 15 seconds -->
<task:scheduled-tasks>
    <task:scheduled ref="testTask" method="run"
        cron="*/15 * * * * *" />
</task:scheduled-tasks>

There you can see my reader and writer:

@Component("reader")
public class ExampleItemReader implements ItemReader<String> {

    private String[] input = {"Hello world!","Wow!", "Thats","cool!",null};
    private int index = 0;

    public String read() throws Exception {
        if (index < input.length) {
            return input[index++];
        }
        else {
            return null;
        }
     }
}

@Component("writer")
public class ExampleItemWriter implements ItemWriter<Object> {

     public void write(List<? extends Object> data) throws Exception {
           System.out.println("data: " + data);
     }
}

And this is the console output for iteration 1 and 2:

<Job: [FlowJob: [name=job1]] launched with the following parameters: [{date=Fri Dec 20 17:00:15 CET 2013}]>
<Job execution starting: JobExecution: id=18, version=0, startTime=null, endTime=null, lastUpdated=Fri Dec 20 17:00:15 CET 2013, status=STARTING, exitStatus=exitCode=UNKNOWN;exitDescription=, job=[JobInstance: id=18, version=0,JobParameters=[{date=Fri Dec 20 17:00:15 CET 2013}], Job=[job1]]>
<Resuming state=job1.step1 with status=UNKNOWN>
<Handling state=job1.step1>
<Executing step: [step1]>
<Executing: id=18>
<Starting repeat context.>
<Repeat operation about to start at count=1>
<Preparing chunk execution for StepContext: org.springframework.batch.core.scope.context.StepContext@33f487e7>
<Chunk execution starting: queue size=0>
<Starting repeat context.>
<Repeat operation about to start at count=1>
<Repeat is complete according to policy and result value.>
<[Hello world!]>
<Inputs not busy, ended: false>
<Applying contribution: [StepContribution: read=1, written=1, filtered=0, readSkips=0, writeSkips=0, processSkips=0, exitStatus=EXECUTING]>
<Saving step execution before commit: StepExecution: id=18, version=1, name=step1, status=STARTED, exitStatus=EXECUTING, readCount=1, filterCount=0, writeCount=1 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=>


... output of other chunks ommitted ...


<Repeat is complete according to policy and result value.>
<Step execution success: id=18>
<Step execution complete: StepExecution: id=18, version=7, name=step1, status=COMPLETED, exitStatus=COMPLETED, readCount=4, filterCount=0, writeCount=4 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=5, rollbackCount=0>
<Completed state=job1.step1 with status=COMPLETED>
<Handling state=job1.end1>
<Completed state=job1.end1 with status=COMPLETED>
<Job execution complete: JobExecution: id=18, version=1, startTime=Fri Dec 20 17:00:15 CET 2013, endTime=null, lastUpdated=Fri Dec 20 17:00:15 CET 2013, status=COMPLETED, exitStatus=exitCode=COMPLETED;exitDescription=, job=[JobInstance: id=18, version=0, JobParameters=[{date=Fri Dec 20 17:00:15 CET 2013}], Job=[job1]]>
<Job: [FlowJob: [name=job1]] completed with the following parameters: [{date=Fri Dec 20 17:00:15 CET 2013}] and the following status: [COMPLETED]>
Exit Status : COMPLETED

So far i have no clue what could cause that behaviour. I hope anyone of you guys can tell me what to do in order to make reading and writing take place at any iteration!

Thank you in advance!

achingfingers

Was it helpful?

Solution

Your reader is singleton but should be @Scope("step"). On subsequent runs the if (index < input.length) condition will be always false.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top