我有以下工作定义:
<job id="test-restartable-job" job-repository="expimpJobRepository" restartable="true" xmlns="http://www.springframework.org/schema/batch">
<step id="step1">
<tasklet ref="step1Tasklet" transaction-manager="expimpTransactionManager">
<transaction-attributes propagation="REQUIRED"/>
</tasklet>
<stop on="STOPPED" restart="step2"/>
</step>
<step id="step2">
<tasklet ref="step2Tasklet" transaction-manager="expimpTransactionManager">
<transaction-attributes propagation="REQUIRED"/>
</tasklet>
</step>
</job>
我的tasklet如下:
public class Step1Tasklet implements Tasklet{
public Step1Tasklet(){
}
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
contribution.setExitStatus(ExitStatus.STOPPED);
return RepeatStatus.FINISHED;
}
}
public class Step2Tasklet implements Tasklet{
public Step2Tasklet(){
}
@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
contribution.setExitStatus(ExitStatus.COMPLETED);
return RepeatStatus.FINISHED;
}
}
我用相同的参数连续两次调用jobLauncher.run(作业,参数)(你可以第二次调用jobOperator.restart()但结果相同)。这是完整性的日志输出:
2016-09-28 16:45:35,210 DEBUG [main] [org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor@144] Registering job: test-transaction-job
2016-09-28 16:45:35,257 DEBUG [main] [org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor@144] Registering job: test-restartable-job
2016-09-28 16:45:35,456 INFO [main] [org.springframework.batch.core.launch.support.SimpleJobLauncher@133] Job: [FlowJob: [name=test-restartable-job]] launched with the following parameters: [{schemaName=PUBLIC, targetFilePrefix=projectNameExport-, TASKS=null, WIKI=null, SCM=null}]
2016-09-28 16:45:35,457 DEBUG [main] [org.springframework.batch.core.job.AbstractJob@289] Job execution starting: JobExecution: id=1, version=0, startTime=null, endTime=null, lastUpdated=Wed Sep 28 16:45:35 EDT 2016, status=STARTING, exitStatus=exitCode=UNKNOWN;exitDescription=, job=[JobInstance: id=1, version=0, Job=[test-restartable-job]], jobParameters=[{schemaName=PUBLIC, targetFilePrefix=projectNameExport-, TASKS=null, WIKI=null, SCM=null}]
2016-09-28 16:45:35,467 DEBUG [main] [org.springframework.batch.core.job.flow.support.SimpleFlow@157] Resuming state=test-restartable-job.step1 with status=UNKNOWN
2016-09-28 16:45:35,467 DEBUG [main] [org.springframework.batch.core.job.flow.support.SimpleFlow@167] Handling state=test-restartable-job.step1
2016-09-28 16:45:35,477 INFO [main] [org.springframework.batch.core.job.SimpleStepHandler@146] Executing step: [step1]
2016-09-28 16:45:35,477 DEBUG [main] [org.springframework.batch.core.step.AbstractStep@184] Executing: id=1
2016-09-28 16:45:35,484 DEBUG [main] [org.springframework.batch.repeat.support.RepeatTemplate@470] Starting repeat context.
2016-09-28 16:45:35,485 DEBUG [main] [org.springframework.batch.repeat.support.RepeatTemplate@372] Repeat operation about to start at count=1
2016-09-28 16:45:35,485 DEBUG [main] [org.springframework.batch.core.scope.context.StepContextRepeatCallback@69] Preparing chunk execution for StepContext: org.springframework.batch.core.scope.context.StepContext@74bdc868
2016-09-28 16:45:35,486 DEBUG [main] [org.springframework.batch.core.scope.context.StepContextRepeatCallback@79] Chunk execution starting: queue size=0
2016-09-28 16:45:35,489 DEBUG [main] [org.springframework.batch.core.scope.StepScope@109] Creating object in scope=step, name=scopedTarget.step1Tasklet
2016-09-28 16:45:35,498 DEBUG [main] [org.springframework.batch.core.step.tasklet.TaskletStep@438] Applying contribution: [StepContribution: read=0, written=0, filtered=0, readSkips=0, writeSkips=0, processSkips=0, exitStatus=STOPPED]
2016-09-28 16:45:35,501 DEBUG [main] [org.springframework.batch.core.step.tasklet.TaskletStep@454] Saving step execution before commit: StepExecution: id=1, version=1, name=step1, status=STARTED, exitStatus=STOPPED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=
2016-09-28 16:45:35,503 DEBUG [main] [org.springframework.batch.repeat.support.RepeatTemplate@443] Repeat is complete according to policy and result value.
2016-09-28 16:45:35,503 DEBUG [main] [org.springframework.batch.core.step.AbstractStep@215] Step execution success: id=1
2016-09-28 16:45:35,506 DEBUG [main] [org.springframework.batch.core.step.AbstractStep@281] Step execution complete: StepExecution: id=1, version=3, name=step1, status=COMPLETED, exitStatus=STOPPED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0
2016-09-28 16:45:35,507 DEBUG [main] [org.springframework.batch.core.job.flow.support.SimpleFlow@183] Completed state=test-restartable-job.step1 with status=STOPPED
2016-09-28 16:45:35,508 DEBUG [main] [org.springframework.batch.core.job.flow.support.SimpleFlow@167] Handling state=test-restartable-job.stop19
2016-09-28 16:45:35,508 DEBUG [main] [org.springframework.batch.core.job.flow.support.SimpleFlow@183] Completed state=test-restartable-job.stop19 with status=STOPPED
2016-09-28 16:45:35,509 DEBUG [main] [org.springframework.batch.core.job.AbstractJob@308] Job execution complete: JobExecution: id=1, version=1, startTime=Wed Sep 28 16:45:35 EDT 2016, endTime=null, lastUpdated=Wed Sep 28 16:45:35 EDT 2016, status=STOPPED, exitStatus=exitCode=STOPPED;exitDescription=, job=[JobInstance: id=1, version=0, Job=[test-restartable-job]], jobParameters=[{schemaName=PUBLIC, targetFilePrefix=projectNameExport-, TASKS=null, WIKI=null, SCM=null}]
2016-09-28 16:45:35,510 INFO [main] [org.springframework.batch.core.launch.support.SimpleJobLauncher@136] Job: [FlowJob: [name=test-restartable-job]] completed with the following parameters: [{schemaName=PUBLIC, targetFilePrefix=projectNameExport-, TASKS=null, WIKI=null, SCM=null}] and the following status: [STOPPED]
2016-09-28 16:45:35,557 INFO [main] [org.springframework.batch.core.launch.support.SimpleJobOperator@270] Checking status of job execution with id=1
2016-09-28 16:45:35,720 INFO [main] [org.springframework.batch.core.launch.support.SimpleJobOperator@278] Attempting to resume job with name=test-restartable-job and parameters={TASKS=null, WIKI=null, schemaName=PUBLIC, targetFilePrefix=projectNameExport-, SCM=null}
2016-09-28 16:45:35,731 INFO [main] [org.springframework.batch.core.launch.support.SimpleJobLauncher@133] Job: [FlowJob: [name=test-restartable-job]] launched with the following parameters: [{TASKS=null, WIKI=null, schemaName=PUBLIC, targetFilePrefix=projectNameExport-, SCM=null}]
2016-09-28 16:45:35,732 DEBUG [main] [org.springframework.batch.core.job.AbstractJob@289] Job execution starting: JobExecution: id=2, version=0, startTime=null, endTime=null, lastUpdated=Wed Sep 28 16:45:35 EDT 2016, status=STARTING, exitStatus=exitCode=UNKNOWN;exitDescription=, job=[JobInstance: id=1, version=0, Job=[test-restartable-job]], jobParameters=[{TASKS=null, WIKI=null, schemaName=PUBLIC, targetFilePrefix=projectNameExport-, SCM=null}]
2016-09-28 16:45:35,734 DEBUG [main] [org.springframework.batch.core.job.flow.support.SimpleFlow@157] Resuming state=test-restartable-job.step1 with status=UNKNOWN
2016-09-28 16:45:35,734 DEBUG [main] [org.springframework.batch.core.job.flow.support.SimpleFlow@167] Handling state=test-restartable-job.step1
2016-09-28 16:45:35,740 INFO [main] [org.springframework.batch.core.job.SimpleStepHandler@217] Step already complete or not restartable, so no action to execute: StepExecution: id=1, version=3, name=step1, status=COMPLETED, exitStatus=STOPPED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=
2016-09-28 16:45:35,741 DEBUG [main] [org.springframework.batch.core.job.flow.support.SimpleFlow@183] Completed state=test-restartable-job.step1 with status=STOPPED
2016-09-28 16:45:35,741 DEBUG [main] [org.springframework.batch.core.job.flow.support.SimpleFlow@167] Handling state=test-restartable-job.stop19
2016-09-28 16:45:35,741 DEBUG [main] [org.springframework.batch.core.job.flow.support.SimpleFlow@183] Completed state=test-restartable-job.stop19 with status=COMPLETED
2016-09-28 16:45:35,742 DEBUG [main] [org.springframework.batch.core.job.AbstractJob@308] Job execution complete: JobExecution: id=2, version=1, startTime=Wed Sep 28 16:45:35 EDT 2016, endTime=null, lastUpdated=Wed Sep 28 16:45:35 EDT 2016, status=COMPLETED, exitStatus=exitCode=COMPLETED;exitDescription=, job=[JobInstance: id=1, version=0, Job=[test-restartable-job]], jobParameters=[{TASKS=null, WIKI=null, schemaName=PUBLIC, targetFilePrefix=projectNameExport-, SCM=null}]
2016-09-28 16:45:35,744 INFO [main] [org.springframework.batch.core.launch.support.SimpleJobLauncher@136] Job: [FlowJob: [name=test-restartable-job]] completed with the following parameters: [{TASKS=null, WIKI=null, schemaName=PUBLIC, targetFilePrefix=projectNameExport-, SCM=null}] and the following status: [COMPLETED]
2016-09-28 16:45:35,744 INFO [main] [org.springframework.context.support.GenericApplicationContext@982] Closing org.springframework.context.support.GenericApplicationContext@70fb111c: startup date [Wed Sep 28 16:45:32 EDT 2016]; root of context hierarchy
实际上它无法从step2重新开始执行。第一次执行的所有退出状态都是正确的。 stop元素Spring Batch文档声明:
'stop'元素指示Job停止使用BatchStatus 已停止。停止工作可以暂时中断处理 操作员可以在重新启动作业之前执行某些操作。该 'stop'元素需要一个指定步骤的'restart'属性 在重新启动作业时应该执行执行。
在以下场景中,如果step1以COMPLETE结束,那么 然后工作就会停止。重新启动后,将开始执行 步骤2。
这个相同的代码在Spring Batch 2.2.7中工作正常但在我昨天升级到3.0.7后停止工作。昨天我也进行了一些调试,发现FlowBuilder.TransitionBuilder永远不会遇到stopAndRestart()方法。这种特殊的转变也不会出现在转换列表中。
出错了什么?在3.0.7中有什么变化吗?
更新:我刚刚确认一切都按预期在3.0.6中运行。我想这可能是3.0.7中的回归。
答案 0 :(得分:0)
我发现有一个与此相关的未解决的错误 - https://jira.spring.io/browse/BATCH-2315。该错误的评论中提到了一个解决方法。
尝试将属性exit-code=""
添加到<stop>
元素。默认值为STOPPED
。我有一个类似的问题,这为我解决了。
我认为这(显然没有记录)退出状态会覆盖步骤的退出状态。因此,在重新启动时,spring批处理尝试在STOPPED步骤重新启动,而不是在元素的restart属性中引用的步骤。看起来,由于停止的步骤没有定义下一步,因此作业不会继续。
在BATCH-2315解决后,这可能不再是一个问题。