带调度程序的简单Spring批处理作业:第一次迭代成功。后续迭代失败

时间:2013-12-20 16:49:46

标签: java spring spring-batch spring-tool-suite

我尝试使用STS提供的简单弹簧批处理项目进行Spring Batch。我添加了Spring的调度功能,并使用PostgreSQL存储元数据。

我使用了一个简单的配置和ItemReader和ItemWriter的示例实现,如下所示。作业定义为每15秒运行一次。虽然在第一次迭代中一切运行良好,但在后续迭代中读取和写入似乎被忽略

这是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>

在那里你可以看到我的读者和作家:

@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);
     }
}

这是迭代1和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

到目前为止,我不知道可能导致这种行为的原因。我希望你们中的任何人能告诉我该怎么做才能在任何迭代中进行阅读和写作!

提前谢谢!

achingfingers

1 个答案:

答案 0 :(得分:3)

您的读者是单身,但应该是@Scope("step")。在后续运行中,if (index < input.length)条件将始终为false。

相关问题