Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-4703: Enhance StoppableTasklet interface to support step-specific stop requests #4715

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.batch.core.step.tasklet;

import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.launch.JobOperator;

/**
Expand All @@ -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 {
Expand All @@ -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();
}
Comment on lines +50 to +52
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new method provides a default implementation that delegates the call to the existing stop() method, ensuring backward compatibility for implementations that do not need step-specific stopping logic.

If you have any suggestions for improvement, please feel free to provide feedback!

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
* @author Will Schipp
* @author Mahmoud Ben Hassine
* @author Injae Kim
* @author Hyunsang Han
*/
public class SystemCommandTasklet implements StepExecutionListener, StoppableTasklet, InitializingBean {

Expand Down Expand Up @@ -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;
}
}

}