Skip to content

Commit bdb2bfc

Browse files
HyunSangHanfmbenhassine
authored andcommitted
Add StepExecution parameter to StoppableTasklet.stop
Resolves #4703
1 parent b01a19d commit bdb2bfc

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
* @author Mahmoud Ben Hassine
8383
* @author Andrey Litvitski
8484
* @author Yejeong Ham
85+
* @author Hyunsang Han
8586
* @since 2.0
8687
* @deprecated since 6.0 in favor of {@link TaskExecutorJobOperator}. Scheduled for
8788
* removal in 6.2 or later.
@@ -348,7 +349,7 @@ public boolean stop(JobExecution jobExecution) throws JobExecutionNotRunningExce
348349
Tasklet tasklet = taskletStep.getTasklet();
349350
if (tasklet instanceof StoppableTasklet stoppableTasklet) {
350351
StepSynchronizationManager.register(stepExecution);
351-
stoppableTasklet.stop();
352+
stoppableTasklet.stop(stepExecution);
352353
StepSynchronizationManager.release();
353354
}
354355
}
@@ -366,7 +367,7 @@ public boolean stop(JobExecution jobExecution) throws JobExecutionNotRunningExce
366367
}
367368
}
368369
catch (NoSuchJobException e) {
369-
logger.warn("Cannot find Job object in the job registry. StoppableTasklet#stop() will not be called", e);
370+
logger.warn("Cannot find Job object in the job registry. StoppableTasklet#stop(StepExecution stepExecution) will not be called", e);
370371
}
371372

372373
return true;

spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/StoppableTasklet.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.batch.core.step.tasklet;
1717

18+
import org.springframework.batch.core.StepExecution;
1819
import org.springframework.batch.core.launch.JobOperator;
1920
import org.springframework.batch.core.step.StepContribution;
2021

@@ -29,6 +30,7 @@
2930
* so the appropriate thread safety and visibility controls should be put in place.
3031
*
3132
* @author Will Schipp
33+
* @author Hyunsang Han
3234
* @since 3.0
3335
*/
3436
public interface StoppableTasklet extends Tasklet {
@@ -39,4 +41,14 @@ public interface StoppableTasklet extends Tasklet {
3941
*/
4042
void stop();
4143

44+
/**
45+
* Used to signal that the job should stop, providing access to the current
46+
* {@link StepExecution} context.
47+
*
48+
* @param stepExecution the current {@link StepExecution} context in which the job
49+
* is being executed
50+
*/
51+
default void stop(StepExecution stepExecution) {
52+
stop();
53+
}
4254
}

spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/SystemCommandTasklet.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
* @author Will Schipp
6262
* @author Mahmoud Ben Hassine
6363
* @author Injae Kim
64+
* @author Hyunsang Han
6465
*/
6566
public class SystemCommandTasklet implements StepExecutionListener, StoppableTasklet, InitializingBean {
6667

@@ -275,4 +276,21 @@ public void stop() {
275276
stopped = true;
276277
}
277278

279+
/**
280+
* Interrupts the execution of the system command if the given {@link StepExecution}
281+
* matches the current execution context. This method allows for granular control over
282+
* stopping specific step executions, ensuring that only the intended command is halted.
283+
*
284+
* @param stepExecution the current {@link StepExecution} context; the execution is
285+
* interrupted if it matches the ongoing one.
286+
* @since 6.0
287+
* @see StoppableTasklet#stop(StepExecution)
288+
*/
289+
@Override
290+
public void stop(StepExecution stepExecution) {
291+
if (stepExecution.equals(execution)) {
292+
stopped = true;
293+
}
294+
}
295+
278296
}

0 commit comments

Comments
 (0)