Package io.lettuce.core.internal
Class AsyncConnectionProvider<K,T extends AsyncCloseable,F extends CompletionStage<T>>
- java.lang.Object
-
- io.lettuce.core.internal.AsyncConnectionProvider<K,T,F>
-
- Type Parameters:
T
- connection type.K
- connection key type.F
- type of theCompletionStage
handle of the connection progress.
public class AsyncConnectionProvider<K,T extends AsyncCloseable,F extends CompletionStage<T>> extends Object
Non-blocking provider for connection objects. This connection provider is typed with a connection type and connection key type.getConnection(Object)
Connection requests} are synchronized with a sharedsynchronzer object
perConnectionKey
. Multiple threads requesting a connection for the sameConnectionKey
share the same synchronizer and are not required to wait until a previous asynchronous connection is established but participate in existing connection initializations. Shared synchronization leads to a fair synchronization amongst multiple threads waiting to obtain a connection.- Since:
- 5.1
- Author:
- Mark Paluch
-
-
Constructor Summary
Constructors Constructor Description AsyncConnectionProvider(Function<? extends K,? extends F> connectionFactory)
Create a newAsyncConnectionProvider
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CompletableFuture<Void>
close()
Close all connections.void
close(K key)
Close a connection by its connectionkey
.void
forEach(BiConsumer<? super K,? super T> action)
Execute an action for all established and pendingAsyncCloseable
s.void
forEach(Consumer<? super T> action)
Execute an action for all established and pending connections.F
getConnection(K key)
Request a connection for the given the connectionkey
and return aCompletionStage
that is notified about the connection outcome.int
getConnectionCount()
void
register(K key, T connection)
Register a connection identified bykey
.
-
-
-
Constructor Detail
-
AsyncConnectionProvider
public AsyncConnectionProvider(Function<? extends K,? extends F> connectionFactory)
Create a newAsyncConnectionProvider
.- Parameters:
connectionFactory
- must not benull
.
-
-
Method Detail
-
getConnection
public F getConnection(K key)
Request a connection for the given the connectionkey
and return aCompletionStage
that is notified about the connection outcome.- Parameters:
key
- the connectionkey
, must not benull
.- Returns:
-
register
public void register(K key, T connection)
Register a connection identified bykey
. Overwrites existing entries.- Parameters:
key
- the connectionkey
.connection
- the connection object.
-
getConnectionCount
public int getConnectionCount()
- Returns:
- number of established connections.
-
close
public CompletableFuture<Void> close()
Close all connections. Pending connections are closed using future chaining.
-
close
public void close(K key)
Close a connection by its connectionkey
. Pending connections are closed using future chaining.- Parameters:
key
- the connectionkey
, must not benull
.
-
forEach
public void forEach(Consumer<? super T> action)
Execute an action for all established and pending connections.- Parameters:
action
- the action.
-
forEach
public void forEach(BiConsumer<? super K,? super T> action)
Execute an action for all established and pendingAsyncCloseable
s.- Parameters:
action
- the action.
-
-