Package com.blackrook.redis
Class RedisConnectionPool
- java.lang.Object
-
- com.blackrook.redis.RedisConnectionPool
-
public class RedisConnectionPool extends Object
A connection pool for Redis socket connections. Connections are fair - released connections are added to the end of an "available" queue.- Author:
- Matthew Tropiano
-
-
Constructor Summary
Constructors Constructor Description RedisConnectionPool(int connectionCount, RedisInfo info)Creates a connection pool using a connection to a host.RedisConnectionPool(int connections, String host, int port, String password)Creates a connection pool using a connection to a host.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RedisConnectiongetAvailableConnection()Retrieves an available connection from the pool.RedisConnectiongetAvailableConnection(long waitMillis)Retrieves an available connection from the pool.intgetAvailableConnectionCount()intgetUsedConnectionCount()voidreleaseConnection(RedisConnection connection)Releases a Redis connection back to this pool.
-
-
-
Constructor Detail
-
RedisConnectionPool
public RedisConnectionPool(int connections, String host, int port, String password) throws IOExceptionCreates a connection pool using a connection to a host.- Parameters:
connections- the number of connections to open.host- the host to connect to.port- the port to connect to on the host.password- the Redis DB password.- Throws:
IOException- if a connection can't be made.UnknownHostException- if the server host can't be resolved.
-
RedisConnectionPool
public RedisConnectionPool(int connectionCount, RedisInfo info) throws IOExceptionCreates a connection pool using a connection to a host.- Parameters:
connectionCount- the number of connections to open.info- theRedisInfoobject to use to describe DB information.- Throws:
IOException- if a connection can't be made.UnknownHostException- if the server host can't be resolved.
-
-
Method Detail
-
getAvailableConnection
public RedisConnection getAvailableConnection() throws InterruptedException, IOException
Retrieves an available connection from the pool.- Returns:
- an available connection.
- Throws:
InterruptedException- if an interrupt is thrown by the current thread waiting for an available connection.IOException- if a connection cannot be re-created or re-established.
-
getAvailableConnection
public RedisConnection getAvailableConnection(long waitMillis) throws InterruptedException, TimeoutException, IOException
Retrieves an available connection from the pool.- Parameters:
waitMillis- the amount of time (in milliseconds) to wait for a connection.- Returns:
- an available connection.
- Throws:
InterruptedException- if an interrupt is thrown by the current thread waiting for an available connection.TimeoutException- if the wait lapses and there are no available connections.IOException- if a connection cannot be re-created or re-established.
-
releaseConnection
public void releaseConnection(RedisConnection connection)
Releases a Redis connection back to this pool.- Parameters:
connection- the connection to release.- Throws:
IllegalStateException- if the connection was never maintained by this pool.
-
getAvailableConnectionCount
public int getAvailableConnectionCount()
- Returns:
- the amount of available connections.
-
getUsedConnectionCount
public int getUsedConnectionCount()
- Returns:
- the amount of used connections.
-
-