registering twice the same jobName on the step registry is not allowed (consistency with JobRegistry)
This commit is contained in:
committed by
Michael Minella
parent
25f732e06b
commit
2870628c85
@@ -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<Step> steps);
|
||||
void register(String jobName, Collection<Step> steps) throws DuplicateJobException;
|
||||
|
||||
/**
|
||||
* Unregisters all the steps of the given job. If the job is not registered,
|
||||
|
||||
@@ -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());
|
||||
|
||||
@@ -77,7 +77,7 @@ public class MapJobRegistry implements JobRegistry, StepRegistry {
|
||||
return Collections.unmodifiableSet(map.keySet());
|
||||
}
|
||||
|
||||
public void register(String jobName, Collection<Step> steps) {
|
||||
public void register(String jobName, Collection<Step> steps) throws DuplicateJobException {
|
||||
stepRegistry.register(jobName, steps);
|
||||
}
|
||||
|
||||
|
||||
@@ -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<String, Map<String, Step>> map = new ConcurrentHashMap<String, Map<String, Step>>();
|
||||
|
||||
public void register(String jobName, Collection<Step> steps) {
|
||||
public void register(String jobName, Collection<Step> 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<String, Step> jobSteps = new HashMap<String, Step>();
|
||||
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) {
|
||||
|
||||
@@ -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<Step> steps = getStepCollection(
|
||||
@@ -193,7 +198,8 @@ public class MapStepRegistryTests {
|
||||
return Arrays.asList(steps);
|
||||
}
|
||||
|
||||
protected void launchRegisterGetRegistered(StepRegistry stepRegistry, String jobName, Collection<Step> steps) {
|
||||
protected void launchRegisterGetRegistered(StepRegistry stepRegistry, String jobName, Collection<Step> steps)
|
||||
throws DuplicateJobException {
|
||||
stepRegistry.register(jobName, steps);
|
||||
assertStepsRegistered(stepRegistry, jobName, steps);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user