1

我用 STS 提供的Simple Spring Batch Project试用了 Spring Batch 。我添加了 Spring 的调度功能并使用 PostgreSQL 来存储元数据。

我使用了 ItemReader 和 ItemWriter 的简单配置和示例实现,如下所示。作业被定义为每 15 秒运行一次。虽然在第一次迭代中一切正常,但在随后的迭代中似乎跳过了读写。

这是作业配置:

<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

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

先感谢您!

疼痛的手指

4

1 回答 1

3

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

于 2013-12-20T19:03:39.127 回答