Class JedisConnectionFactory
java.lang.Object
org.springframework.data.redis.connection.jedis.JedisConnectionFactory
- All Implemented Interfaces:
DisposableBean
,InitializingBean
,PersistenceExceptionTranslator
,RedisConnectionFactory
public class JedisConnectionFactory
extends Object
implements InitializingBean, DisposableBean, RedisConnectionFactory
Connection factory creating Jedis based connections.
JedisConnectionFactory
should be configured using an environmental configuration and the
client configuration
. Jedis supports the following environmental configurations:
This connection factory must be initialized
prior to obtaining
connections
.
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:
-
JedisClientConfiguration
Jedis
-
Constructor Summary
ConstructorDescriptionConstructs a newJedisConnectionFactory
instance with default settings (default connection pooling).JedisConnectionFactory
(RedisClusterConfiguration clusterConfig) Constructs a newJedisConnectionFactory
instance using the givenRedisClusterConfiguration
applied to create aJedisCluster
.JedisConnectionFactory
(RedisClusterConfiguration clusterConfig, JedisClientConfiguration clientConfig) Constructs a newJedisConnectionFactory
instance using the givenRedisClusterConfiguration
andJedisClientConfiguration
.JedisConnectionFactory
(RedisClusterConfiguration clusterConfig, redis.clients.jedis.JedisPoolConfig poolConfig) Constructs a newJedisConnectionFactory
instance using the givenRedisClusterConfiguration
applied to create aJedisCluster
.JedisConnectionFactory
(RedisSentinelConfiguration sentinelConfig) Constructs a newJedisConnectionFactory
instance using the givenJedisPoolConfig
applied toJedisSentinelPool
.JedisConnectionFactory
(RedisSentinelConfiguration sentinelConfig, JedisClientConfiguration clientConfig) Constructs a newJedisConnectionFactory
instance using the givenRedisSentinelConfiguration
andJedisClientConfiguration
.JedisConnectionFactory
(RedisSentinelConfiguration sentinelConfig, redis.clients.jedis.JedisPoolConfig poolConfig) Constructs a newJedisConnectionFactory
instance using the givenJedisPoolConfig
applied toJedisSentinelPool
.JedisConnectionFactory
(RedisStandaloneConfiguration standaloneConfig) Constructs a newJedisConnectionFactory
instance using the givenRedisStandaloneConfiguration
.JedisConnectionFactory
(RedisStandaloneConfiguration standaloneConfig, JedisClientConfiguration clientConfig) Constructs a newJedisConnectionFactory
instance using the givenRedisStandaloneConfiguration
andJedisClientConfiguration
.JedisConnectionFactory
(redis.clients.jedis.JedisPoolConfig poolConfig) Constructs a newJedisConnectionFactory
instance using the given pool configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoid
protected redis.clients.jedis.JedisCluster
createCluster
(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 ClusterTopologyProvider
createTopologyProvider
(redis.clients.jedis.JedisCluster cluster) Template method to create aClusterTopologyProvider
givenJedisCluster
.void
destroy()
protected redis.clients.jedis.Jedis
Returns a Jedis instance to be used as a Redis connection.Returns the client name.Provides a suitable connection for interacting with Redis Cluster.Provides a suitable connection for interacting with Redis.boolean
Specifies if pipelined results should be converted to the expected data type.int
Returns the index of the database.Returns the Redis hostname.Returns the password used for authenticating with the Redis server.<T> org.apache.commons.pool2.impl.GenericObjectPoolConfig<T>
Returns the poolConfig.int
getPort()
Returns the port used to connect to the Redis instance.Provides a suitable connection for interacting with Redis Sentinel.int
Returns the timeout.boolean
Indicates the use of a connection pool.boolean
boolean
boolean
isUseSsl()
Returns whether to use SSL.protected JedisConnection
postProcessConnection
(JedisConnection connection) Post process a newly retrieved connection.void
setClientName
(String clientName) Deprecated.void
setConvertPipelineAndTxResults
(boolean convertPipelineAndTxResults) Specifies if pipelined results should be converted to the expected data type.void
setDatabase
(int index) Deprecated.since 2.0, configure the database index usingRedisStandaloneConfiguration
orRedisSentinelConfiguration
.void
setHostName
(String hostName) Deprecated.since 2.0, configure the hostname usingRedisStandaloneConfiguration
.void
setPassword
(String password) Deprecated.since 2.0, configure the password usingRedisStandaloneConfiguration
,RedisSentinelConfiguration
orRedisClusterConfiguration
.void
setPoolConfig
(redis.clients.jedis.JedisPoolConfig poolConfig) Deprecated.since 2.0, configureJedisPoolConfig
usingJedisClientConfiguration
.void
setPort
(int port) Deprecated.since 2.0, configure the port usingRedisStandaloneConfiguration
.void
setTimeout
(int timeout) Deprecated.since 2.0, configure the timeout usingJedisClientConfiguration
.void
setUsePool
(boolean usePool) Deprecated.since 2.0, configure pooling usage withJedisClientConfiguration
.void
setUseSsl
(boolean useSsl) Deprecated.since 2.0, configure the SSL usage withJedisClientConfiguration
.
-
Constructor Details
-
JedisConnectionFactory
public JedisConnectionFactory()Constructs a newJedisConnectionFactory
instance with default settings (default connection pooling). -
JedisConnectionFactory
public JedisConnectionFactory(redis.clients.jedis.JedisPoolConfig poolConfig) Constructs a newJedisConnectionFactory
instance using the given pool configuration.- Parameters:
poolConfig
- pool configuration
-
JedisConnectionFactory
Constructs a newJedisConnectionFactory
instance using the givenJedisPoolConfig
applied toJedisSentinelPool
.- Parameters:
sentinelConfig
- must not be null.- Since:
- 1.4
-
JedisConnectionFactory
public JedisConnectionFactory(RedisSentinelConfiguration sentinelConfig, @Nullable redis.clients.jedis.JedisPoolConfig poolConfig) Constructs a newJedisConnectionFactory
instance using the givenJedisPoolConfig
applied toJedisSentinelPool
.- Parameters:
sentinelConfig
- the sentinel configuration to use.poolConfig
- pool configuration. Defaulted to new instance if null.- Since:
- 1.4
-
JedisConnectionFactory
Constructs a newJedisConnectionFactory
instance using the givenRedisClusterConfiguration
applied to create aJedisCluster
.- Parameters:
clusterConfig
- must not be null.- Since:
- 1.7
-
JedisConnectionFactory
public JedisConnectionFactory(RedisClusterConfiguration clusterConfig, redis.clients.jedis.JedisPoolConfig poolConfig) Constructs a newJedisConnectionFactory
instance using the givenRedisClusterConfiguration
applied to create aJedisCluster
.- Parameters:
clusterConfig
- must not be null.- Since:
- 1.7
-
JedisConnectionFactory
Constructs a newJedisConnectionFactory
instance using the givenRedisStandaloneConfiguration
.- Parameters:
standaloneConfig
- must not be null.- Since:
- 2.0
-
JedisConnectionFactory
public JedisConnectionFactory(RedisStandaloneConfiguration standaloneConfig, JedisClientConfiguration clientConfig) Constructs a newJedisConnectionFactory
instance using the givenRedisStandaloneConfiguration
andJedisClientConfiguration
.- Parameters:
standaloneConfig
- must not be null.clientConfig
- must not be null.- Since:
- 2.0
-
JedisConnectionFactory
public JedisConnectionFactory(RedisSentinelConfiguration sentinelConfig, JedisClientConfiguration clientConfig) Constructs a newJedisConnectionFactory
instance using the givenRedisSentinelConfiguration
andJedisClientConfiguration
.- Parameters:
sentinelConfig
- must not be null.clientConfig
- must not be null.- Since:
- 2.0
-
JedisConnectionFactory
public JedisConnectionFactory(RedisClusterConfiguration clusterConfig, JedisClientConfiguration clientConfig) Constructs a newJedisConnectionFactory
instance using the givenRedisClusterConfiguration
andJedisClientConfiguration
.- Parameters:
clusterConfig
- must not be null.clientConfig
- must not be null.- Since:
- 2.0
-
-
Method Details
-
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
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSet
in interfaceInitializingBean
-
createRedisSentinelPool
protected redis.clients.jedis.util.Pool<redis.clients.jedis.Jedis> createRedisSentinelPool(RedisSentinelConfiguration config) CreatesJedisSentinelPool
.- Parameters:
config
- the actualRedisSentinelConfiguration
. Never null.- Returns:
- the
Pool
to use. Never null. - Since:
- 1.4
-
createRedisPool
protected redis.clients.jedis.util.Pool<redis.clients.jedis.Jedis> createRedisPool()CreatesJedisPool
.- Returns:
- the
Pool
to use. Never null. - Since:
- 1.4
-
createTopologyProvider
Template method to create aClusterTopologyProvider
givenJedisCluster
. CreatesJedisClusterConnection.JedisClusterTopologyProvider
by 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:
destroy
in interfaceDisposableBean
-
getConnection
Description copied from interface:RedisConnectionFactory
Provides a suitable connection for interacting with Redis.- Specified by:
getConnection
in interfaceRedisConnectionFactory
- Returns:
- connection for interacting with Redis.
-
getClusterConnection
Description copied from interface:RedisConnectionFactory
Provides a suitable connection for interacting with Redis Cluster.- Specified by:
getClusterConnection
in interfaceRedisConnectionFactory
- Returns:
-
translateExceptionIfPossible
- Specified by:
translateExceptionIfPossible
in interfacePersistenceExceptionTranslator
-
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
- ifJedisClientConfiguration
is 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
,RedisSentinelConfiguration
orRedisClusterConfiguration
.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
- ifJedisClientConfiguration
is 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
- ifJedisClientConfiguration
is immutable.IllegalStateException
- if configured to use sentinel andusePool
is false as Jedis requires pooling for Redis sentinel use.
-
getPoolConfig
Returns the poolConfig.- Returns:
- the poolConfig
-
setPoolConfig
Deprecated.since 2.0, configureJedisPoolConfig
usingJedisClientConfiguration
.Sets the pool configuration for this factory.- Parameters:
poolConfig
- the poolConfig to set.- Throws:
IllegalStateException
- ifJedisClientConfiguration
is immutable.
-
getDatabase
public int getDatabase()Returns the index of the database.- Returns:
- the database index.
-
setDatabase
Deprecated.since 2.0, configure the database index usingRedisStandaloneConfiguration
orRedisSentinelConfiguration
.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
- ifJedisClientConfiguration
is 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
-
getConvertPipelineAndTxResults
public boolean getConvertPipelineAndTxResults()Specifies if pipelined results should be converted to the expected data type. If false, results ofJedisConnection.closePipeline()
andJedisConnection.exec()
will be of the type returned by the Jedis driver.- Specified by:
getConvertPipelineAndTxResults
in interfaceRedisConnectionFactory
- Returns:
- Whether or not to convert pipeline and tx results.
-
setConvertPipelineAndTxResults
public void setConvertPipelineAndTxResults(boolean convertPipelineAndTxResults) Specifies if pipelined results should be converted to the expected data type. If false, results ofJedisConnection.closePipeline()
andJedisConnection.exec()
will be of the type returned by the Jedis driver.- Parameters:
convertPipelineAndTxResults
- Whether or not to convert pipeline and tx results.
-
isRedisSentinelAware
public boolean isRedisSentinelAware()- Returns:
- true when
RedisSentinelConfiguration
is present. - Since:
- 1.4
-
isRedisClusterAware
public boolean isRedisClusterAware()- Returns:
- true when
RedisClusterConfiguration
is present. - Since:
- 2.0
-
getSentinelConnection
Description copied from interface:RedisConnectionFactory
Provides a suitable connection for interacting with Redis Sentinel.- Specified by:
getSentinelConnection
in interfaceRedisConnectionFactory
- Returns:
- connection for interacting with Redis Sentinel.
-
JedisClientConfiguration
.