Skip to content

Commit d18dc01

Browse files
committed
Add ability to stop any kind of step
Before this commit, it was only possible to stop tasklet steps. This commit introduces a new interface to be implemented by any step that is able to handle external stop signals. Resolves #4965
1 parent 79468bf commit d18dc01

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.batch.core.job.parameters.JobParametersIncrementer;
3737
import org.springframework.batch.core.job.parameters.JobParametersInvalidException;
3838
import org.springframework.batch.core.step.Step;
39+
import org.springframework.batch.core.step.StoppableStep;
3940
import org.springframework.batch.core.step.StepExecution;
4041
import org.springframework.batch.core.job.UnexpectedJobExecutionException;
4142
import org.springframework.batch.core.configuration.JobRegistry;
@@ -351,6 +352,11 @@ public boolean stop(JobExecution jobExecution) throws JobExecutionNotRunningExce
351352
StepSynchronizationManager.release();
352353
}
353354
}
355+
if (step instanceof StoppableStep stoppableStep) {
356+
StepSynchronizationManager.register(stepExecution);
357+
stoppableStep.stop(stepExecution);
358+
StepSynchronizationManager.release();
359+
}
354360
}
355361
catch (NoSuchStepException e) {
356362
logger.warn("Step not found", e);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
* @author Mahmoud Ben Hassine
6464
* @author Jinwoo Bae
6565
*/
66-
public abstract class AbstractStep implements Step, InitializingBean, BeanNameAware {
66+
public abstract class AbstractStep implements StoppableStep, InitializingBean, BeanNameAware {
6767

6868
private static final Log logger = LogFactory.getLog(AbstractStep.class);
6969

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2025-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.step;
17+
18+
/**
19+
* Extension of the {@link Step} interface to be implemented by steps that support being
20+
* stopped.
21+
*
22+
* @author Mahmoud Ben Hassine
23+
* @since 6.0
24+
*/
25+
public interface StoppableStep extends Step {
26+
27+
/**
28+
* Callback to signal the step to stop. The default implementation sets the
29+
* {@link StepExecution} to terminate only. Concrete implementations can override this
30+
* method to add custom stop logic.
31+
* @param stepExecution the current step execution
32+
*/
33+
default void stop(StepExecution stepExecution) {
34+
stepExecution.setTerminateOnly();
35+
}
36+
37+
}

0 commit comments

Comments
 (0)