From aee9dc2f6ff3ec6cb431f69e781c446cc9ea9f6f Mon Sep 17 00:00:00 2001 From: Michael Minella Date: Mon, 22 Sep 2014 15:39:17 -0500 Subject: [PATCH] Update stopped flag to volitile in SystemCommandTasklet The SystemCommandTasklet checks a flag to determine if another thread has requested that it be stopped. Since this flag will be set via another thread than the executing thread, it needs to be declared volitile so that it's state is propigated across threads correctly. The documentation for StoppableTasklet has also been upadted to call out that scenario. This commit addresses Jira BATCH-2271 --- .../batch/core/step/tasklet/StoppableTasklet.java | 5 ++++- .../batch/core/step/tasklet/SystemCommandTasklet.java | 10 +++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/StoppableTasklet.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/StoppableTasklet.java index 21f357a93..362866c56 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/StoppableTasklet.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/StoppableTasklet.java @@ -23,7 +23,10 @@ import org.springframework.batch.core.launch.JobOperator; * as to how the stop will behave. The only guarantee provided by the * framework is that a call to {@link JobOperator#stop(long)} will * attempt to call the stop method on any currently running - * StoppableTasklet. + * StoppableTasklet. The call to {@link StoppableTasklet#stop()} will + * be from a thread other than the thread executing {@link org.springframework.batch.core.step.tasklet.Tasklet#execute(org.springframework.batch.core.StepContribution, org.springframework.batch.core.scope.context.ChunkContext)} + * so the appropriate thread safety and visibility controls should be + * put in place. * * @author Will Schipp * @since 3.0 diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/SystemCommandTasklet.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/SystemCommandTasklet.java index ba437cc47..1fd71b975 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/SystemCommandTasklet.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/SystemCommandTasklet.java @@ -16,10 +16,6 @@ package org.springframework.batch.core.step.tasklet; -import java.io.File; -import java.util.concurrent.Callable; -import java.util.concurrent.FutureTask; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.batch.core.ExitStatus; @@ -34,6 +30,10 @@ import org.springframework.core.task.SimpleAsyncTaskExecutor; import org.springframework.core.task.TaskExecutor; import org.springframework.util.Assert; +import java.io.File; +import java.util.concurrent.Callable; +import java.util.concurrent.FutureTask; + /** * {@link Tasklet} that executes a system command. * @@ -78,7 +78,7 @@ public class SystemCommandTasklet extends StepExecutionListenerSupport implement private boolean interruptOnCancel = false; - private boolean stopped = false; + private volatile boolean stopped = false; /** * Execute system command and map its exit code to {@link ExitStatus} using