org.springframework.data.redis.connection.lettuce
Class LettuceConnectionFactory

java.lang.Object
  extended by org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory
All Implemented Interfaces:
DisposableBean, InitializingBean, PersistenceExceptionTranslator, RedisConnectionFactory

public class LettuceConnectionFactory
extends Object
implements InitializingBean, DisposableBean, RedisConnectionFactory

Connection factory creating Lettuce-based connections.

This factory creates a new LettuceConnection on each call to getConnection(). Multiple LettuceConnections share a single thread-safe native connection by default.

The shared native connection is never closed by LettuceConnection, therefore it is not validated by default on getConnection(). Use setValidateConnection(boolean) to change this behavior if necessary.


Constructor Summary
LettuceConnectionFactory()
          Constructs a new LettuceConnectionFactory instance with default settings.
LettuceConnectionFactory(String host, int port)
          Constructs a new LettuceConnectionFactory instance with default settings.
 
Method Summary
 void afterPropertiesSet()
           
 void destroy()
           
 RedisConnection getConnection()
          Provides a suitable connection for interacting with Redis.
 int getDatabase()
          Returns the index of the database.
 String getHostName()
          Returns the current host.
protected  com.lambdaworks.redis.RedisAsyncConnection<byte[],byte[]> getNativeConnection()
           
 int getPort()
          Returns the current port.
 boolean getShareNativeConnection()
          Indicates if multiple LettuceConnections should share a single native connection.
 long getTimeout()
          Returns the connection timeout (in milliseconds).
 boolean getValidateConnection()
          Indicates if validation of the native Lettuce connection is enabled
 void initConnection()
           
 void resetConnection()
          Reset the underlying shared Connection, to be reinitialized on next access.
 void setDatabase(int index)
          Sets the index of the database used by this connection factory.
 void setHostName(String host)
          Sets the host.
 void setPort(int port)
          Sets the port.
 void setShareNativeConnection(boolean shareNativeConnection)
          Enables multiple LettuceConnections to share a single native connection.
 void setTimeout(long timeout)
          Sets the connection timeout (in milliseconds).
 void setValidateConnection(boolean validateConnection)
          Enables validation of the shared native Lettuce connection on calls to getConnection().
 DataAccessException translateExceptionIfPossible(RuntimeException ex)
           
 void validateConnection()
          Validate the shared Connection and reinitialize if invalid
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LettuceConnectionFactory

public LettuceConnectionFactory()
Constructs a new LettuceConnectionFactory instance with default settings.


LettuceConnectionFactory

public LettuceConnectionFactory(String host,
                                int port)
Constructs a new LettuceConnectionFactory instance with default settings.

Method Detail

afterPropertiesSet

public void afterPropertiesSet()
Specified by:
afterPropertiesSet in interface InitializingBean

destroy

public void destroy()
Specified by:
destroy in interface DisposableBean

getConnection

public RedisConnection getConnection()
Description copied from interface: RedisConnectionFactory
Provides a suitable connection for interacting with Redis.

Specified by:
getConnection in interface RedisConnectionFactory
Returns:
connection for interacting with Redis.

initConnection

public void initConnection()

resetConnection

public void resetConnection()
Reset the underlying shared Connection, to be reinitialized on next access.


validateConnection

public void validateConnection()
Validate the shared Connection and reinitialize if invalid


translateExceptionIfPossible

public DataAccessException translateExceptionIfPossible(RuntimeException ex)
Specified by:
translateExceptionIfPossible in interface PersistenceExceptionTranslator

getHostName

public String getHostName()
Returns the current host.

Returns:
the host

setHostName

public void setHostName(String host)
Sets the host.

Parameters:
host - the host to set

getPort

public int getPort()
Returns the current port.

Returns:
the port

setPort

public void setPort(int port)
Sets the port.

Parameters:
port - the port to set

getTimeout

public long getTimeout()
Returns the connection timeout (in milliseconds).

Returns:
connection timeout

setTimeout

public void setTimeout(long timeout)
Sets the connection timeout (in milliseconds).

Parameters:
timeout - connection timeout

getValidateConnection

public boolean getValidateConnection()
Indicates if validation of the native Lettuce connection is enabled

Returns:
connection validation enabled

setValidateConnection

public void setValidateConnection(boolean validateConnection)
Enables validation of the shared native Lettuce connection on calls to getConnection(). A new connection will be created and used if validation fails.

Lettuce will automatically reconnect until close is called, which should never happen through LettuceConnection if a shared native connection is used, therefore the default is false.

Setting this to true will result in a round-trip call to the server on each new connection, so this setting should only be used if connection sharing is enabled and there is code that is actively closing the native Lettuce connection.

Parameters:
validateConnection - enable connection validation

getShareNativeConnection

public boolean getShareNativeConnection()
Indicates if multiple LettuceConnections should share a single native connection.

Returns:
native connection shared

setShareNativeConnection

public void setShareNativeConnection(boolean shareNativeConnection)
Enables multiple LettuceConnections to share a single native connection. If set to false, every operation on LettuceConnection will open and close a socket.

Parameters:
shareNativeConnection - enable connection sharing

getDatabase

public int getDatabase()
Returns the index of the database.

Returns:
Returns the database index

setDatabase

public void setDatabase(int index)
Sets the index of the database used by this connection factory. Default is 0.

Parameters:
index - database index

getNativeConnection

protected com.lambdaworks.redis.RedisAsyncConnection<byte[],byte[]> getNativeConnection()