public abstract class AsyncConnectionPoolSupport extends Object
BoundedAsyncPool. Connection pool creation requires a Supplier that
connects asynchronously to Redis. The pool can allocate either wrapped or direct connections.
- Wrapped instances will return the connection back to the pool when called
StatefulConnection.close()/StatefulConnection.closeAsync(). - Regular connections need to be returned to the pool with
AsyncPool.release(Object)
Lettuce connections are designed to be thread-safe so one connection can be shared amongst multiple threads and Lettuce
connections auto-reconnect by default. Connection pooling with Lettuce can be
required when you're invoking Redis operations in multiple threads and you use
- blocking commands such as
BLPOP. - transactions
BLPOP. command batching.
Example
// application initialization
RedisClusterClient clusterClient = RedisClusterClient.create(RedisURI.create(host, port));
AsyncPool<StatefulRedisConnection<String, String>> pool = AsyncConnectionPoolSupport
.createBoundedObjectPool(() -> clusterClient.connectAsync(), BoundedPoolConfig.create());
// executing work
CompletableFuture<String> pingResponse = pool.acquire().thenCompose(c -> {
return c.async().ping().whenComplete((s, throwable) -> pool.release(c));
});
// terminating
CompletableFuture<Void> poolClose = pool.closeAsync();
// after poolClose completes:
CompletableFuture<Void> closeFuture = clusterClient.shutdown();
- Since:
- 5.1
- Author:
- Mark Paluch
-
Method Summary
Modifier and Type Method Description static <T extends StatefulConnection<?, ?>>
BoundedAsyncPool<T>createBoundedObjectPool(Supplier<CompletionStage<T>> connectionSupplier, BoundedPoolConfig config)Create and initialize asynchronously a newBoundedAsyncPoolusing theSupplier.static <T extends StatefulConnection<?, ?>>
BoundedAsyncPool<T>createBoundedObjectPool(Supplier<CompletionStage<T>> connectionSupplier, BoundedPoolConfig config, boolean wrapConnections)Create and initialize asynchronously a newBoundedAsyncPoolusing theSupplier.static <T extends StatefulConnection<?, ?>>
CompletionStage<BoundedAsyncPool<T>>createBoundedObjectPoolAsync(Supplier<CompletionStage<T>> connectionSupplier, BoundedPoolConfig config)Create and initialize asynchronously a newBoundedAsyncPoolusing theSupplier.static <T extends StatefulConnection<?, ?>>
CompletionStage<BoundedAsyncPool<T>>createBoundedObjectPoolAsync(Supplier<CompletionStage<T>> connectionSupplier, BoundedPoolConfig config, boolean wrapConnections)Create and initialize asynchronously a newBoundedAsyncPoolusing theSupplier.protected static <T extends StatefulConnection<?, ?>>
BoundedAsyncPool<T>doCreatePool(Supplier<CompletionStage<T>> connectionSupplier, BoundedPoolConfig config, boolean wrapConnections)
-
Method Details
-
createBoundedObjectPool
public static <T extends StatefulConnection<?, ?>> BoundedAsyncPool<T> createBoundedObjectPool(Supplier<CompletionStage<T>> connectionSupplier, BoundedPoolConfig config)Create and initialize asynchronously a newBoundedAsyncPoolusing theSupplier. Allocated instances are wrapped and must not be returned withAsyncPool.release(Object).Since Lettuce 6, this method is blocking as it awaits pool initialization (creation of idle connections).Use
createBoundedObjectPoolAsync(Supplier, BoundedPoolConfig)to obtain aCompletionStagefor non-blocking synchronization.- Type Parameters:
T- connection type.- Parameters:
connectionSupplier- must not benull.config- must not benull.- Returns:
- the connection pool.
-
createBoundedObjectPool
public static <T extends StatefulConnection<?, ?>> BoundedAsyncPool<T> createBoundedObjectPool(Supplier<CompletionStage<T>> connectionSupplier, BoundedPoolConfig config, boolean wrapConnections)Create and initialize asynchronously a newBoundedAsyncPoolusing theSupplier.Since Lettuce 6, this method is blocking as it awaits pool initialization (creation of idle connections).Use
createBoundedObjectPoolAsync(Supplier, BoundedPoolConfig, boolean)to obtain aCompletionStagefor non-blocking synchronization.- Type Parameters:
T- connection type.- Parameters:
connectionSupplier- must not benull.config- must not benull.wrapConnections-falseto return direct connections that need to be returned to the pool usingAsyncPool.release(Object).trueto return wrapped connection that are returned to the pool when invokingStatefulConnection.close()/StatefulConnection.closeAsync().- Returns:
- the connection pool.
-
createBoundedObjectPoolAsync
public static <T extends StatefulConnection<?, ?>> CompletionStage<BoundedAsyncPool<T>> createBoundedObjectPoolAsync(Supplier<CompletionStage<T>> connectionSupplier, BoundedPoolConfig config)Create and initialize asynchronously a newBoundedAsyncPoolusing theSupplier. Allocated instances are wrapped and must not be returned withAsyncPool.release(Object).- Type Parameters:
T- connection type.- Parameters:
connectionSupplier- must not benull.config- must not benull.- Returns:
CompletionStageemitting the connection pool upon completion.- Since:
- 5.3.3
-
createBoundedObjectPoolAsync
public static <T extends StatefulConnection<?, ?>> CompletionStage<BoundedAsyncPool<T>> createBoundedObjectPoolAsync(Supplier<CompletionStage<T>> connectionSupplier, BoundedPoolConfig config, boolean wrapConnections)Create and initialize asynchronously a newBoundedAsyncPoolusing theSupplier.- Type Parameters:
T- connection type.- Parameters:
connectionSupplier- must not benull.config- must not benull.wrapConnections-falseto return direct connections that need to be returned to the pool usingAsyncPool.release(Object).trueto return wrapped connection that are returned to the pool when invokingStatefulConnection.close()/StatefulConnection.closeAsync().- Returns:
CompletionStageemitting the connection pool upon completion.- Since:
- 5.3.3
-
doCreatePool
protected static <T extends StatefulConnection<?, ?>> BoundedAsyncPool<T> doCreatePool(Supplier<CompletionStage<T>> connectionSupplier, BoundedPoolConfig config, boolean wrapConnections)
-