Class ValkeyGlideConnectionFactory
- All Implemented Interfaces:
ValkeyConnectionFactory,org.springframework.beans.factory.DisposableBean,org.springframework.beans.factory.InitializingBean,org.springframework.context.Lifecycle,org.springframework.context.Phased,org.springframework.context.SmartLifecycle,org.springframework.dao.support.PersistenceExceptionTranslator
This factory creates a new ValkeyGlideConnection on each call to getConnection().
The underlying client instance is shared among all connections.
This class implements the InitializingBean interface,
triggering the creation of the client instance on afterPropertiesSet().
It also implements DisposableBean for closing the client on
application shutdown.
The Valkey Glide connection factory can be used both with a standalone Valkey server and with a
Valkey cluster. For standalone mode, use the ValkeyGlideConnectionFactory() constructor
or ValkeyGlideConnectionFactory(ValkeyGlideClientConfiguration) constructor. For cluster mode,
ensure the ValkeyGlideClientConfiguration is configured for cluster mode.
- Since:
- 2.0
- Author:
- Ilia Kolominsky
-
Field Summary
Fields inherited from interface org.springframework.context.SmartLifecycle
DEFAULT_PHASE -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newValkeyGlideConnectionFactoryinstance with default settings.ValkeyGlideConnectionFactory(ValkeyClusterConfiguration clusterConfiguration) Constructs a newValkeyGlideConnectionFactoryinstance with the givenValkeyClusterConfiguration.ValkeyGlideConnectionFactory(ValkeyClusterConfiguration clusterConfiguration, ValkeyGlideClientConfiguration valkeyGlideConfiguration) Constructs a newValkeyGlideConnectionFactoryinstance with the givenValkeyClusterConfigurationandValkeyGlideClientConfiguration.ValkeyGlideConnectionFactory(ValkeyGlideClientConfiguration valkeyGlideConfiguration) Deprecated.ValkeyGlideConnectionFactory(ValkeySentinelConfiguration sentinelConfiguration) Constructs a newValkeyGlideConnectionFactoryinstance using the givenValkeySentinelConfiguration.ValkeyGlideConnectionFactory(ValkeySentinelConfiguration sentinelConfiguration, ValkeyGlideClientConfiguration clientConfiguration) Constructs a newValkeyGlideConnectionFactoryinstance using the givenValkeySentinelConfigurationandValkeyGlideClientConfiguration.ValkeyGlideConnectionFactory(ValkeyStandaloneConfiguration standaloneConfiguration) ValkeyGlideConnectionFactory(ValkeyStandaloneConfiguration standaloneConfiguration, ValkeyGlideClientConfiguration valkeyGlideConfiguration) Constructs a newValkeyGlideConnectionFactoryinstance with the givenValkeyStandaloneConfigurationandValkeyGlideClientConfiguration. -
Method Summary
Modifier and TypeMethodDescriptionvoidInitialize the factory by pre-creating a pool of client instances if early startup is enabled.voiddestroy()Shut down the factory when this factory is destroyed.Returns the client name.Returns a suitableconnectionfor interacting with Valkey Cluster.Returns a suitableconnectionfor interacting with Valkey.booleanSpecifies if pipelined results should be converted to the expected data type.intReturns the index of the database.Returns the current host.Returns the password used for authenticating with the Valkey server.intgetPhase()intgetPort()Returns the current port.Returns a suitableconnectionfor interacting with Valkey Sentinel.booleanbooleanbooleanbooleanReturns if the client is running.booleanisUseSsl()Returns whether to use SSL.voidsetAutoStartup(boolean autoStartup) Configure if this Lifecycle connection factory should get started automatically by the container.voidsetEarlyStartup(boolean earlyStartup) Configure if this InitializingBean's component Lifecycle should get started early byafterPropertiesSet()at the time that the bean is initialized.voidsetExecutor(org.springframework.core.task.AsyncTaskExecutor executor) Set theAsyncTaskExecutorto use for asynchronous command execution.voidsetPhase(int phase) Specify the lifecycle phase for pausing and resuming this executor.voidstart()Initializes the factory, starting it.voidstop()Stops the factory.org.springframework.dao.DataAccessExceptionTranslates a Valkey-Glide exception to a Spring DAO exception.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.context.SmartLifecycle
stop
-
Constructor Details
-
ValkeyGlideConnectionFactory
public ValkeyGlideConnectionFactory()Constructs a newValkeyGlideConnectionFactoryinstance with default settings. -
ValkeyGlideConnectionFactory
-
ValkeyGlideConnectionFactory
public ValkeyGlideConnectionFactory(ValkeyStandaloneConfiguration standaloneConfiguration, ValkeyGlideClientConfiguration valkeyGlideConfiguration) Constructs a newValkeyGlideConnectionFactoryinstance with the givenValkeyStandaloneConfigurationandValkeyGlideClientConfiguration.- Parameters:
standaloneConfiguration- must not be nullclientConfiguration- must not be null- Since:
- 3.0
-
ValkeyGlideConnectionFactory
Constructs a newValkeyGlideConnectionFactoryinstance with the givenValkeyClusterConfiguration.- Parameters:
clusterConfiguration- must not be null- Since:
- 3.0
-
ValkeyGlideConnectionFactory
public ValkeyGlideConnectionFactory(ValkeyClusterConfiguration clusterConfiguration, ValkeyGlideClientConfiguration valkeyGlideConfiguration) Constructs a newValkeyGlideConnectionFactoryinstance with the givenValkeyClusterConfigurationandValkeyGlideClientConfiguration.- Parameters:
clusterConfiguration- must not be nullclusterClientConfiguration- must not be null- Since:
- 3.0
-
ValkeyGlideConnectionFactory
Constructs a newValkeyGlideConnectionFactoryinstance using the givenValkeySentinelConfiguration.- Parameters:
sentinelConfiguration- must not be null.- Throws:
UnsupportedOperationException- as Sentinel connections are not supported with Valkey-Glide.- Since:
- 3.0
-
ValkeyGlideConnectionFactory
public ValkeyGlideConnectionFactory(ValkeySentinelConfiguration sentinelConfiguration, ValkeyGlideClientConfiguration clientConfiguration) Constructs a newValkeyGlideConnectionFactoryinstance using the givenValkeySentinelConfigurationandValkeyGlideClientConfiguration.- Parameters:
sentinelConfiguration- must not be null.clientConfiguration- must not be null.- Throws:
UnsupportedOperationException- as Sentinel connections are not supported with Valkey-Glide.- Since:
- 3.0
-
ValkeyGlideConnectionFactory
@Deprecated public ValkeyGlideConnectionFactory(ValkeyGlideClientConfiguration valkeyGlideConfiguration) Deprecated.Constructs a newValkeyGlideConnectionFactoryinstance with the givenValkeyGlideClientConfiguration.- Parameters:
clientConfiguration- must not be null
-
-
Method Details
-
afterPropertiesSet
public void afterPropertiesSet()Initialize the factory by pre-creating a pool of client instances if early startup is enabled.- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean
-
getConnection
Description copied from interface:ValkeyConnectionFactoryReturns a suitableconnectionfor interacting with Valkey.- Specified by:
getConnectionin interfaceValkeyConnectionFactory- Returns:
connectionfor interacting with Valkey.
-
getClusterConnection
Description copied from interface:ValkeyConnectionFactoryReturns a suitableconnectionfor interacting with Valkey Cluster.- Specified by:
getClusterConnectionin interfaceValkeyConnectionFactory- Returns:
- a
connectionfor interacting with Valkey Cluster.
-
getConvertPipelineAndTxResults
public boolean getConvertPipelineAndTxResults()Description copied from interface:ValkeyConnectionFactorySpecifies if pipelined results should be converted to the expected data type.If false, results of
ValkeyConnection.closePipeline()andValkeyTxCommands.exec()will be of the type returned by the underlying driver. This method is mostly for backwards compatibility with 1.0. It is generally always a good idea to allow results to be converted and deserialized. In fact, this is now the default behavior.- Specified by:
getConvertPipelineAndTxResultsin interfaceValkeyConnectionFactory- Returns:
trueto convert pipeline and transaction results;falseotherwise.
-
getSentinelConnection
Description copied from interface:ValkeyConnectionFactoryReturns a suitableconnectionfor interacting with Valkey Sentinel.- Specified by:
getSentinelConnectionin interfaceValkeyConnectionFactory- Returns:
- a
connectionfor interacting with Valkey Sentinel.
-
destroy
public void destroy()Shut down the factory when this factory is destroyed. Closes all pooled client instances.- Specified by:
destroyin interfaceorg.springframework.beans.factory.DisposableBean
-
start
public void start()Initializes the factory, starting it.- Specified by:
startin interfaceorg.springframework.context.Lifecycle
-
stop
public void stop()Stops the factory.- Specified by:
stopin interfaceorg.springframework.context.Lifecycle
-
isRunning
public boolean isRunning()Returns if the client is running.- Specified by:
isRunningin interfaceorg.springframework.context.Lifecycle- Returns:
- true if running
-
isClusterAware
public boolean isClusterAware()- Returns:
- true if cluster mode is enabled.
-
getClientConfiguration
- Returns:
- The client configuration used.
-
getStandaloneConfiguration
- Returns:
- the
ValkeyStandaloneConfiguration, may be null. - Since:
- 3.0
-
getClusterConfiguration
- Returns:
- the
ValkeyClusterConfiguration, may be null. - Since:
- 3.0
-
getSentinelConfiguration
- Returns:
- the
ValkeySentinelConfiguration, may be null. - Throws:
UnsupportedOperationException- as Sentinel connections are not supported with Valkey-Glide.- Since:
- 3.0
-
setExecutor
public void setExecutor(org.springframework.core.task.AsyncTaskExecutor executor) Set theAsyncTaskExecutorto use for asynchronous command execution.- Parameters:
executor-executorused to execute commands asynchronously.- Since:
- 3.2
-
getHostName
Returns the current host.- Returns:
- the host.
-
getPort
public int getPort()Returns the current port.- Returns:
- the port.
-
getDatabase
public int getDatabase()Returns the index of the database.- Returns:
- the database index.
-
getClientName
Returns the client name.- Returns:
- the client name or null if not set.
-
isUseSsl
public boolean isUseSsl()Returns whether to use SSL.- Returns:
- use of SSL.
-
getPassword
Returns the password used for authenticating with the Valkey server.- Returns:
- password for authentication or null if not set.
-
isAutoStartup
public boolean isAutoStartup()- Specified by:
isAutoStartupin interfaceorg.springframework.context.SmartLifecycle- Returns:
- whether this lifecycle component should get started automatically by the container
-
setAutoStartup
public void setAutoStartup(boolean autoStartup) Configure if this Lifecycle connection factory should get started automatically by the container.- Parameters:
autoStartup- true to automaticallystart()the connection factory; false otherwise.
-
isEarlyStartup
public boolean isEarlyStartup()- Returns:
- whether to
start()the component duringafterPropertiesSet().
-
setEarlyStartup
public void setEarlyStartup(boolean earlyStartup) Configure if this InitializingBean's component Lifecycle should get started early byafterPropertiesSet()at the time that the bean is initialized. The component defaults to auto-startup.- Parameters:
earlyStartup- true to earlystart()the component; false otherwise.
-
getPhase
public int getPhase()- Specified by:
getPhasein interfaceorg.springframework.context.Phased- Specified by:
getPhasein interfaceorg.springframework.context.SmartLifecycle- Returns:
- the phase value for this lifecycle component
-
setPhase
public void setPhase(int phase) Specify the lifecycle phase for pausing and resuming this executor.- Parameters:
phase- the phase value to set
-
translateExceptionIfPossible
@Nullable public org.springframework.dao.DataAccessException translateExceptionIfPossible(RuntimeException ex) Translates a Valkey-Glide exception to a Spring DAO exception.- Specified by:
translateExceptionIfPossiblein interfaceorg.springframework.dao.support.PersistenceExceptionTranslator- Parameters:
ex- The exception to translate- Returns:
- The translated exception, or null if the exception cannot be translated
-
ValkeyGlideConnectionFactory(ValkeyStandaloneConfiguration, ValkeyGlideClientConfiguration)orValkeyGlideConnectionFactory(ValkeyClusterConfiguration, ValkeyGlideClusterClientConfiguration)instead