BATCH-1709: Fix concurrency issue in BeanWrapperFieldSetMapper
This commit is contained in:
@@ -22,6 +22,8 @@ import java.util.HashSet;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
import org.springframework.batch.item.file.transform.FieldSet;
|
import org.springframework.batch.item.file.transform.FieldSet;
|
||||||
import org.springframework.batch.support.DefaultPropertyEditorRegistrar;
|
import org.springframework.batch.support.DefaultPropertyEditorRegistrar;
|
||||||
@@ -94,7 +96,7 @@ public class BeanWrapperFieldSetMapper<T> extends DefaultPropertyEditorRegistrar
|
|||||||
|
|
||||||
private BeanFactory beanFactory;
|
private BeanFactory beanFactory;
|
||||||
|
|
||||||
private static Map<DistanceHolder, Map<String, String>> propertiesMatched = new HashMap<DistanceHolder, Map<String, String>>();
|
private ConcurrentMap<DistanceHolder, ConcurrentMap<String, String>> propertiesMatched = new ConcurrentHashMap<DistanceHolder, ConcurrentMap<String, String>>();
|
||||||
|
|
||||||
private int distanceLimit = 5;
|
private int distanceLimit = 5;
|
||||||
|
|
||||||
@@ -249,11 +251,10 @@ public class BeanWrapperFieldSetMapper<T> extends DefaultPropertyEditorRegistrar
|
|||||||
|
|
||||||
// Map from field names to property names
|
// Map from field names to property names
|
||||||
DistanceHolder distanceKey = new DistanceHolder(cls, distanceLimit);
|
DistanceHolder distanceKey = new DistanceHolder(cls, distanceLimit);
|
||||||
Map<String, String> matches = propertiesMatched.get(distanceKey);
|
if (!propertiesMatched.containsKey(distanceKey)) {
|
||||||
if (matches == null) {
|
propertiesMatched.putIfAbsent(distanceKey, new ConcurrentHashMap<String, String>());
|
||||||
matches = new HashMap<String, String>();
|
|
||||||
propertiesMatched.put(distanceKey, matches);
|
|
||||||
}
|
}
|
||||||
|
Map<String, String> matches = new HashMap<String, String>(propertiesMatched.get(distanceKey));
|
||||||
|
|
||||||
Set<String> keys = new HashSet(properties.keySet());
|
Set<String> keys = new HashSet(properties.keySet());
|
||||||
for (String key : keys) {
|
for (String key : keys) {
|
||||||
@@ -281,6 +282,9 @@ public class BeanWrapperFieldSetMapper<T> extends DefaultPropertyEditorRegistrar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!propertiesMatched.containsKey(distanceKey)) {
|
||||||
|
propertiesMatched.putIfAbsent(distanceKey, new ConcurrentHashMap<String, String>(matches));
|
||||||
|
}
|
||||||
return properties;
|
return properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -379,12 +383,13 @@ public class BeanWrapperFieldSetMapper<T> extends DefaultPropertyEditorRegistrar
|
|||||||
|
|
||||||
private static class DistanceHolder {
|
private static class DistanceHolder {
|
||||||
private final Class<?> cls;
|
private final Class<?> cls;
|
||||||
|
|
||||||
private final int distance;
|
private final int distance;
|
||||||
|
|
||||||
public DistanceHolder(Class<?> cls, int distance) {
|
public DistanceHolder(Class<?> cls, int distance) {
|
||||||
this.cls = cls;
|
this.cls = cls;
|
||||||
this.distance = distance;
|
this.distance = distance;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -417,5 +422,4 @@ public class BeanWrapperFieldSetMapper<T> extends DefaultPropertyEditorRegistrar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,85 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2006-2007 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
|
||||||
|
*
|
||||||
|
* http://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.item.file.mapping;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.Future;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
|
||||||
|
|
||||||
|
public class BeanWrapperFieldSetMapperConcurrentTests {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testConcurrentUsage() throws Exception {
|
||||||
|
final BeanWrapperFieldSetMapper<GreenBean> mapper = new BeanWrapperFieldSetMapper<GreenBean>();
|
||||||
|
mapper.setStrict(true);
|
||||||
|
mapper.setTargetType(GreenBean.class);
|
||||||
|
// mapper.setDistanceLimit(0);
|
||||||
|
final DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();
|
||||||
|
String[] names = { "blue", "green" };
|
||||||
|
lineTokenizer.setNames(names);
|
||||||
|
|
||||||
|
ExecutorService executorService = Executors.newFixedThreadPool(5);
|
||||||
|
Collection<Future<Boolean>> results = new ArrayList<Future<Boolean>>();
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
Future<Boolean> result = executorService.submit(new Callable<Boolean>() {
|
||||||
|
public Boolean call() throws Exception {
|
||||||
|
for (int i = 0; i < 10; i++) {
|
||||||
|
GreenBean bean = mapper.mapFieldSet(lineTokenizer.tokenize("blue,green"));
|
||||||
|
Assert.assertEquals("green", bean.getGreen());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
results.add(result);
|
||||||
|
}
|
||||||
|
for (Future<Boolean> future : results) {
|
||||||
|
assertTrue(future.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class GreenBean {
|
||||||
|
private String green;
|
||||||
|
|
||||||
|
private String blue;
|
||||||
|
|
||||||
|
public String getBlue() {
|
||||||
|
return blue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBlue(String blue) {
|
||||||
|
this.blue = blue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGreen() {
|
||||||
|
return green;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGreen(String green) {
|
||||||
|
this.green = green;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -49,7 +49,7 @@ import org.springframework.validation.BindException;
|
|||||||
import org.springframework.validation.DataBinder;
|
import org.springframework.validation.DataBinder;
|
||||||
|
|
||||||
public class BeanWrapperFieldSetMapperTests {
|
public class BeanWrapperFieldSetMapperTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNameAndTypeSpecified() throws Exception {
|
public void testNameAndTypeSpecified() throws Exception {
|
||||||
BeanWrapperFieldSetMapper<TestObject> mapper = new BeanWrapperFieldSetMapper<TestObject>();
|
BeanWrapperFieldSetMapper<TestObject> mapper = new BeanWrapperFieldSetMapper<TestObject>();
|
||||||
|
|||||||
Reference in New Issue
Block a user