Class JedisConnectionFactory
- All Implemented Interfaces:
DisposableBean,InitializingBean,Lifecycle,Phased,SmartLifecycle,PersistenceExceptionTranslator,RedisConnectionFactory
JedisConnectionFactory should be configured using an environmental configuration and the
client configuration. Jedis supports the following environmental configurations:
This connection factory implements InitializingBean and SmartLifecycle for flexible lifecycle
control. It must be initialized and started before you can obtain a
connection. Initialization starts this bean
early by default. You can Lifecycle.stop() and restart this connection factory if needed. Disabling early startup leaves lifecycle
management to the container refresh if auto-startup is enabled.
Note that JedisConnection and its clustered variant are not Thread-safe and
instances should not be shared across threads. Refer to the
Jedis
documentation for guidance on configuring Jedis in a multithreaded environment.
- Author:
- Costin Leau, Thomas Darimont, Christoph Strobl, Mark Paluch, Fu Jian, Ajith Kumar
- See Also:
-
JedisClientConfigurationJedis
-
Field Summary
Fields inherited from interface org.springframework.context.SmartLifecycle
DEFAULT_PHASE -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a newJedisConnectionFactoryinstance with default settings (default connection pooling).JedisConnectionFactory(RedisClusterConfiguration clusterConfiguration) Constructs a newJedisConnectionFactoryinstance using the givenRedisClusterConfigurationapplied to create aJedisCluster.JedisConnectionFactory(RedisClusterConfiguration clusterConfiguration, JedisClientConfiguration clientConfiguration) Constructs a newJedisConnectionFactoryinstance using the givenRedisClusterConfigurationandJedisClientConfiguration.JedisConnectionFactory(RedisClusterConfiguration clusterConfiguration, redis.clients.jedis.JedisPoolConfig poolConfig) Constructs a newJedisConnectionFactoryinstance using the givenRedisClusterConfigurationapplied to create aJedisCluster.JedisConnectionFactory(RedisSentinelConfiguration sentinelConfiguration) Constructs a newJedisConnectionFactoryinstance using the givenJedisPoolConfigapplied toJedisSentinelPool.JedisConnectionFactory(RedisSentinelConfiguration sentinelConfiguration, JedisClientConfiguration clientConfiguration) Constructs a newJedisConnectionFactoryinstance using the givenRedisSentinelConfigurationandJedisClientConfiguration.JedisConnectionFactory(RedisSentinelConfiguration sentinelConfiguration, redis.clients.jedis.JedisPoolConfig poolConfig) Constructs a newJedisConnectionFactoryinstance using the givenJedisPoolConfigapplied toJedisSentinelPool.JedisConnectionFactory(RedisStandaloneConfiguration standaloneConfiguration) Constructs a newJedisConnectionFactoryinstance using the givenRedisStandaloneConfiguration.JedisConnectionFactory(RedisStandaloneConfiguration standaloneConfiguration, JedisClientConfiguration clientConfiguration) Constructs a newJedisConnectionFactoryinstance using the givenRedisStandaloneConfigurationandJedisClientConfiguration.JedisConnectionFactory(redis.clients.jedis.JedisPoolConfig poolConfig) Constructs a newJedisConnectionFactoryinstance using the given pool configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoidprotected redis.clients.jedis.JedisClustercreateCluster(RedisClusterConfiguration clusterConfig, org.apache.commons.pool2.impl.GenericObjectPoolConfig<redis.clients.jedis.Connection> poolConfig) protected redis.clients.jedis.util.Pool<redis.clients.jedis.Jedis>CreatesJedisPool.protected redis.clients.jedis.util.Pool<redis.clients.jedis.Jedis>CreatesJedisSentinelPool.protected ClusterTopologyProvidercreateTopologyProvider(redis.clients.jedis.JedisCluster cluster) Template method to create aClusterTopologyProvidergivenJedisCluster.voiddestroy()protected redis.clients.jedis.JedisReturns a Jedis instance to be used as a Redis connection.Returns the client name.Returns a suitableconnectionfor interacting with Redis Cluster.Returns a suitableconnectionfor interacting with Redis.booleanSpecifies if pipelined results should be converted to the expected data type.intReturns the index of the database.Returns the Redis hostname.Returns the password used for authenticating with the Redis server.intgetPhase()<T> org.apache.commons.pool2.impl.GenericObjectPoolConfig<T>Returns the poolConfig.intgetPort()Returns the port used to connect to the Redis instance.Returns a suitableconnectionfor interacting with Redis Sentinel.intReturns the timeout.booleanIndicates the use of a connection pool.booleanbooleanbooleanbooleanbooleanbooleanisUseSsl()Returns whether to use SSL.protected JedisClusterConnectionpostProcessConnection(JedisClusterConnection connection) Post process a newly retrieved connection.protected JedisConnectionpostProcessConnection(JedisConnection connection) Post process a newly retrieved connection.voidsetAutoStartup(boolean autoStartup) Configure if this Lifecycle connection factory should get started automatically by the container at the time that the containing ApplicationContext gets refreshed.voidsetClientName(String clientName) Deprecated.voidsetConvertPipelineAndTxResults(boolean convertPipelineAndTxResults) Specifies if pipelined results should be converted to the expected data type.voidsetDatabase(int index) Deprecated.since 2.0, configure the database index usingRedisStandaloneConfigurationorRedisSentinelConfiguration.voidsetEarlyStartup(boolean earlyStartup) Configure if this InitializingBean's component Lifecycle should get started early byafterPropertiesSet()at the time that the bean is initialized.voidsetExecutor(AsyncTaskExecutor executor) Configures theexecutorused to execute commands asynchronously across the cluster.voidsetHostName(String hostName) Deprecated.since 2.0, configure the hostname usingRedisStandaloneConfiguration.voidsetPassword(String password) Deprecated.since 2.0, configure the password usingRedisStandaloneConfiguration,RedisSentinelConfigurationorRedisClusterConfiguration.voidsetPhase(int phase) Specify the lifecycle phase for pausing and resuming this executor.voidsetPoolConfig(redis.clients.jedis.JedisPoolConfig poolConfig) Deprecated.since 2.0, configureJedisPoolConfigusingJedisClientConfiguration.voidsetPort(int port) Deprecated.since 2.0, configure the port usingRedisStandaloneConfiguration.voidsetTimeout(int timeout) Deprecated.since 2.0, configure the timeout usingJedisClientConfiguration.voidsetUsePool(boolean usePool) Deprecated.since 2.0, configure pooling usage withJedisClientConfiguration.voidsetUseSsl(boolean useSsl) Deprecated.since 2.0, configure the SSL usage withJedisClientConfiguration.voidstart()voidstop()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
-
JedisConnectionFactory
public JedisConnectionFactory()Constructs a newJedisConnectionFactoryinstance with default settings (default connection pooling). -
JedisConnectionFactory
public JedisConnectionFactory(redis.clients.jedis.JedisPoolConfig poolConfig) Constructs a newJedisConnectionFactoryinstance using the given pool configuration.- Parameters:
poolConfig- pool configuration
-
JedisConnectionFactory
Constructs a newJedisConnectionFactoryinstance using the givenRedisClusterConfigurationapplied to create aJedisCluster.- Parameters:
clusterConfiguration- must not be null.- Since:
- 1.7
-
JedisConnectionFactory
public JedisConnectionFactory(RedisClusterConfiguration clusterConfiguration, JedisClientConfiguration clientConfiguration) Constructs a newJedisConnectionFactoryinstance using the givenRedisClusterConfigurationandJedisClientConfiguration.- Parameters:
clusterConfiguration- must not be null.clientConfiguration- must not be null.- Since:
- 2.0
-
JedisConnectionFactory
public JedisConnectionFactory(RedisClusterConfiguration clusterConfiguration, redis.clients.jedis.JedisPoolConfig poolConfig) Constructs a newJedisConnectionFactoryinstance using the givenRedisClusterConfigurationapplied to create aJedisCluster.- Parameters:
clusterConfiguration- must not be null.- Since:
- 1.7
-
JedisConnectionFactory
Constructs a newJedisConnectionFactoryinstance using the givenJedisPoolConfigapplied toJedisSentinelPool.- Parameters:
sentinelConfiguration- must not be null.- Since:
- 1.4
-
JedisConnectionFactory
public JedisConnectionFactory(RedisSentinelConfiguration sentinelConfiguration, JedisClientConfiguration clientConfiguration) Constructs a newJedisConnectionFactoryinstance using the givenRedisSentinelConfigurationandJedisClientConfiguration.- Parameters:
sentinelConfiguration- must not be null.clientConfiguration- must not be null.- Since:
- 2.0
-
JedisConnectionFactory
public JedisConnectionFactory(RedisSentinelConfiguration sentinelConfiguration, @Nullable redis.clients.jedis.JedisPoolConfig poolConfig) Constructs a newJedisConnectionFactoryinstance using the givenJedisPoolConfigapplied toJedisSentinelPool.- Parameters:
sentinelConfiguration- the sentinel configuration to use.poolConfig- pool configuration. Defaulted to new instance if null.- Since:
- 1.4
-
JedisConnectionFactory
Constructs a newJedisConnectionFactoryinstance using the givenRedisStandaloneConfiguration.- Parameters:
standaloneConfiguration- must not be null.- Since:
- 2.0
-
JedisConnectionFactory
public JedisConnectionFactory(RedisStandaloneConfiguration standaloneConfiguration, JedisClientConfiguration clientConfiguration) Constructs a newJedisConnectionFactoryinstance using the givenRedisStandaloneConfigurationandJedisClientConfiguration.- Parameters:
standaloneConfiguration- must not be null.clientConfiguration- must not be null.- Since:
- 2.0
-
-
Method Details
-
setExecutor
Configures theexecutorused to execute commands asynchronously across the cluster.- Parameters:
executor-executorused to execute commands asynchronously across the cluster.- Since:
- 3.2
-
getHostName
Returns the Redis hostname.- Returns:
- the hostName.
-
setHostName
Deprecated.since 2.0, configure the hostname usingRedisStandaloneConfiguration.Sets the Redis hostname.- Parameters:
hostName- the hostname to set.
-
isUseSsl
public boolean isUseSsl()Returns whether to use SSL.- Returns:
- use of SSL.
- Since:
- 1.8
-
setUseSsl
Deprecated.since 2.0, configure the SSL usage withJedisClientConfiguration.Sets whether to use SSL.- Parameters:
useSsl- true to use SSL.- Throws:
IllegalStateException- ifJedisClientConfigurationis immutable.- Since:
- 1.8
-
getPassword
Returns the password used for authenticating with the Redis server.- Returns:
- password for authentication.
-
setPassword
Deprecated.since 2.0, configure the password usingRedisStandaloneConfiguration,RedisSentinelConfigurationorRedisClusterConfiguration.Sets the password used for authenticating with the Redis server.- Parameters:
password- the password to set.
-
getPort
public int getPort()Returns the port used to connect to the Redis instance.- Returns:
- the Redis port.
-
setPort
Deprecated.since 2.0, configure the port usingRedisStandaloneConfiguration.Sets the port used to connect to the Redis instance.- Parameters:
port- the Redis port.
-
getTimeout
public int getTimeout()Returns the timeout.- Returns:
- the timeout.
-
setTimeout
Deprecated.since 2.0, configure the timeout usingJedisClientConfiguration.Sets the timeout.- Parameters:
timeout- the timeout to set.- Throws:
IllegalStateException- ifJedisClientConfigurationis immutable.
-
getUsePool
public boolean getUsePool()Indicates the use of a connection pool.Applies only to single node Redis. Sentinel and Cluster modes use always connection-pooling regardless of the pooling setting.
- Returns:
- the use of connection pooling.
-
setUsePool
Deprecated.since 2.0, configure pooling usage withJedisClientConfiguration.Turns on or off the use of connection pooling.- Parameters:
usePool- the usePool to set.- Throws:
IllegalStateException- ifJedisClientConfigurationis immutable.IllegalStateException- if configured to use sentinel andusePoolis false as Jedis requires pooling for Redis sentinel use.
-
getPoolConfig
Returns the poolConfig.- Returns:
- the poolConfig
-
setPoolConfig
Deprecated.since 2.0, configureJedisPoolConfigusingJedisClientConfiguration.Sets the pool configuration for this factory.- Parameters:
poolConfig- the poolConfig to set.- Throws:
IllegalStateException- ifJedisClientConfigurationis immutable.
-
getDatabase
public int getDatabase()Returns the index of the database.- Returns:
- the database index.
-
setDatabase
Deprecated.since 2.0, configure the database index usingRedisStandaloneConfigurationorRedisSentinelConfiguration.Sets the index of the database used by this connection factory. Default is 0.- Parameters:
index- database index.
-
getClientName
Returns the client name.- Returns:
- the client name.
- Since:
- 1.8
-
setClientName
Deprecated.since 2.0, configure the client name usingJedisClientConfiguration.Sets the client name used by this connection factory. Defaults to none which does not set a client name.- Parameters:
clientName- the client name.- Throws:
IllegalStateException- ifJedisClientConfigurationis immutable.- Since:
- 1.8
-
getClientConfiguration
- Returns:
- the
JedisClientConfiguration. - Since:
- 2.0
-
getStandaloneConfiguration
- Returns:
- the
RedisStandaloneConfiguration. - Since:
- 2.0
-
getSentinelConfiguration
- Returns:
- the
RedisStandaloneConfiguration, may be null. - Since:
- 2.0
-
getClusterConfiguration
- Returns:
- the
RedisClusterConfiguration, may be null. - Since:
- 2.0
-
getPhase
public int getPhase()- Specified by:
getPhasein interfacePhased- Specified by:
getPhasein interfaceSmartLifecycle
-
setPhase
public void setPhase(int phase) Specify the lifecycle phase for pausing and resuming this executor. The default is0.- Since:
- 3.2
- See Also:
-
isAutoStartup
public boolean isAutoStartup()- Specified by:
isAutoStartupin interfaceSmartLifecycle- Since:
- 3.3
-
setAutoStartup
public void setAutoStartup(boolean autoStartup) Configure if this Lifecycle connection factory should get started automatically by the container at the time that the containing ApplicationContext gets refreshed.This connection factory defaults to early auto-startup during
afterPropertiesSet()and can potentially create Redis connections early on in the lifecycle. SeesetEarlyStartup(boolean)for delaying connection creation to the ApplicationContext refresh if auto-startup is enabled.- Parameters:
autoStartup- true to automaticallystart()the connection factory; false otherwise.- Since:
- 3.3
- See Also:
-
isEarlyStartup
public boolean isEarlyStartup()- Returns:
- whether to
start()the component duringafterPropertiesSet(). - Since:
- 3.3
-
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.This method is related to
auto-startupand can be used to delay Redis client startup until the ApplicationContext refresh. Disabling early startup does not disable auto-startup.- Parameters:
earlyStartup- true to earlystart()the component; false otherwise.- Since:
- 3.3
- See Also:
-
getConvertPipelineAndTxResults
public boolean getConvertPipelineAndTxResults()Specifies if pipelined results should be converted to the expected data type. Iffalse, results ofJedisConnection.closePipeline()andJedisConnection.exec()will be of the type returned by the Jedis driver.- Specified by:
getConvertPipelineAndTxResultsin interfaceRedisConnectionFactory- Returns:
trueto convert pipeline and transaction results;falseotherwise.
-
setConvertPipelineAndTxResults
public void setConvertPipelineAndTxResults(boolean convertPipelineAndTxResults) Specifies if pipelined results should be converted to the expected data type. Iffalse, results ofJedisConnection.closePipeline()andJedisConnection.exec()will be of the type returned by the Jedis driver.- Parameters:
convertPipelineAndTxResults-trueto convert pipeline and transaction results;falseotherwise.
-
isRedisSentinelAware
public boolean isRedisSentinelAware()- Returns:
- true when
RedisSentinelConfigurationis present. - Since:
- 1.4
-
isRedisClusterAware
public boolean isRedisClusterAware()- Returns:
- true when
RedisClusterConfigurationis present. - Since:
- 2.0
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSetin interfaceInitializingBean
-
start
public void start() -
stop
public void stop() -
isRunning
public boolean isRunning() -
createRedisSentinelPool
protected redis.clients.jedis.util.Pool<redis.clients.jedis.Jedis> createRedisSentinelPool(RedisSentinelConfiguration config) CreatesJedisSentinelPool.- Parameters:
config- the actualRedisSentinelConfiguration. Never null.- Returns:
- the
Poolto use. Never null. - Since:
- 1.4
-
createRedisPool
protected redis.clients.jedis.util.Pool<redis.clients.jedis.Jedis> createRedisPool()CreatesJedisPool.- Returns:
- the
Poolto use. Never null. - Since:
- 1.4
-
createTopologyProvider
Template method to create aClusterTopologyProvidergivenJedisCluster. CreatesJedisClusterConnection.JedisClusterTopologyProviderby default.- Parameters:
cluster- theJedisCluster, must not be null.- Returns:
- the
ClusterTopologyProvider. - See Also:
-
createCluster
protected redis.clients.jedis.JedisCluster createCluster(RedisClusterConfiguration clusterConfig, org.apache.commons.pool2.impl.GenericObjectPoolConfig<redis.clients.jedis.Connection> poolConfig) - Parameters:
clusterConfig- must not be null.poolConfig- can be null.- Returns:
- the actual
JedisCluster. - Since:
- 1.7
-
destroy
public void destroy()- Specified by:
destroyin interfaceDisposableBean
-
getConnection
Description copied from interface:RedisConnectionFactoryReturns a suitableconnectionfor interacting with Redis.- Specified by:
getConnectionin interfaceRedisConnectionFactory- Returns:
connectionfor interacting with Redis.
-
fetchJedisConnector
protected redis.clients.jedis.Jedis fetchJedisConnector()Returns a Jedis instance to be used as a Redis connection. The instance can be newly created or retrieved from a pool.- Returns:
- Jedis instance ready for wrapping into a
RedisConnection.
-
postProcessConnection
Post process a newly retrieved connection. Useful for decorating or executing initialization commands on a new connection. This implementation simply returns the connection.- Parameters:
connection- the jedis connection.- Returns:
- processed connection
-
getClusterConnection
Description copied from interface:RedisConnectionFactoryReturns a suitableconnectionfor interacting with Redis Cluster.- Specified by:
getClusterConnectionin interfaceRedisConnectionFactory- Returns:
- a
connectionfor interacting with Redis Cluster.
-
postProcessConnection
Post process a newly retrieved connection. Useful for decorating or executing initialization commands on a new connection. This implementation simply returns the connection.- Parameters:
connection- the jedis connection.- Returns:
- processed connection.
- Since:
- 3.2
-
translateExceptionIfPossible
- Specified by:
translateExceptionIfPossiblein interfacePersistenceExceptionTranslator
-
getSentinelConnection
Description copied from interface:RedisConnectionFactoryReturns a suitableconnectionfor interacting with Redis Sentinel.- Specified by:
getSentinelConnectionin interfaceRedisConnectionFactory- Returns:
- a
connectionfor interacting with Redis Sentinel.
-
JedisClientConfiguration.