From ba1f2a5e7e0517aa27edbb8a807df8a5d9f70af9 Mon Sep 17 00:00:00 2001 From: Seonkyo Ok Date: Sat, 13 Jul 2024 10:28:18 +0900 Subject: [PATCH] Check if job is stopping before checking interrupt --- .../batch/core/step/tasklet/TaskletStep.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java index aaf192beb2..5c625565b0 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/TaskletStep.java @@ -19,6 +19,7 @@ import org.apache.commons.logging.LogFactory; import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.ChunkListener; +import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobInterruptedException; import org.springframework.batch.core.StepContribution; import org.springframework.batch.core.StepExecution; @@ -71,6 +72,7 @@ * @author Michael Minella * @author Will Schipp * @author Mahmoud Ben Hassine + * @author Seonkyo Ok */ public class TaskletStep extends AbstractStep { @@ -238,7 +240,7 @@ protected void doExecute(StepExecution stepExecution) throws Exception { public RepeatStatus doInChunkContext(RepeatContext repeatContext, ChunkContext chunkContext) throws Exception { - StepExecution stepExecution = chunkContext.getStepContext().getStepExecution(); + final StepExecution stepExecution = chunkContext.getStepContext().getStepExecution(); // Before starting a new transaction, check for // interruption. @@ -256,6 +258,12 @@ public RepeatStatus doInChunkContext(RepeatContext repeatContext, ChunkContext c chunkListener.afterChunk(chunkContext); + final JobExecution jobExecution = stepExecution.getJobExecution(); + if (jobExecution.isStopping()) { + logger.info("Parent JobExecution is stopped, so passing message on to StepExecution"); + stepExecution.setTerminateOnly(); + } + // Check for interruption after transaction as well, so that // the interrupted exception is correctly propagated up to // caller @@ -367,7 +375,7 @@ public RepeatStatus doInTransaction(TransactionStatus status) { RepeatStatus result = RepeatStatus.CONTINUABLE; - StepContribution contribution = stepExecution.createStepContribution(); + final StepContribution contribution = stepExecution.createStepContribution(); chunkListener.beforeChunk(chunkContext); @@ -437,7 +445,7 @@ public RepeatStatus doInTransaction(TransactionStatus status) { catch (Exception e) { // If we get to here there was a problem saving the step // execution and we have to fail. - String msg = "JobRepository failure forcing rollback"; + final String msg = "JobRepository failure forcing rollback"; logger.error(msg, e); throw new FatalStepExecutionException(msg, e); } @@ -476,7 +484,7 @@ private void rollback(StepExecution stepExecution) { } } - private void copy(final StepExecution source, final StepExecution target) { + private static void copy(final StepExecution source, final StepExecution target) { target.setVersion(source.getVersion()); target.setWriteCount(source.getWriteCount()); target.setFilterCount(source.getFilterCount());