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
by default
. You can Lifecycle.stop()
and restart
this connection factory if needed.
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
-
Field Summary
Fields inherited from interface org.springframework.context.SmartLifecycle
DEFAULT_PHASE
-
Constructor Summary
ConstructorDescriptionConstructs a newJedisConnectionFactory
instance with default settings (default connection pooling).JedisConnectionFactory
(RedisClusterConfiguration clusterConfiguration) Constructs a newJedisConnectionFactory
instance using the givenRedisClusterConfiguration
applied to create aJedisCluster
.JedisConnectionFactory
(RedisClusterConfiguration clusterConfiguration, JedisClientConfiguration clientConfiguration) Constructs a newJedisConnectionFactory
instance using the givenRedisClusterConfiguration
andJedisClientConfiguration
.JedisConnectionFactory
(RedisClusterConfiguration clusterConfiguration, redis.clients.jedis.JedisPoolConfig poolConfig) Constructs a newJedisConnectionFactory
instance using the givenRedisClusterConfiguration
applied to create aJedisCluster
.JedisConnectionFactory
(RedisSentinelConfiguration sentinelConfiguration) Constructs a newJedisConnectionFactory
instance using the givenJedisPoolConfig
applied toJedisSentinelPool
.JedisConnectionFactory
(RedisSentinelConfiguration sentinelConfiguration, JedisClientConfiguration clientConfiguration) Constructs a newJedisConnectionFactory
instance using the givenRedisSentinelConfiguration
andJedisClientConfiguration
.JedisConnectionFactory
(RedisSentinelConfiguration sentinelConfiguration, redis.clients.jedis.JedisPoolConfig poolConfig) Constructs a newJedisConnectionFactory
instance using the givenJedisPoolConfig
applied toJedisSentinelPool
.JedisConnectionFactory
(RedisStandaloneConfiguration standaloneConfiguration) Constructs a newJedisConnectionFactory
instance using the givenRedisStandaloneConfiguration
.JedisConnectionFactory
(RedisStandaloneConfiguration standaloneConfiguration, JedisClientConfiguration clientConfiguration) 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.Returns a suitableconnection
for interacting with Redis Cluster.Returns a suitableconnection
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.int
getPhase()
<T> org.apache.commons.pool2.impl.GenericObjectPoolConfig<T>
Returns the poolConfig.int
getPort()
Returns the port used to connect to the Redis instance.Returns a suitableconnection
for interacting with Redis Sentinel.int
Returns the timeout.boolean
Indicates the use of a connection pool.boolean
boolean
boolean
boolean
isUseSsl()
Returns whether to use SSL.protected JedisClusterConnection
postProcessConnection
(JedisClusterConnection connection) Post process a newly retrieved connection.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
setExecutor
(AsyncTaskExecutor executor) Configures theexecutor
used to execute commands asynchronously across the cluster.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
setPhase
(int phase) Specify the lifecycle phase for pausing and resuming this executor.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
.void
start()
void
stop()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.springframework.context.SmartLifecycle
isAutoStartup, stop
-
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 givenRedisClusterConfiguration
applied to create aJedisCluster
.- Parameters:
clusterConfiguration
- must not be null.- Since:
- 1.7
-
JedisConnectionFactory
public JedisConnectionFactory(RedisClusterConfiguration clusterConfiguration, JedisClientConfiguration clientConfiguration) Constructs a newJedisConnectionFactory
instance using the givenRedisClusterConfiguration
andJedisClientConfiguration
.- 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 newJedisConnectionFactory
instance using the givenRedisClusterConfiguration
applied to create aJedisCluster
.- Parameters:
clusterConfiguration
- must not be null.- Since:
- 1.7
-
JedisConnectionFactory
Constructs a newJedisConnectionFactory
instance using the givenJedisPoolConfig
applied toJedisSentinelPool
.- Parameters:
sentinelConfiguration
- must not be null.- Since:
- 1.4
-
JedisConnectionFactory
public JedisConnectionFactory(RedisSentinelConfiguration sentinelConfiguration, JedisClientConfiguration clientConfiguration) Constructs a newJedisConnectionFactory
instance using the givenRedisSentinelConfiguration
andJedisClientConfiguration
.- 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 newJedisConnectionFactory
instance using the givenJedisPoolConfig
applied toJedisSentinelPool
.- Parameters:
sentinelConfiguration
- the sentinel configuration to use.poolConfig
- pool configuration. Defaulted to new instance if null.- Since:
- 1.4
-
JedisConnectionFactory
Constructs a newJedisConnectionFactory
instance using the givenRedisStandaloneConfiguration
.- Parameters:
standaloneConfiguration
- must not be null.- Since:
- 2.0
-
JedisConnectionFactory
public JedisConnectionFactory(RedisStandaloneConfiguration standaloneConfiguration, JedisClientConfiguration clientConfiguration) Constructs a newJedisConnectionFactory
instance using the givenRedisStandaloneConfiguration
andJedisClientConfiguration
.- Parameters:
standaloneConfiguration
- must not be null.clientConfiguration
- must not be null.- Since:
- 2.0
-
-
Method Details
-
setExecutor
Configures theexecutor
used to execute commands asynchronously across the cluster.- Parameters:
executor
-executor
used 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
- 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. Iffalse
, results ofJedisConnection.closePipeline()
andJedisConnection.exec()
will be of the type returned by the Jedis driver.- Specified by:
getConvertPipelineAndTxResults
in interfaceRedisConnectionFactory
- Returns:
true
to convert pipeline and transaction results;false
otherwise.
-
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
-true
to convert pipeline and transaction results;false
otherwise.
-
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
-
afterPropertiesSet
public void afterPropertiesSet()- Specified by:
afterPropertiesSet
in interfaceInitializingBean
-
start
public void start() -
stop
public void stop() -
getPhase
public int getPhase()- Specified by:
getPhase
in interfacePhased
- Specified by:
getPhase
in 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:
-
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
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
Returns a suitableconnection
for interacting with Redis.- Specified by:
getConnection
in interfaceRedisConnectionFactory
- Returns:
connection
for 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:RedisConnectionFactory
Returns a suitableconnection
for interacting with Redis Cluster.- Specified by:
getClusterConnection
in interfaceRedisConnectionFactory
- Returns:
- a
connection
for 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:
translateExceptionIfPossible
in interfacePersistenceExceptionTranslator
-
getSentinelConnection
Description copied from interface:RedisConnectionFactory
Returns a suitableconnection
for interacting with Redis Sentinel.- Specified by:
getSentinelConnection
in interfaceRedisConnectionFactory
- Returns:
- a
connection
for interacting with Redis Sentinel.
-
JedisClientConfiguration
.