Class HazelcastCachingProvider
- All Implemented Interfaces:
Closeable,AutoCloseable,javax.cache.spi.CachingProvider
CachingProvider.
This provider class is registered as a CachingProvider implementation.
When Hazelcast is the only CachingProvider on the classpath,
using Caching.getCachingProvider() will instantiate and return
an instance of this class.
This provider implementation delegates to a CachingProvider backed
by either a member- or a client-side HazelcastInstance:
HazelcastMemberCachingProvideris the member-sideCachingProviderimplementationHazelcastClientCachingProvideris the client-sideCachingProviderimplementation
Provider Type Selection
When using Caching.getCachingProvider() without a class name argument,
this provider is instantiated. The choice between member- or client-side provider
is made by inspecting the value of system property
hazelcast.jcache.provider.type:
- If no value was set, then the client-side caching provider is selected
- If a value was set, then value
memberselects the member-side caching provider, while valueclientselects the client-side provider. Legacy valueserveris also accepted as an alias formemberfor backwards compatibility, however its usage is discouraged and will be removed in a future version. Other values result in aCacheExceptionbeing thrown.
When using one of Caching#getCachingProvider variants with an explicit
class name argument, then:
- using
com.hazelcast.cache.HazelcastCachingProvideras class name is identical to usingCaching.getCachingProvider(); choice between member- or client-side caching provider is performed via system propertyhazelcast.jcache.provider.typeas described above. - using
MEMBER_CACHING_PROVIDERas class name will return a member-side caching provider - using
CLIENT_CACHING_PROVIDERas class name will return a client-side caching provider
Creating or reusing HazelcastInstances with CacheManagers
Arguments used withCachingProvider.getCacheManager(URI, ClassLoader, Properties)
and its variants control whether a HazelcastInstance will be created or reused
to back the CacheManager being created:
- Property
hazelcast.config.locationspecifies a URI to locate a Hazelcast member or client configuration file. Supportsclasspath:,file:,http:andhttps:URI schemes. Examples:classpath:com/acme/hazelcast.xmlwill locatehazelcast.xmlin packagecom.acme,http://internal.acme.com/hazelcast.xmlwill locate the configuration from the given HTTP URL. - Property
hazelcast.instance.namespecifies the instance name of a runningHazelcastInstance. If no instance is found running by that name, then a newHazelcastInstanceis started with a default configuration and the given instance name. - In any
CachingProvider#getCacheManagervariant that accepts aURIas argument, and if no properties were provided or properties did not result in resolving a specificHazelcastInstance, then theURIargument is interpreted as a Hazelcast config location as follows:- if
URIstarts with one of supported schemes (classpath:,http:,https:,file:), then a Hazelcast XML configuration is loaded from that location. - otherwise,
URIis interpreted as a system property. IfSystem.getProperty(URI)returns a value that starts with one of supported schemes above, then a Hazelcast XML configuration is loaded from that location. - if
URIor its resolved value as a system property does not start with a supported URI scheme, a defaultHazelcastInstancenamed "_hzinstance_jcache_shared" is created or used, if it already exists.
- if
propertiesByLocation(String) and propertiesByInstanceName(String)
will create an appropriate Properties instance for use with
getCacheManager(URI, ClassLoader, Properties).
Examples
Obtain a member-side caching provider backed by an existing HazelcastInstance.
In this example the member-side caching provider is selected by setting the value of
system property hazelcast.jcache.provider.type to value "member". An existing
HazelcastInstance is referenced by instance name in the Properties provided as
argument to CachingProvider.getCacheManager(URI, ClassLoader, Properties).
Config config = new Config();
config.setInstanceName("hz-jcache");
HazelcastInstance member = Hazelcast.newHazelcastInstance(config);
System.setProperty("hazelcast.jcache.provider.type", "member");
CachingProvider provider = Caching.getCachingProvider();
CacheManager manager = provider.getCacheManager(null, null, HazelcastCachingProvider.propertiesByInstanceName("hz-jcache"));
Cache cache = manager.createCache("sessions", new MutableConfiguration());
cache.put("a", "b");
Obtain a client-side caching provider, starting a default client HazelcastInstance
In this example the client-side caching provider is selected as default option. A new
client-side HazelcastInstance is created with default configuration once
CachingProvider.getCacheManager() is called.
// start a Hazelcast member for the client to connect to
HazelcastInstance member = Hazelcast.newHazelcastInstance();
// obtain a client-side (default) CachingProvider
CachingProvider provider = Caching.getCachingProvider();
// obtain the default CacheManager; since there is no JCache-backing client-side
// HazelcastInstance started, this will start a new instance
CacheManager manager = provider.getCacheManager();
Cache cache = manager.createCache("sessions", new MutableConfiguration());
cache.put("a", "b");
- Since:
- 3.4
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringClass name of the client-side Caching Providerstatic final StringHazelcast config location propertystatic final StringHazelcast instance itself propertystatic final StringHazelcast instance name propertystatic final StringClass name of the member-side Caching Providerstatic final StringDeprecated.static final StringName of defaultHazelcastInstancewhich may be started when obtaining the defaultCachingProvider. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidclose(ClassLoader classLoader) voidclose(URI uri, ClassLoader classLoader) javax.cache.CacheManagerjavax.cache.CacheManagergetCacheManager(URI uri, ClassLoader classLoader) javax.cache.CacheManagergetCacheManager(URI uri, ClassLoader classLoader, Properties properties) booleanisSupported(javax.cache.configuration.OptionalFeature optionalFeature) static PropertiespropertiesByInstanceItself(HazelcastInstance instance) Create thePropertieswith the provided instance itself.static PropertiespropertiesByInstanceName(String instanceName) Create thePropertieswith the provided instance name.static PropertiespropertiesByLocation(String configFileLocation) Create thePropertieswith the provided config file location.toString()
-
Field Details
-
HAZELCAST_CONFIG_LOCATION
Hazelcast config location property- See Also:
-
HAZELCAST_INSTANCE_NAME
Hazelcast instance name property- See Also:
-
HAZELCAST_INSTANCE_ITSELF
Hazelcast instance itself property- See Also:
-
MEMBER_CACHING_PROVIDER
Class name of the member-side Caching Provider -
SERVER_CACHING_PROVIDER
Deprecated.Same value asMEMBER_CACHING_PROVIDER. This field is maintained for backwards compatibility. Its use is discouraged and will be removed in a future version. -
CLIENT_CACHING_PROVIDER
Class name of the client-side Caching Provider -
SHARED_JCACHE_INSTANCE_NAME
Name of defaultHazelcastInstancewhich may be started when obtaining the defaultCachingProvider.- See Also:
-
-
Constructor Details
-
HazelcastCachingProvider
public HazelcastCachingProvider()
-
-
Method Details
-
propertiesByLocation
Create thePropertieswith the provided config file location.- Parameters:
configFileLocation- the location of the config file to configure- Returns:
- properties instance pre-configured with the configuration location
-
propertiesByInstanceName
Create thePropertieswith the provided instance name.- Parameters:
instanceName- the instance name to configure- Returns:
- the properties instance pre-configured with the instance name
-
propertiesByInstanceItself
Create thePropertieswith the provided instance itself.- Parameters:
instance- the instance itself to be used- Returns:
- the properties instance pre-configured with the instance itself
-
getCacheManager
public javax.cache.CacheManager getCacheManager(URI uri, ClassLoader classLoader, Properties properties) - Specified by:
getCacheManagerin interfacejavax.cache.spi.CachingProvider
-
getDefaultClassLoader
- Specified by:
getDefaultClassLoaderin interfacejavax.cache.spi.CachingProvider
-
getDefaultURI
- Specified by:
getDefaultURIin interfacejavax.cache.spi.CachingProvider
-
getDefaultProperties
- Specified by:
getDefaultPropertiesin interfacejavax.cache.spi.CachingProvider
-
getCacheManager
- Specified by:
getCacheManagerin interfacejavax.cache.spi.CachingProvider
-
getCacheManager
public javax.cache.CacheManager getCacheManager()- Specified by:
getCacheManagerin interfacejavax.cache.spi.CachingProvider
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfacejavax.cache.spi.CachingProvider- Specified by:
closein interfaceCloseable
-
close
- Specified by:
closein interfacejavax.cache.spi.CachingProvider
-
close
- Specified by:
closein interfacejavax.cache.spi.CachingProvider
-
isSupported
public boolean isSupported(javax.cache.configuration.OptionalFeature optionalFeature) - Specified by:
isSupportedin interfacejavax.cache.spi.CachingProvider
-
toString
-