Commit 6d18f8a1 authored by GregDThomas's avatar GregDThomas Committed by daryl herzmann

Ensure that Hazelcast backed Cache objects have the correct settings (#847)

parent c399e891
......@@ -72,6 +72,9 @@ public interface Cache<K,V> extends java.util.Map<K,V> {
* than the max size, the least frequently used items will be removed. If
* the max cache size is set to -1, there is no size limit.
*
*<p><strong>Note:</strong> If using the Hazelcast clustering plugin, this will not take
* effect until the next time the cache is created</p>
*
* @param maxSize the maximum size of the cache in bytes.
*/
void setMaxCacheSize(int maxSize);
......@@ -79,7 +82,7 @@ public interface Cache<K,V> extends java.util.Map<K,V> {
/**
* Returns the maximum number of milliseconds that any object can live
* in cache. Once the specified number of milliseconds passes, the object
* will be automatically expried from cache. If the max lifetime is set
* will be automatically expired from cache. If the max lifetime is set
* to -1, then objects never expire.
*
* @return the maximum number of milliseconds before objects are expired.
......@@ -89,9 +92,12 @@ public interface Cache<K,V> extends java.util.Map<K,V> {
/**
* Sets the maximum number of milliseconds that any object can live
* in cache. Once the specified number of milliseconds passes, the object
* will be automatically expried from cache. If the max lifetime is set
* will be automatically expired from cache. If the max lifetime is set
* to -1, then objects never expire.
*
*<p><strong>Note:</strong> If using the Hazelcast clustering plugin, this will not take
* effect until the next time the cache is created</p>
*
* @param maxLifetime the maximum number of milliseconds before objects are expired.
*/
void setMaxLifetime(long maxLifetime);
......
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<class>org.jivesoftware.openfire.plugin.HazelcastPlugin</class>
<name>Hazelcast plugin</name>
<description>Adds clustering support</description>
<author>Tom Evans</author>
<version>2.2.1</version>
<date>11/04/2016</date>
<minServerVersion>4.0.0</minServerVersion>
</plugin>
<?xml version="1.0" encoding="UTF-8"?>
<plugin>
<class>org.jivesoftware.openfire.plugin.HazelcastPlugin</class>
<name>Hazelcast plugin</name>
<description>Adds clustering support</description>
<author>Tom Evans</author>
<version>2.2.2</version>
<date>08/03/2017</date>
<minServerVersion>4.0.0</minServerVersion>
</plugin>
......@@ -16,24 +16,14 @@
package org.jivesoftware.openfire.plugin.util.cache;
import java.io.Serializable;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import com.hazelcast.config.ClasspathXmlConfig;
import com.hazelcast.config.Config;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MaxSizeConfig;
import com.hazelcast.core.Cluster;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.Member;
import org.jivesoftware.openfire.JMXManager;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.cluster.ClusterNodeInfo;
......@@ -44,6 +34,7 @@ import org.jivesoftware.openfire.plugin.util.cluster.HazelcastClusterNodeInfo;
import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.StringUtils;
import org.jivesoftware.util.cache.Cache;
import org.jivesoftware.util.cache.CacheFactory;
import org.jivesoftware.util.cache.CacheFactoryStrategy;
import org.jivesoftware.util.cache.CacheWrapper;
import org.jivesoftware.util.cache.ClusterTask;
......@@ -52,12 +43,23 @@ import org.jivesoftware.util.cache.ExternalizableUtilStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hazelcast.config.ClasspathXmlConfig;
import com.hazelcast.config.Config;
import com.hazelcast.core.Cluster;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.Member;
import java.io.Serializable;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
/**
* CacheFactory implementation to use when using Hazelcast in cluster mode.
......@@ -211,6 +213,15 @@ public class ClusteredCacheFactory implements CacheFactoryStrategy {
if (state == State.stopped) {
throw new IllegalStateException("Cannot create clustered cache when not in a cluster");
}
// Determine the time to live. Note that in Hazelcast 0 means "forever", not -1
final long openfireLifetimeInMilliseconds = CacheFactory.getMaxCacheLifetime(name);
final int hazelcastLifetimeInSeconds = openfireLifetimeInMilliseconds < 0 ? 0 : (int) (openfireLifetimeInMilliseconds / 1000);
// Determine the max cache size. Note that in Hazelcast the max cache size must be positive
final long openfireMaxCacheSize = CacheFactory.getMaxCacheSize(name);
final int hazelcastMaxCacheSize = openfireMaxCacheSize < 0 ? Integer.MAX_VALUE : (int) openfireMaxCacheSize;
final MapConfig mapConfig = hazelcast.getConfig().getMapConfig(name);
mapConfig.setTimeToLiveSeconds(hazelcastLifetimeInSeconds);
mapConfig.setMaxSizeConfig(new MaxSizeConfig(hazelcastMaxCacheSize, MaxSizeConfig.MaxSizePolicy.USED_HEAP_SIZE));
return new ClusteredCache(name, hazelcast.getMap(name));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment