From f9b032be94f91f341056789fabf7b70d54fdb6e6 Mon Sep 17 00:00:00 2001 From: HyunSangHan Date: Sun, 24 Nov 2024 13:18:02 +0900 Subject: [PATCH 1/3] Add StoppableTasklet#stop(StepExecution) --- .../batch/core/step/tasklet/StoppableTasklet.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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 4d604afd6d..a859e4560a 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 @@ -15,6 +15,7 @@ */ package org.springframework.batch.core.step.tasklet; +import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.launch.JobOperator; /** @@ -28,6 +29,7 @@ * so the appropriate thread safety and visibility controls should be put in place. * * @author Will Schipp + * @author Hyunsang Han * @since 3.0 */ public interface StoppableTasklet extends Tasklet { @@ -38,4 +40,14 @@ public interface StoppableTasklet extends Tasklet { */ void stop(); + /** + * Used to signal that the job should stop, providing access to the current + * {@link StepExecution} context. + * + * @param stepExecution the current {@link StepExecution} context in which the job + * is being executed + */ + default void stop(StepExecution stepExecution) { + stop(); + } } From 00009099d452fc4aec3b791a621253e833075671 Mon Sep 17 00:00:00 2001 From: HyunSangHan Date: Sun, 24 Nov 2024 13:26:51 +0900 Subject: [PATCH 2/3] Use StoppableTasklet#stop(StepExecution) in SimpleJobOperator#stop(long executionId) --- .../batch/core/launch/support/SimpleJobOperator.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java index 059e769960..aa7a257451 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java @@ -84,6 +84,7 @@ * @author Lucas Ward * @author Will Schipp * @author Mahmoud Ben Hassine + * @author Hyunsang Han * @since 2.0 */ public class SimpleJobOperator implements JobOperator, InitializingBean { @@ -350,7 +351,7 @@ public boolean stop(long executionId) throws NoSuchJobExecutionException, JobExe Tasklet tasklet = ((TaskletStep) step).getTasklet(); if (tasklet instanceof StoppableTasklet) { StepSynchronizationManager.register(stepExecution); - ((StoppableTasklet) tasklet).stop(); + ((StoppableTasklet) tasklet).stop(stepExecution); StepSynchronizationManager.release(); } } @@ -363,7 +364,7 @@ public boolean stop(long executionId) throws NoSuchJobExecutionException, JobExe } } catch (NoSuchJobException e) { - logger.warn("Cannot find Job object in the job registry. StoppableTasklet#stop() will not be called", e); + logger.warn("Cannot find Job object in the job registry. StoppableTasklet#stop(StepExecution stepExecution) will not be called", e); } return true; From 30ba4e39c58b5b32d41f38726e29b4bd60e40286 Mon Sep 17 00:00:00 2001 From: HyunSangHan Date: Sun, 24 Nov 2024 13:18:49 +0900 Subject: [PATCH 3/3] Implement StoppableTasklet#stop(StepExecution) in SystemCommandTasklet --- .../step/tasklet/SystemCommandTasklet.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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 4499279e8e..1fd8ecc7ab 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 @@ -61,6 +61,7 @@ * @author Will Schipp * @author Mahmoud Ben Hassine * @author Injae Kim + * @author Hyunsang Han */ public class SystemCommandTasklet implements StepExecutionListener, StoppableTasklet, InitializingBean { @@ -275,4 +276,21 @@ public void stop() { stopped = true; } + /** + * Interrupts the execution of the system command if the given {@link StepExecution} + * matches the current execution context. This method allows for granular control over + * stopping specific step executions, ensuring that only the intended command is halted. + * + * @param stepExecution the current {@link StepExecution} context; the execution is + * interrupted if it matches the ongoing one. + * @since 6.0 + * @see StoppableTasklet#stop(StepExecution) + */ + @Override + public void stop(StepExecution stepExecution) { + if (stepExecution.equals(execution)) { + stopped = true; + } + } + }