Package org.jivesoftware.util.cache
Class CacheFactory
- java.lang.Object
-
- org.jivesoftware.util.cache.CacheFactory
-
public class CacheFactory extends Object
Creates Cache objects. The returned caches will either be local or clustered depending on the clustering enabled setting and a user's license.When clustered caching is turned on, cache usage statistics for all caches that have been created are periodically published to the clustered cache named "opt-$cacheStats".
-
-
Field Summary
Fields Modifier and Type Field Description static String
CLUSTERED_CACHE_PROPERTY_NAME
static long
DEFAULT_MAX_CACHE_LIFETIME
static int
DEFAULT_MAX_CACHE_SIZE
static String
LOCAL_CACHE_PROPERTY_NAME
-
Method Summary
All Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static void
clearCaches()
static void
clearCaches(String... cacheName)
static <T extends Cache>
TcreateCache(String name)
Returns the named cache, creating it as necessary.static <T extends Cache>
TcreateLocalCache(String name)
Returns the named local cache, creating it as necessary.static <T extends Cache>
TcreateSerializingCache(String name, Class keyClass, Class valueClass)
Returns the serializing cache, creating it as necessary.static void
destroyCache(String name)
Destroys the cache for the cache name specified.static void
doClusterTask(ClusterTask<?> task)
Invokes a task on other cluster members in an asynchronous fashion.static void
doClusterTask(ClusterTask<?> task, byte[] nodeID)
Invokes a task on a given cluster member in an asynchronous fashion.static <T> Collection<T>
doSynchronousClusterTask(ClusterTask<T> task, boolean includeLocalMember)
Invokes a task on other cluster members synchronously and returns the result as a Collection (method will not return until the task has been executed on each cluster member).static <T> T
doSynchronousClusterTask(ClusterTask<T> task, byte[] nodeID)
Invokes a task on a given cluster member synchronously and returns the result of the remote operation.static Cache[]
getAllCaches()
Returns an array of all caches in the system.static String
getCacheTypeProperty(String cacheName)
static byte[]
getClusterMemberID()
Returns a byte[] that uniquely identifies this member within the cluster ornull
when not in a cluster.static ClusterNodeInfo
getClusterNodeInfo(byte[] nodeID)
Returns the node info for the given cluster nodestatic Collection<ClusterNodeInfo>
getClusterNodesInfo()
Returns basic information about the current members of the cluster or an empty collection if not running in a cluster.static long
getClusterTime()
Gets the pseudo-synchronized time from the cluster.static Lock
getLock(Object key, Cache cache)
Deprecated.in favour ofCache.getLock(K)
.static long
getMaxCacheLifetime(String cacheName)
If a local property is found for the supplied name which specifies a value for cache entry lifetime, it is returned.static long
getMaxCacheSize(String cacheName)
If a local property is found for the supplied name which specifies a value for cache size, it is returned.static int
getMaxClusterNodes()
Returns the maximum number of cluster members allowed.static long
getMinCacheSize(String cacheName)
static String
getPluginName()
static byte[]
getSeniorClusterMemberID()
Returns a byte[] that uniquely identifies this senior cluster member ornull
when not in a cluster.static boolean
hasMaxLifetimeFromProperty(String cacheName)
static boolean
hasMaxSizeFromProperty(String cacheName)
static void
initialize()
static boolean
isClusteringAvailable()
Returns true if clustering is installed and can be used by this JVM to join a cluster.static boolean
isClusteringStarted()
Returns true if this node is currently a member of a cluster.static boolean
isClusteringStarting()
Returns true is clustering is currently being started.static boolean
isSeniorClusterMember()
Returns true if this member is the senior member in the cluster.static void
joinedCluster()
Notification message indicating that this JVM has joined a cluster.static void
leftCluster()
Notification message indicating that this JVM has left the cluster.static void
setCacheTypeProperty(String cacheName, String type)
static void
setMaxLifetimeProperty(String cacheName, long lifetime)
Sets a local property which overrides the maximum cache entry lifetime for the supplied cache name.static void
setMaxSizeProperty(String cacheName, long size)
Sets a local property which overrides the maximum cache size for the supplied cache name.static void
setMinCacheSize(String cacheName, long size)
static void
startClustering()
static void
stopClustering()
-
-
-
Field Detail
-
LOCAL_CACHE_PROPERTY_NAME
public static String LOCAL_CACHE_PROPERTY_NAME
-
CLUSTERED_CACHE_PROPERTY_NAME
public static String CLUSTERED_CACHE_PROPERTY_NAME
-
DEFAULT_MAX_CACHE_SIZE
public static final int DEFAULT_MAX_CACHE_SIZE
- See Also:
- Constant Field Values
-
DEFAULT_MAX_CACHE_LIFETIME
public static final long DEFAULT_MAX_CACHE_LIFETIME
-
-
Method Detail
-
getMaxCacheSize
public static long getMaxCacheSize(String cacheName)
If a local property is found for the supplied name which specifies a value for cache size, it is returned. Otherwise, the defaultSize argument is returned.- Parameters:
cacheName
- the name of the cache to look up a corresponding property for.- Returns:
- either the property value or the default value.
-
setMaxSizeProperty
public static void setMaxSizeProperty(String cacheName, long size)
Sets a local property which overrides the maximum cache size for the supplied cache name.- Parameters:
cacheName
- the name of the cache to store a value for.size
- the maximum cache size.
-
hasMaxSizeFromProperty
public static boolean hasMaxSizeFromProperty(String cacheName)
-
getMaxCacheLifetime
public static long getMaxCacheLifetime(String cacheName)
If a local property is found for the supplied name which specifies a value for cache entry lifetime, it is returned. Otherwise, the defaultLifetime argument is returned.- Parameters:
cacheName
- the name of the cache to look up a corresponding property for.- Returns:
- either the property value or the default value.
-
setMaxLifetimeProperty
public static void setMaxLifetimeProperty(String cacheName, long lifetime)
Sets a local property which overrides the maximum cache entry lifetime for the supplied cache name.- Parameters:
cacheName
- the name of the cache to store a value for.lifetime
- the maximum cache entry lifetime.
-
hasMaxLifetimeFromProperty
public static boolean hasMaxLifetimeFromProperty(String cacheName)
-
setMinCacheSize
public static void setMinCacheSize(String cacheName, long size)
-
getMinCacheSize
public static long getMinCacheSize(String cacheName)
-
getAllCaches
public static Cache[] getAllCaches()
Returns an array of all caches in the system.- Returns:
- an array of all caches in the system.
-
createCache
public static <T extends Cache> T createCache(String name)
Returns the named cache, creating it as necessary.- Type Parameters:
T
- the type cache being created- Parameters:
name
- the name of the cache to create.- Returns:
- the named cache, creating it as necessary.
-
createSerializingCache
public static <T extends Cache> T createSerializingCache(String name, Class keyClass, Class valueClass)
Returns the serializing cache, creating it as necessary. Unlike the caches returned bycreateCache(String)
, the caches returned by this method store data in serialized form (without a reference to their class). The primary benefit of usage of this cache is that the cached data is stored without any references to their classes. This allows cache content to remain usable after the classes that instantiate the data get reloaded. This is of particular interest when the cache is used to store data provided by Openfire plugins (as these classes get loaded by a class loader that is replaced when a plugin gets reloaded or upgraded). As compared to other caches, usage of this cache will require more system resources, as the serialized representation of an object typically is (much) larger than its original (unserialized) form.- Parameters:
name
- The name of the cache to create.keyClass
- The class of instances used as keys.valueClass
- The class of instances used as values.- Returns:
- the named cache, creating it as necessary.
- See Also:
- Issue OF-2239: Make it easier to cache plugin class instances
-
createLocalCache
public static <T extends Cache> T createLocalCache(String name)
Returns the named local cache, creating it as necessary.- Type Parameters:
T
- the type cache being created- Parameters:
name
- the name of the cache to create.- Returns:
- the named cache, creating it as necessary.
-
destroyCache
public static void destroyCache(String name)
Destroys the cache for the cache name specified.- Parameters:
name
- the name of the cache to destroy.
-
getLock
@Deprecated public static Lock getLock(Object key, Cache cache)
Deprecated.in favour ofCache.getLock(K)
. Will be removed in Openfire 5.0.0.Returns an existing
Lock
on the specified key or creates a new one if none was found. This operation is thread safe. Successive calls with the same key may or may not return the sameLock
. However, different threads asking for the same Lock at the same time will get the same Lock object.The supplied cache may or may not be used depending whether the server is running on cluster mode or not. When not running as part of a cluster then the lock will be unrelated to the cache and will only be visible in this JVM.
- Parameters:
key
- the object that defines the visibility or scope of the lock.cache
- the cache used for holding the lock.- Returns:
- an existing lock on the specified key or creates a new one if none was found.
-
isClusteringAvailable
public static boolean isClusteringAvailable()
Returns true if clustering is installed and can be used by this JVM to join a cluster. A false value could mean that either clustering support is not available or the license does not allow to have more than 1 cluster node.- Returns:
- true if clustering is installed and can be used by this JVM to join a cluster.
-
isClusteringStarting
public static boolean isClusteringStarting()
Returns true is clustering is currently being started. Once the cluster is started or failed to be started this value will be false.- Returns:
- true is clustering is currently being started.
-
isClusteringStarted
public static boolean isClusteringStarted()
Returns true if this node is currently a member of a cluster. The last step of application initialization is to join a cluster, so this method returns false during most of application startup.- Returns:
- true if this node is currently a member of a cluster.
-
getClusterMemberID
public static byte[] getClusterMemberID()
Returns a byte[] that uniquely identifies this member within the cluster ornull
when not in a cluster.- Returns:
- a byte[] that uniquely identifies this member within the cluster or null when not in a cluster.
-
clearCaches
public static void clearCaches()
-
clearCaches
public static void clearCaches(String... cacheName)
-
getSeniorClusterMemberID
public static byte[] getSeniorClusterMemberID()
Returns a byte[] that uniquely identifies this senior cluster member ornull
when not in a cluster.- Returns:
- a byte[] that uniquely identifies this senior cluster member or null when not in a cluster.
-
isSeniorClusterMember
public static boolean isSeniorClusterMember()
Returns true if this member is the senior member in the cluster. If clustering is not enabled, this method will also return true. This test is useful for tasks that should only be run on a single member in a cluster.- Returns:
- true if this cluster member is the senior or if clustering is not enabled.
-
getClusterNodesInfo
public static Collection<ClusterNodeInfo> getClusterNodesInfo()
Returns basic information about the current members of the cluster or an empty collection if not running in a cluster.- Returns:
- information about the current members of the cluster or an empty collection if not running in a cluster.
-
getMaxClusterNodes
public static int getMaxClusterNodes()
Returns the maximum number of cluster members allowed. A value of 0 will be returned when clustering is not allowed.- Returns:
- the maximum number of cluster members allowed or 0 if clustering is not allowed.
-
getClusterTime
public static long getClusterTime()
Gets the pseudo-synchronized time from the cluster. While the cluster members may have varying system times, this method is expected to return a timestamp that is synchronized (or nearly so; best effort) across the cluster.- Returns:
- Synchronized time for all cluster members
-
doClusterTask
public static void doClusterTask(ClusterTask<?> task)
Invokes a task on other cluster members in an asynchronous fashion. The task will not be executed on the local cluster member. If clustering is not enabled, this method will do nothing.- Parameters:
task
- the task to be invoked on all other cluster members.
-
doClusterTask
public static void doClusterTask(ClusterTask<?> task, byte[] nodeID)
Invokes a task on a given cluster member in an asynchronous fashion. If clustering is not enabled, this method will do nothing.- Parameters:
task
- the task to be invoked on the specified cluster member.nodeID
- the byte array that identifies the target cluster member.- Throws:
IllegalStateException
- if requested node was not found or not running in a cluster.
-
doSynchronousClusterTask
public static <T> Collection<T> doSynchronousClusterTask(ClusterTask<T> task, boolean includeLocalMember)
Invokes a task on other cluster members synchronously and returns the result as a Collection (method will not return until the task has been executed on each cluster member). The task will not be executed on the local cluster member. If clustering is not enabled, this method will return an empty collection.- Type Parameters:
T
- the return type of the cluster task- Parameters:
task
- the ClusterTask object to be invoked on all other cluster members.includeLocalMember
- true to run the task on the local member, false otherwise- Returns:
- collection with the result of the execution.
-
doSynchronousClusterTask
public static <T> T doSynchronousClusterTask(ClusterTask<T> task, byte[] nodeID)
Invokes a task on a given cluster member synchronously and returns the result of the remote operation. If clustering is not enabled, this method will return null.- Type Parameters:
T
- the return type of the cluster task- Parameters:
task
- the ClusterTask object to be invoked on a given cluster member.nodeID
- the byte array that identifies the target cluster member.- Returns:
- result of remote operation or null if operation failed or operation returned null.
- Throws:
IllegalStateException
- if requested node was not found or not running in a cluster.
-
getClusterNodeInfo
public static ClusterNodeInfo getClusterNodeInfo(byte[] nodeID)
Returns the node info for the given cluster node- Parameters:
nodeID
- The target cluster node- Returns:
- The info for the cluster node or null if not found
-
getPluginName
public static String getPluginName()
-
initialize
public static void initialize() throws InitializationException
- Throws:
InitializationException
-
startClustering
public static void startClustering()
-
stopClustering
public static void stopClustering()
-
joinedCluster
public static void joinedCluster()
Notification message indicating that this JVM has joined a cluster.
-
leftCluster
public static void leftCluster()
Notification message indicating that this JVM has left the cluster.
-
-