From 25f732e06b09c99b04b0b43800b38eb2a33fc7df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ste=CC=81phane=20Nicoll?= Date: Fri, 7 Dec 2012 18:43:18 +0100 Subject: [PATCH] delegating synchronization to ConcurrentHashMap instead of dealing that ourselves. --- .../support/MapStepRegistry.java | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 deletions(-) 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 a1cea6c93..8fb4348a0 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 @@ -9,6 +9,8 @@ import org.springframework.util.Assert; import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; /** * Simple map-based implementation of {@link StepRegistry}. Access to the map is @@ -19,7 +21,7 @@ import java.util.Map; */ public class MapStepRegistry implements StepRegistry { - private final Map> map = new HashMap>(); + private final ConcurrentMap> map = new ConcurrentHashMap>(); public void register(String jobName, Collection steps) { Assert.notNull(jobName, "The job name cannot be null."); @@ -27,38 +29,30 @@ public class MapStepRegistry implements StepRegistry { unregisterStepsFromJob(jobName); - synchronized (this.map) { - final Map jobSteps = new HashMap(); - for (Step step : steps) { - jobSteps.put(step.getName(), step); - } - - this.map.put(jobName, jobSteps); + final Map jobSteps = new HashMap(); + for (Step step : steps) { + jobSteps.put(step.getName(), step); } + this.map.put(jobName, jobSteps); } public void unregisterStepsFromJob(String jobName) { Assert.notNull(jobName, "Job configuration must have a name."); - synchronized (map) { - map.remove(jobName); - } + map.remove(jobName); } public Step getStep(String jobName, String stepName) throws NoSuchJobException { Assert.notNull(jobName, "The job name cannot be null."); Assert.notNull(stepName, "The step name cannot be null."); - - synchronized (map) { - if (!map.containsKey(jobName)) { - throw new NoSuchJobException("No job configuration with the name [" + jobName + "] was registered"); + if (!map.containsKey(jobName)) { + throw new NoSuchJobException("No job configuration with the name [" + jobName + "] was registered"); + } else { + final Map jobSteps = map.get(jobName); + if (jobSteps.containsKey(stepName)) { + return jobSteps.get(stepName); } else { - final Map jobSteps = map.get(jobName); - if (jobSteps.containsKey(stepName)) { - return jobSteps.get(stepName); - } else { - throw new NoSuchStepException("The step called [" + stepName + "] does not exist in the job [" + - jobName + "]"); - } + throw new NoSuchStepException("The step called [" + stepName + "] does not exist in the job [" + + jobName + "]"); } } }