diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/StepRegistry.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/StepRegistry.java index 291682eec..c3e857c38 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/StepRegistry.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/StepRegistry.java @@ -22,8 +22,9 @@ public interface StepRegistry { * * @param jobName the give job name * @param steps the job steps + * @throws DuplicateJobException if a job with the same job name has already been registered. */ - void register(String jobName, Collection steps); + void register(String jobName, Collection steps) throws DuplicateJobException; /** * Unregisters all the steps of the given job. If the job is not registered, diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultJobLoader.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultJobLoader.java index 9c0d44dbf..d5d52f0df 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultJobLoader.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultJobLoader.java @@ -181,6 +181,7 @@ public class DefaultJobLoader implements JobLoader, InitializingBean { if (unregister) { logger.debug("Unregistering job: " + jobName + " from context: " + context.getDisplayName()); jobRegistry.unregister(jobName); + stepRegistry.unregisterStepsFromJob(jobName); } logger.debug("Registering job: " + jobName + " from context: " + context.getDisplayName()); diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapJobRegistry.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapJobRegistry.java index 14e2271ea..b4cc29e9c 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapJobRegistry.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapJobRegistry.java @@ -77,7 +77,7 @@ public class MapJobRegistry implements JobRegistry, StepRegistry { return Collections.unmodifiableSet(map.keySet()); } - public void register(String jobName, Collection steps) { + public void register(String jobName, Collection steps) throws DuplicateJobException { stepRegistry.register(jobName, steps); } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapStepRegistry.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapStepRegistry.java index 8fb4348a0..d4d4dffc2 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapStepRegistry.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/MapStepRegistry.java @@ -1,6 +1,7 @@ package org.springframework.batch.core.configuration.support; import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.DuplicateJobException; import org.springframework.batch.core.configuration.StepRegistry; import org.springframework.batch.core.launch.NoSuchJobException; import org.springframework.batch.core.step.NoSuchStepException; @@ -23,17 +24,20 @@ public class MapStepRegistry implements StepRegistry { private final ConcurrentMap> map = new ConcurrentHashMap>(); - public void register(String jobName, Collection steps) { + public void register(String jobName, Collection steps) throws DuplicateJobException { Assert.notNull(jobName, "The job name cannot be null."); Assert.notNull(steps, "The job steps cannot be null."); - unregisterStepsFromJob(jobName); final Map jobSteps = new HashMap(); for (Step step : steps) { jobSteps.put(step.getName(), step); } - this.map.put(jobName, jobSteps); + final Object previousValue = map.putIfAbsent(jobName, jobSteps); + if (previousValue != null) { + throw new DuplicateJobException("A job configuration with this name [" + jobName + + "] was already registered"); + } } public void unregisterStepsFromJob(String jobName) { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/MapStepRegistryTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/MapStepRegistryTests.java index 146143986..fefa1a3da 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/MapStepRegistryTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/MapStepRegistryTests.java @@ -3,6 +3,7 @@ package org.springframework.batch.core.configuration.support; import org.junit.Assert; import org.junit.Test; import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.DuplicateJobException; import org.springframework.batch.core.configuration.StepRegistry; import org.springframework.batch.core.launch.NoSuchJobException; import org.springframework.batch.core.step.NoSuchStepException; @@ -12,6 +13,8 @@ import java.util.Arrays; import java.util.Collection; import java.util.HashSet; +import static junit.framework.Assert.fail; + /** * @author Sebastien Gerard */ @@ -20,14 +23,14 @@ public class MapStepRegistryTests { private static final String EXCEPTION_NOT_THROWN_MSG = "An exception should have been thrown"; @Test - public void registerStepEmptyCollection() { + public void registerStepEmptyCollection() throws DuplicateJobException { final StepRegistry stepRegistry = createRegistry(); launchRegisterGetRegistered(stepRegistry, "myJob", getStepCollection()); } @Test - public void registerStepNullJobName() { + public void registerStepNullJobName() throws DuplicateJobException { final StepRegistry stepRegistry = createRegistry(); try { @@ -38,7 +41,7 @@ public class MapStepRegistryTests { } @Test - public void registerStepNullSteps() { + public void registerStepNullSteps() throws DuplicateJobException { final StepRegistry stepRegistry = createRegistry(); try { @@ -49,7 +52,7 @@ public class MapStepRegistryTests { } @Test - public void registerStepGetStep() { + public void registerStepGetStep() throws DuplicateJobException { final StepRegistry stepRegistry = createRegistry(); launchRegisterGetRegistered(stepRegistry, "myJob", @@ -61,7 +64,7 @@ public class MapStepRegistryTests { } @Test - public void getJobNotRegistered() { + public void getJobNotRegistered() throws DuplicateJobException { final StepRegistry stepRegistry = createRegistry(); final String aStepName = "myStep"; @@ -83,7 +86,7 @@ public class MapStepRegistryTests { } @Test - public void getStepNotRegistered() { + public void getStepNotRegistered() throws DuplicateJobException { final StepRegistry stepRegistry = createRegistry(); final String jobName = "myJob"; @@ -98,7 +101,7 @@ public class MapStepRegistryTests { } @Test - public void registerRegisterAgainAndGet() { + public void registerTwice() throws DuplicateJobException { final StepRegistry stepRegistry = createRegistry(); final String jobName = "myJob"; @@ -111,14 +114,16 @@ public class MapStepRegistryTests { // first registration launchRegisterGetRegistered(stepRegistry, jobName, stepsFirstRegistration); - // register again the job - launchRegisterGetRegistered(stepRegistry, jobName, - getStepCollection( - createStep("myFourthStep"), - createStep("lastOne") - )); - assertStepsNotRegistered(stepRegistry, jobName, stepsFirstRegistration); + // Second registration with same name should fail + try { + stepRegistry.register(jobName, getStepCollection( + createStep("myFourthStep"), + createStep("lastOne"))); + fail("Should have failed with a "+DuplicateJobException.class.getSimpleName()); + } catch (DuplicateJobException e) { + // OK + } } @Test @@ -133,7 +138,7 @@ public class MapStepRegistryTests { } @Test - public void getStepNullStepName() throws NoSuchJobException { + public void getStepNullStepName() throws NoSuchJobException, DuplicateJobException { final StepRegistry stepRegistry = createRegistry(); final String stepName = "myStep"; @@ -147,7 +152,7 @@ public class MapStepRegistryTests { } @Test - public void registerStepUnregisterJob() { + public void registerStepUnregisterJob() throws DuplicateJobException { final StepRegistry stepRegistry = createRegistry(); final Collection steps = getStepCollection( @@ -193,7 +198,8 @@ public class MapStepRegistryTests { return Arrays.asList(steps); } - protected void launchRegisterGetRegistered(StepRegistry stepRegistry, String jobName, Collection steps) { + protected void launchRegisterGetRegistered(StepRegistry stepRegistry, String jobName, Collection steps) + throws DuplicateJobException { stepRegistry.register(jobName, steps); assertStepsRegistered(stepRegistry, jobName, steps); }