public class BoundedAsyncPool<T> extends BasePool implements AsyncPool<T>
idle
objects upon construction. The pool is stateful and requires cleanup
once it's no longer in use.
Object pool bounds are maintained on a best-effort basis as bounds are maintained upon object request whereas the actual object creation might finish at a later time. You might see temporarily slight differences in object usage vs. pool count due to asynchronous processing vs. protecting the pool from exceed its bounds.
BoundedPoolConfig
,
AsyncObjectFactory
Modifier and Type | Field and Description |
---|---|
static CompletableFuture<Object> |
COMPLETED_FUTURE |
Constructor and Description |
---|
BoundedAsyncPool(AsyncObjectFactory<T> factory,
BoundedPoolConfig poolConfig)
|
Modifier and Type | Method and Description |
---|---|
CompletableFuture<T> |
acquire()
Acquire an object from this
AsyncPool . |
void |
clear()
Clear the pool.
|
CompletableFuture<Void> |
clearAsync()
Clear the pool.
|
void |
close() |
CompletableFuture<Void> |
closeAsync()
Requests to close this object and releases any system resources associated with it.
|
static <T> CompletionStage<BoundedAsyncPool<T>> |
create(AsyncObjectFactory<T> factory,
BoundedPoolConfig poolConfig)
Create and initialize
BoundedAsyncPool asynchronously. |
int |
getCreationInProgress() |
int |
getIdle() |
int |
getMaxIdle()
Returns the cap on the number of "idle" instances in the pool.
|
int |
getMaxTotal()
Returns the maximum number of objects that can be allocated by the pool (checked out to clients, or idle awaiting
checkout) at a given time.
|
int |
getMinIdle()
Returns the target for the minimum number of idle objects to maintain in the pool.
|
int |
getObjectCount() |
CompletableFuture<Void> |
release(T object)
Release an object back to this
AsyncPool . |
isTestOnAcquire, isTestOnCreate, isTestOnRelease
public static final CompletableFuture<Object> COMPLETED_FUTURE
public BoundedAsyncPool(AsyncObjectFactory<T> factory, BoundedPoolConfig poolConfig)
BoundedAsyncPool
given BasePoolConfig
and AsyncObjectFactory
. The factory creates
idle objects upon construction and requires termination
once it's no longer in use.
Please note that pre-initialization cannot be awaited when using this constructor. Please use
create(AsyncObjectFactory, BoundedPoolConfig)
instead.
factory
- must not be null
.poolConfig
- must not be null
.public static <T> CompletionStage<BoundedAsyncPool<T>> create(AsyncObjectFactory<T> factory, BoundedPoolConfig poolConfig)
BoundedAsyncPool
asynchronously.T
- object type that is managed by the pool.factory
- must not be null
.poolConfig
- must not be null
.CompletionStage
that completes with the BoundedAsyncPool
when created and pre-initialized
successfully. Completes exceptionally if the pool initialization failed.public CompletableFuture<T> acquire()
AsyncPool
AsyncPool
. The returned CompletableFuture
is notified once the acquire is
successful and failed otherwise. Behavior upon acquiring objects from an exhausted pool depends on the actual pool
implementation whether requests are rejected immediately (exceptional completion with
NoSuchElementException
) or delayed after exceeding a particular timeout (
TimeoutException
).
It's required that an acquired object is always released to the pool again once the object is no longer in
use..public CompletableFuture<Void> release(T object)
AsyncPool
AsyncPool
. The returned CompletableFuture
is notified once the release is
successful and failed otherwise. When failed the object will automatically disposed.public void clear()
AsyncPool
public CompletableFuture<Void> clearAsync()
AsyncPool
clearAsync
in interface AsyncPool<T>
public void close()
public CompletableFuture<Void> closeAsync()
AsyncCloseable
Calls to this method return a CompletableFuture
that is notified with the outcome of the close request.
closeAsync
in interface AsyncCloseable
closeAsync
in interface AsyncPool<T>
public int getMaxTotal()
Integer#MAX_VALUE
.BoundedPoolConfig.getMaxTotal()
public int getMaxIdle()
maxIdle
is set too low on heavily loaded
systems it is possible you will see objects being destroyed and almost immediately new objects being created. This is a
result of the active threads momentarily returning objects faster than they are requesting them, causing the number of
idle objects to rise above maxIdle. The best value for maxIdle for heavily loaded system will vary but the default is a
good starting point.Integer#MAX_VALUE
.BoundedPoolConfig.getMaxIdle()
public int getMinIdle()
If the configured value of minIdle is greater than the configured value for maxIdle
then the value of
maxIdle
will be used instead.
BoundedPoolConfig.getMinIdle()
public int getIdle()
public int getObjectCount()
public int getCreationInProgress()
Copyright © 2023 lettuce.io. All rights reserved.