From cde60fe938a5e22af44f7be9466379c73d49546f Mon Sep 17 00:00:00 2001 From: Michael Minella Date: Tue, 28 Jan 2014 09:25:06 -0600 Subject: [PATCH] Per JSR-352, restart is a two step process, establishing where to begin evaluation and then evaluating each state. This update addresses the flagging of the point the current job restarted from so that the evaluation of restart can occur in subsequent states. --- .../batch/core/jsr/job/DefaultStepHandler.java | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/DefaultStepHandler.java b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/DefaultStepHandler.java index 8ee8822df..eb836edbd 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/DefaultStepHandler.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/job/DefaultStepHandler.java @@ -30,6 +30,7 @@ import org.springframework.batch.core.repository.JobRepository; import org.springframework.batch.core.repository.JobRestartException; import org.springframework.batch.item.ExecutionContext; import org.springframework.util.Assert; +import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; /** @@ -75,10 +76,10 @@ public class DefaultStepHandler extends SimpleStepHandler { @Override protected boolean shouldStart(StepExecution lastStepExecution, JobExecution jobExecution, Step step) throws JobRestartException, StartLimitExceededException { - BatchStatus stepStatus; String restartStep = null; if (lastStepExecution == null) { + jobExecution.getExecutionContext().put("batch.startedStep", step.getName()); stepStatus = BatchStatus.STARTING; } else { @@ -89,9 +90,14 @@ public class DefaultStepHandler extends SimpleStepHandler { if(lastJobExecution.getExecutionContext().containsKey("batch.restartStep")) { restartStep = lastJobExecution.getExecutionContext().getString("batch.restartStep"); - if(lastJobExecution.getStatus() == BatchStatus.STOPPED && StringUtils.hasText(restartStep) && !restartStep.equals(step.getName())) { - logger.info("Job was stopped and should restart at step " + restartStep + ". The current step is " + step.getName()); - return false; + if(CollectionUtils.isEmpty(jobExecution.getStepExecutions()) && lastJobExecution.getStatus() == BatchStatus.STOPPED && StringUtils.hasText(restartStep)) { + if(!restartStep.equals(step.getName()) && !jobExecution.getExecutionContext().containsKey("batch.startedStep")) { + logger.info("Job was stopped and should restart at step " + restartStep + ". The current step is " + step.getName()); + return false; + } else { + // Indicates the starting point for execution evaluation per JSR-352 + jobExecution.getExecutionContext().put("batch.startedStep", step.getName()); + } } } } @@ -135,9 +141,9 @@ public class DefaultStepHandler extends SimpleStepHandler { JobExecution lastJobExecution = null; for (JobExecution curJobExecution : jobExecutions) { - if(lastJobExecution == null && curJobExecution.getId() != jobExecution.getId()) { + if(lastJobExecution == null && curJobExecution.getId().longValue() != jobExecution.getId().longValue()) { lastJobExecution = curJobExecution; - } else if(lastJobExecution != null && curJobExecution.getId() > lastJobExecution.getId() && curJobExecution.getId() != jobExecution.getId()) { + } else if(curJobExecution.getId().longValue() != jobExecution.getId().longValue() && (lastJobExecution == null || curJobExecution.getId().longValue() > lastJobExecution.getId().longValue())) { lastJobExecution = curJobExecution; } }