src.net.sf.ehcache.distribution.RMICacheReplicatorFactory Maven / Gradle / Ivy
/**
* Copyright 2003-2006 Greg Luck
*
* 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 net.sf.ehcache.distribution;
import net.sf.ehcache.event.CacheEventListener;
import net.sf.ehcache.event.CacheEventListenerFactory;
import net.sf.ehcache.util.PropertyUtil;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Creates an RMICacheReplicator using properties. Config lines look like:
* <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
* properties="
* replicateAsynchronously=true,
* replicatePuts=true
* replicateUpdates=true
* replicateUpdatesViaCopy=true
* replicateRemovals=true
* "/>
*
* @author Greg Luck
* @version $Id: RMICacheReplicatorFactory.java 173 2006-08-21 07:04:28Z gregluck $
*/
public class RMICacheReplicatorFactory extends CacheEventListenerFactory {
/**
* A default for the amount of time the replication thread sleeps after it detects the replicationQueue is empty
* before checking again.
*/
protected static final int DEFAULT_ASYNCHRONOUS_REPLICATION_INTERVAL_MILLIS = 1000;
private static final Log LOG = LogFactory.getLog(RMICacheReplicatorFactory.class.getName());
private static final String REPLICATE_PUTS = "replicatePuts";
private static final String REPLICATE_UPDATES = "replicateUpdates";
private static final String REPLICATE_UPDATES_VIA_COPY = "replicateUpdatesViaCopy";
private static final String REPLICATE_REMOVALS = "replicateRemovals";
private static final String REPLICATE_ASYNCHRONOUSLY = "replicateAsynchronously";
private static final String ASYNCHRONOUS_REPLICATION_INTERVAL_MILLIS = "asynchronousReplicationIntervalMillis";
private static final int MINIMUM_REASONABLE_INTERVAL = 10;
/**
* Create a CacheEventListener
which is also a CacheReplicator.
*
* The defaults if properties are not specified are:
*
* - replicatePuts=true
*
- replicateUpdates=true
*
- replicateUpdatesViaCopy=true
*
- replicateRemovals=true;
*
- replicateAsynchronously=true
*
- asynchronousReplicationIntervalMillis=1000
*
*
* @param properties implementation specific properties. These are configured as comma
* separated name value pairs in ehcache.xml e.g.
*
*
* <cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"
* properties="
* replicateAsynchronously=true,
* replicatePuts=true
* replicateUpdates=true
* replicateUpdatesViaCopy=true
* replicateRemovals=true
* asynchronousReplicationIntervalMillis=1000
* "/>
* @return a constructed CacheEventListener
*/
public final CacheEventListener createCacheEventListener(Properties properties) {
boolean replicatePuts = extractReplicatePuts(properties);
boolean replicateUpdates = extractReplicateUpdates(properties);
boolean replicateUpdatesViaCopy = extractReplicateUpdatesViaCopy(properties);
boolean replicateRemovals = extractReplicateRemovals(properties);
boolean replicateAsynchronously = extractReplicateAsynchronously(properties);
int asynchronousReplicationIntervalMillis = extractReplicationIntervalMilis(properties);
if (replicateAsynchronously) {
return new RMIAsynchronousCacheReplicator(
replicatePuts,
replicateUpdates,
replicateUpdatesViaCopy,
replicateRemovals,
asynchronousReplicationIntervalMillis);
} else {
return new RMISynchronousCacheReplicator(
replicatePuts,
replicateUpdates,
replicateUpdatesViaCopy,
replicateRemovals);
}
}
/**
* Extracts the value of asynchronousReplicationIntervalMillis. Sets it to 1000ms if
* either not set or there is a problem parsing the number
* @param properties
*/
protected int extractReplicationIntervalMilis(Properties properties) {
int asynchronousReplicationIntervalMillis;
String asynchronousReplicationIntervalMillisString =
PropertyUtil.extractAndLogProperty(ASYNCHRONOUS_REPLICATION_INTERVAL_MILLIS, properties);
if (asynchronousReplicationIntervalMillisString != null) {
try {
int asynchronousReplicationIntervalMillisCandidate =
Integer.parseInt(asynchronousReplicationIntervalMillisString);
if (asynchronousReplicationIntervalMillisCandidate < MINIMUM_REASONABLE_INTERVAL) {
LOG.warn("Trying to set the asynchronousReplicationIntervalMillis to an unreasonable number." +
" Using the default instead.");
asynchronousReplicationIntervalMillis = DEFAULT_ASYNCHRONOUS_REPLICATION_INTERVAL_MILLIS;
} else {
asynchronousReplicationIntervalMillis = asynchronousReplicationIntervalMillisCandidate;
}
} catch (NumberFormatException e) {
LOG.warn("Number format exception trying to set asynchronousReplicationIntervalMillis. " +
"Using the default instead. String value was: '" + asynchronousReplicationIntervalMillisString + "'");
asynchronousReplicationIntervalMillis = DEFAULT_ASYNCHRONOUS_REPLICATION_INTERVAL_MILLIS;
}
} else {
asynchronousReplicationIntervalMillis = DEFAULT_ASYNCHRONOUS_REPLICATION_INTERVAL_MILLIS;
}
return asynchronousReplicationIntervalMillis;
}
/**
* Extracts the value of replicateAsynchronously from the properties
* @param properties
*/
protected boolean extractReplicateAsynchronously(Properties properties) {
boolean replicateAsynchronously;
String replicateAsynchronouslyString = PropertyUtil.extractAndLogProperty(REPLICATE_ASYNCHRONOUSLY, properties);
if (replicateAsynchronouslyString != null) {
replicateAsynchronously = PropertyUtil.parseBoolean(replicateAsynchronouslyString);
} else {
replicateAsynchronously = true;
}
return replicateAsynchronously;
}
/**
* Extracts the value of replicateRemovals from the properties
* @param properties
*/
protected boolean extractReplicateRemovals(Properties properties) {
boolean replicateRemovals;
String replicateRemovalsString = PropertyUtil.extractAndLogProperty(REPLICATE_REMOVALS, properties);
if (replicateRemovalsString != null) {
replicateRemovals = PropertyUtil.parseBoolean(replicateRemovalsString);
} else {
replicateRemovals = true;
}
return replicateRemovals;
}
/**
* Extracts the value of replicateUpdatesViaCopy from the properties
* @param properties
*/
protected boolean extractReplicateUpdatesViaCopy(Properties properties) {
boolean replicateUpdatesViaCopy;
String replicateUpdatesViaCopyString = PropertyUtil.extractAndLogProperty(REPLICATE_UPDATES_VIA_COPY, properties);
if (replicateUpdatesViaCopyString != null) {
replicateUpdatesViaCopy = PropertyUtil.parseBoolean(replicateUpdatesViaCopyString);
} else {
replicateUpdatesViaCopy = true;
}
return replicateUpdatesViaCopy;
}
/**
* Extracts the value of replicateUpdates from the properties
* @param properties
*/
protected boolean extractReplicateUpdates(Properties properties) {
boolean replicateUpdates;
String replicateUpdatesString = PropertyUtil.extractAndLogProperty(REPLICATE_UPDATES, properties);
if (replicateUpdatesString != null) {
replicateUpdates = PropertyUtil.parseBoolean(replicateUpdatesString);
} else {
replicateUpdates = true;
}
return replicateUpdates;
}
/**
* Extracts the value of replicatePuts from the properties
* @param properties
*/
protected boolean extractReplicatePuts(Properties properties) {
boolean replicatePuts;
String replicatePutsString = PropertyUtil.extractAndLogProperty(REPLICATE_PUTS, properties);
if (replicatePutsString != null) {
replicatePuts = PropertyUtil.parseBoolean(replicatePutsString);
} else {
replicatePuts = true;
}
return replicatePuts;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy