INT-3353 Concurrent RedisMetadataStore
JIRA: https://jira.spring.io/browse/INT-3353 Shared metadata for PersistentAcceptOnceFileListFilters. INT-3353 Polishing; PR Comments
This commit is contained in:
committed by
Artem Bilan
parent
40a535b140
commit
4dee2a224b
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* Copyright 2014 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.integration.metadata;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Supports atomic updates to values in the store.
|
||||
*
|
||||
* @author Gary Russell
|
||||
* @since 4.0
|
||||
*
|
||||
*/
|
||||
public interface ConcurrentMetadataStore extends MetadataStore {
|
||||
|
||||
/**
|
||||
* Atomically insert the key into the store.
|
||||
*
|
||||
* @param key The key.
|
||||
* @param value The value.
|
||||
* @return null if successful, the old value otherwise.
|
||||
*/
|
||||
String putIfAbsent(String key, String value);
|
||||
|
||||
/**
|
||||
* Atomically replace the value for the key in the store if the old
|
||||
* value matches the oldValue argument.
|
||||
*
|
||||
* @param key The key.
|
||||
* @param oldValue The old value.
|
||||
* @param newValue The new value.
|
||||
* @return true if successful.
|
||||
*/
|
||||
boolean replace(String key, String oldValue, String newValue);
|
||||
|
||||
}
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -25,12 +25,15 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Properties;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.integration.util.DefaultLockRegistry;
|
||||
import org.springframework.integration.util.LockRegistry;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.DefaultPropertiesPersister;
|
||||
|
||||
@@ -45,7 +48,7 @@ import org.springframework.util.DefaultPropertiesPersister;
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*/
|
||||
public class PropertiesPersistingMetadataStore implements MetadataStore, InitializingBean, DisposableBean {
|
||||
public class PropertiesPersistingMetadataStore implements ConcurrentMetadataStore, InitializingBean, DisposableBean {
|
||||
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
@@ -53,9 +56,11 @@ public class PropertiesPersistingMetadataStore implements MetadataStore, Initial
|
||||
|
||||
private final DefaultPropertiesPersister persister = new DefaultPropertiesPersister();
|
||||
|
||||
private volatile File file;
|
||||
private final LockRegistry lockRegistry = new DefaultLockRegistry();
|
||||
|
||||
private volatile String baseDirectory = System.getProperty("java.io.tmpdir") + "/spring-integration/";
|
||||
private String baseDirectory = System.getProperty("java.io.tmpdir") + "/spring-integration/";
|
||||
|
||||
private File file;
|
||||
|
||||
|
||||
public void setBaseDirectory(String baseDirectory) {
|
||||
@@ -82,17 +87,85 @@ public class PropertiesPersistingMetadataStore implements MetadataStore, Initial
|
||||
|
||||
@Override
|
||||
public void put(String key, String value) {
|
||||
this.metadata.setProperty(key, value);
|
||||
Assert.notNull(key, "'key' cannot be null");
|
||||
Assert.notNull(value, "'value' cannot be null");
|
||||
Lock lock = this.lockRegistry.obtain(key);
|
||||
lock.lock();
|
||||
try {
|
||||
this.metadata.setProperty(key, value);
|
||||
}
|
||||
finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String get(String key) {
|
||||
return this.metadata.getProperty(key);
|
||||
Assert.notNull(key, "'key' cannot be null");
|
||||
Lock lock = this.lockRegistry.obtain(key);
|
||||
lock.lock();
|
||||
try {
|
||||
return this.metadata.getProperty(key);
|
||||
}
|
||||
finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String remove(String key) {
|
||||
return (String) this.metadata.remove(key);
|
||||
Assert.notNull(key, "'key' cannot be null");
|
||||
Lock lock = this.lockRegistry.obtain(key);
|
||||
lock.lock();
|
||||
try {
|
||||
return (String) this.metadata.remove(key);
|
||||
}
|
||||
finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String putIfAbsent(String key, String value) {
|
||||
Assert.notNull(key, "'key' cannot be null");
|
||||
Assert.notNull(value, "'value' cannot be null");
|
||||
Lock lock = this.lockRegistry.obtain(key);
|
||||
lock.lock();
|
||||
try {
|
||||
String property = this.metadata.getProperty(key);
|
||||
if (property == null) {
|
||||
this.metadata.setProperty(key, value);
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
return property;
|
||||
}
|
||||
}
|
||||
finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean replace(String key, String oldValue, String newValue) {
|
||||
Assert.notNull(key, "'key' cannot be null");
|
||||
Assert.notNull(oldValue, "'oldValue' cannot be null");
|
||||
Assert.notNull(newValue, "'newValue' cannot be null");
|
||||
Lock lock = this.lockRegistry.obtain(key);
|
||||
lock.lock();
|
||||
try {
|
||||
String property = this.metadata.getProperty(key);
|
||||
if (oldValue.equals(property)) {
|
||||
this.metadata.setProperty(key, newValue);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
finally {
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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
|
||||
@@ -13,8 +13,8 @@
|
||||
|
||||
package org.springframework.integration.metadata;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
|
||||
|
||||
/**
|
||||
@@ -22,24 +22,37 @@ import java.util.Map;
|
||||
* The metadata will not be persisted across application restarts.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*/
|
||||
public class SimpleMetadataStore implements MetadataStore {
|
||||
public class SimpleMetadataStore implements ConcurrentMetadataStore {
|
||||
|
||||
private final Map<String, String> metadata = new HashMap<String, String>();
|
||||
private final ConcurrentMap<String, String> metadata = new ConcurrentHashMap<String, String>();
|
||||
|
||||
|
||||
@Override
|
||||
public void put(String key, String value) {
|
||||
this.metadata.put(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String get(String key) {
|
||||
return this.metadata.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String remove(String key) {
|
||||
return metadata.remove(key);
|
||||
return this.metadata.remove(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String putIfAbsent(String key, String value) {
|
||||
return this.metadata.putIfAbsent(key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean replace(String key, String oldValue, String newValue) {
|
||||
return this.metadata.replace(key, oldValue, newValue);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 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.
|
||||
@@ -17,7 +17,9 @@
|
||||
package org.springframework.integration.metadata;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
@@ -32,6 +34,7 @@ import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||
* @author Oleg Zhurakousky
|
||||
* @author Mark Fisher
|
||||
* @author Gunnar Hillert
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*/
|
||||
public class PropertiesPersistingMetadataStoreTests {
|
||||
@@ -43,7 +46,10 @@ public class PropertiesPersistingMetadataStoreTests {
|
||||
PropertiesPersistingMetadataStore metadataStore = new PropertiesPersistingMetadataStore();
|
||||
metadataStore.afterPropertiesSet();
|
||||
assertTrue(file.exists());
|
||||
metadataStore.put("foo", "bar");
|
||||
assertNull(metadataStore.putIfAbsent("foo", "baz"));
|
||||
assertNotNull(metadataStore.putIfAbsent("foo", "baz"));
|
||||
assertFalse(metadataStore.replace("foo", "xxx", "bar"));
|
||||
assertTrue(metadataStore.replace("foo", "baz", "bar"));
|
||||
metadataStore.destroy();
|
||||
Properties persistentProperties = PropertiesLoaderUtils.loadProperties(new FileSystemResource(file));
|
||||
assertNotNull(persistentProperties);
|
||||
|
||||
Reference in New Issue
Block a user