gpt4 book ai didi

java - 带有调度程序 : First iteration succeeds. 的简单 Spring Batch 作业后续迭代失败

转载 作者:行者123 更新时间:2023-11-29 05:32:17 24 4
gpt4 key购买 nike

我使用 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

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

提前致谢!

疼痛的手指

最佳答案

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

关于java - 带有调度程序 : First iteration succeeds. 的简单 Spring Batch 作业后续迭代失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20708305/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com