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

Step names must be unique #3757 #4756

Open
wants to merge 6 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
20 changes: 20 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,26 @@
<spring-javaformat-maven-plugin.version>0.0.39</spring-javaformat-maven-plugin.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,8 +17,7 @@
package org.springframework.batch.core.job;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;

import io.micrometer.core.instrument.LongTaskTimer;
Expand All @@ -43,6 +42,7 @@
import org.springframework.batch.core.StartLimitExceededException;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.job.builder.AlreadyUsedStepNameException;
import org.springframework.batch.core.launch.NoSuchJobException;
import org.springframework.batch.core.launch.support.ExitCodeMapper;
import org.springframework.batch.core.listener.CompositeJobExecutionListener;
Expand Down Expand Up @@ -300,6 +300,7 @@ public final void execute(JobExecution execution) {

execution.setStartTime(LocalDateTime.now());
updateStatus(execution, BatchStatus.STARTED);
checkStepNamesUnicity();

listener.beforeJob(execution);

Expand Down Expand Up @@ -368,11 +369,11 @@ public final void execute(JobExecution execution) {
finally {
JobSynchronizationManager.release();
}

}

}

protected abstract void checkStepNamesUnicity() throws AlreadyUsedStepNameException;

private void stopObservation(JobExecution execution, Observation observation) {
List<Throwable> throwables = execution.getFailureExceptions();
if (!throwables.isEmpty()) {
Expand Down Expand Up @@ -430,6 +431,16 @@ else if (ex instanceof NoSuchJobException || ex.getCause() instanceof NoSuchJobE
return exitStatus;
}

protected static void addToMapCheckingUnicity(Map<String, Step> map, Step step, String name)
throws AlreadyUsedStepNameException {
map.merge(name, step, (old, value) -> {
if (!old.equals(value)) {
throw new AlreadyUsedStepNameException(name);
}
return old;
});
}

private void updateStatus(JobExecution jobExecution, BatchStatus status) {
jobExecution.setStatus(status);
jobRepository.update(jobExecution);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,9 +16,7 @@

package org.springframework.batch.core.job;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.*;

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.Job;
Expand Down Expand Up @@ -145,4 +143,9 @@ protected void doExecute(JobExecution execution)
}
}

@Override
protected void checkStepNamesUnicity() {
// noop : steps of SimpleJob can share the same name
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2006-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.batch.core.job.builder;

/**
* Exception to indicate the name of a step is already used by a different step in the
* same flow. Step names must be unique within a flow definition because the search of the
* next step to find relies on the step name
*
* @author Fabrice Bibonne
*/
public class AlreadyUsedStepNameException extends RuntimeException {

public AlreadyUsedStepNameException(String name) {
super("the name " + name + " is already used");
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,19 +15,20 @@
*/
package org.springframework.batch.core.job.flow;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionException;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.job.AbstractJob;
import org.springframework.batch.core.job.SimpleStepHandler;
import org.springframework.batch.core.job.builder.AlreadyUsedStepNameException;
import org.springframework.batch.core.step.StepHolder;
import org.springframework.batch.core.step.StepLocator;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Implementation of the {@link Job} interface that allows for complex flows of steps,
* rather than requiring sequential execution. In general, this job implementation was
Expand Down Expand Up @@ -74,50 +75,50 @@ public void setFlow(Flow flow) {
*/
@Override
public Step getStep(String stepName) {
if (!initialized) {
init();
}
init();
return stepMap.get(stepName);
}

/**
* Initialize the step names
*/
private void init() {
findSteps(flow, stepMap);
initialized = true;
if (!initialized) {
findStepsThrowingIfNameNotUnique(flow);
initialized = true;
}
}

private void findSteps(Flow flow, Map<String, Step> map) {
private void findStepsThrowingIfNameNotUnique(Flow flow) {

for (State state : flow.getStates()) {
if (state instanceof StepLocator locator) {
for (String name : locator.getStepNames()) {
map.put(name, locator.getStep(name));
addToMapCheckingUnicity(this.stepMap, locator.getStep(name), name);
}
}
else if (state instanceof StepHolder) {
Step step = ((StepHolder) state).getStep();
String name = step.getName();
stepMap.put(name, step);
// TODO remove this else bock ? not executed during tests : the only State
// which implements StepHolder is StepState which already implements
// StepLocator
// within tests coverage `state instanceof StepHolder` is false 30 times/30
else if (state instanceof StepHolder stepHolder) {
Step step = stepHolder.getStep();
addToMapCheckingUnicity(this.stepMap, step, step.getName());
}
else if (state instanceof FlowHolder) {
for (Flow subflow : ((FlowHolder) state).getFlows()) {
findSteps(subflow, map);
else if (state instanceof FlowHolder flowHolder) {
for (Flow subflow : flowHolder.getFlows()) {
findStepsThrowingIfNameNotUnique(subflow);
}
}
}

}

/**
* {@inheritDoc}
*/
@Override
public Collection<String> getStepNames() {
if (!initialized) {
init();
}
init();
return stepMap.keySet();
}

Expand All @@ -139,4 +140,9 @@ protected void doExecute(final JobExecution execution) throws JobExecutionExcept
}
}

@Override
protected void checkStepNamesUnicity() throws AlreadyUsedStepNameException {
init();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,31 +17,20 @@

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionException;
import org.springframework.batch.core.JobInterruptedException;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepExecution;
import org.springframework.batch.core.*;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.batch.core.step.StepSupport;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.lang.Nullable;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

/**
* @author Dave Syer
Expand Down Expand Up @@ -215,6 +204,10 @@ public StubJob() {
protected void doExecute(JobExecution execution) throws JobExecutionException {
}

@Override
protected void checkStepNamesUnicity() {
}

@Override
public Step getStep(String stepName) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2023 the original author or authors.
* Copyright 2006-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -513,6 +513,25 @@ void testGetMultipleJobParameters() throws Exception {

}

@Test
public void testMultipleStepsWithSameName() {
job.setName("MultipleStepsWithSameName");
String sharedName = "stepName";
final List<String> executionsCallbacks = new ArrayList<>();
StubStep sharedNameStep1 = new StubStep(sharedName, jobRepository);
sharedNameStep1.setCallback(() -> executionsCallbacks.add("step1"));
job.addStep(sharedNameStep1);
StubStep sharedNameStep2 = new StubStep(sharedName, jobRepository);
sharedNameStep2.setCallback(() -> executionsCallbacks.add("step2"));
job.addStep(sharedNameStep2);
StubStep sharedNameStep3 = new StubStep(sharedName, jobRepository);
sharedNameStep3.setCallback(() -> executionsCallbacks.add("step3"));
job.addStep(sharedNameStep3);
job.execute(jobExecution);
assertEquals(List.of("step1", "step2", "step3"), executionsCallbacks);
assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
}

/*
* Check JobRepository to ensure status is being saved.
*/
Expand Down
Loading