T
- The type of objects in the pool.X
- The type of exception that operations on objects in the pool can throw.public final class Pool<T extends PoolableObject<X>,X extends Exception> extends Object
Instances of this class are thread-safe.
Constructor and Description |
---|
Pool(PoolConfig config,
PoolableObjectFactory<T,X> factory,
PoolLogger logger)
Creates a new pool.
|
Modifier and Type | Method and Description |
---|---|
T |
acquire()
Acquires an object.
|
T |
acquire(Duration maxWaitTime)
Acquires an object.
|
<E extends Exception> |
acquire(Duration maxWaitTime,
Supplier<E> errorSupplier)
Acquires an object.
|
T |
acquire(long maxWaitTime,
TimeUnit timeUnit)
Acquires an object.
|
<E extends Exception> |
acquire(long maxWaitTime,
TimeUnit timeUnit,
Supplier<E> errorSupplier)
Acquires an object.
|
<E extends Exception> |
acquire(Supplier<E> errorSupplier)
Acquires an object.
|
Optional<T> |
acquireNow()
Acquires an object if possible.
|
T |
acquireOrCreate()
Acquires an object if possible.
|
void |
forAllIdleObjects(PoolableObjectConsumer<T,X> action)
Runs an operation on all idle objects.
|
boolean |
isActive()
Returns whether or not this pool is active.
|
void |
shutdown()
Shuts down this pool.
|
static <T extends PoolableObject<None>> |
throwingNone(PoolConfig config,
Supplier<T> supplier,
PoolLogger logger)
Creates a new pool that throws no exceptions.
|
public Pool(PoolConfig config, PoolableObjectFactory<T,X> factory, PoolLogger logger) throws X extends Exception
config
- The configuration to use.factory
- The object factory to use.logger
- The logger to use to log events triggered by the pool or pooled objects.NullPointerException
- If the given configuration, factory or logger is null
.X
- If the given config requests a positive initial size, and one or more objects could not be created.X extends Exception
public T acquire() throws X extends Exception, InterruptedException
X
- If an error occurs while acquiring an object.NoSuchElementException
- If the maximum wait time expires before an object could be acquired.InterruptedException
- If the current thread is interrupted while acquiring an object.IllegalStateException
- If this pool has shut down.X extends Exception
public T acquire(Duration maxWaitTime) throws X extends Exception, InterruptedException
maxWaitTime
- The maximum wait time.
If null
or negative, this method will block until an object is available.X
- If an error occurs while acquiring an object.NoSuchElementException
- If the maximum wait time expires before an object could be acquired.InterruptedException
- If the current thread is interrupted while acquiring an object.IllegalStateException
- If this pool has shut down.X extends Exception
public T acquire(long maxWaitTime, TimeUnit timeUnit) throws X extends Exception, InterruptedException
maxWaitTime
- The maximum wait time. If negative, this method will block until an object is available.timeUnit
- The time unit for the maximum wait time.NullPointerException
- If the given time unit is null
.X
- If an error occurs while acquiring an object.NoSuchElementException
- If the maximum wait time expires before an object could be acquired.InterruptedException
- If the current thread is interrupted while acquiring an object.IllegalStateException
- If this pool has shut down.X extends Exception
public <E extends Exception> T acquire(Supplier<E> errorSupplier) throws X extends Exception, E extends Exception, InterruptedException
E
- The type of exception to throw if the maximum wait time expires.errorSupplier
- A supplier for the exception to throw if the maximum wait time expires.NullPointerException
- If the given supplier is null
and the maximum wait time expires.X
- If an error occurs while acquiring an object.E
- If the maximum wait time expires before an object could be acquired.InterruptedException
- If the current thread is interrupted while acquiring an object.IllegalStateException
- If this pool has shut down.X extends Exception
public <E extends Exception> T acquire(Duration maxWaitTime, Supplier<E> errorSupplier) throws X extends Exception, E extends Exception, InterruptedException
E
- The type of exception to throw if the maximum wait time expires.maxWaitTime
- The maximum wait time.
If null
or negative, this method will block until an object is available.errorSupplier
- A supplier for the exception to throw if the maximum wait time expires.NullPointerException
- If the given supplier is null
and the maximum wait time expires.X
- If an error occurs while acquiring an object.E
- If the maximum wait time expires before an object could be acquired.InterruptedException
- If the current thread is interrupted while acquiring an object.IllegalStateException
- If this pool has shut down.X extends Exception
public <E extends Exception> T acquire(long maxWaitTime, TimeUnit timeUnit, Supplier<E> errorSupplier) throws X extends Exception, E extends Exception, InterruptedException
E
- The type of exception to throw if the maximum wait time expires.maxWaitTime
- The maximum wait time. If negative, this method will block until an object is available.timeUnit
- The time unit for the maximum wait time.errorSupplier
- A supplier for the exception to throw if the maximum wait time expires.NullPointerException
- If the given time unit is null
.
or if the given supplier is null
and the maximum wait time expires.X
- If an error occurs while acquiring an object.E
- If the maximum wait time expires before an object could be acquired.InterruptedException
- If the current thread is interrupted while acquiring an object.IllegalStateException
- If this pool has shut down.X extends Exception
public Optional<T> acquireNow() throws X extends Exception
Optional
describing the acquired object, or Optional.empty()
if no object was available.X
- If an error occurs while acquiring an object.IllegalStateException
- If this pool has shut down.X extends Exception
public T acquireOrCreate() throws X extends Exception
X
- If an error occurs while acquiring an object.IllegalStateException
- If this pool has shut down.X extends Exception
public void forAllIdleObjects(PoolableObjectConsumer<T,X> action) throws X extends Exception
Any object that is no longer valid or has been idle too long will be removed from the pool before running the operation on idle objects. As a result, all arguments to the operation will be valid.
action
- The operation to run.X
- If an error occurs while running the operation on any of the idle objects.NullPointerException
- If the given operation is null
.X extends Exception
public boolean isActive()
true
if this pool is active, or false
if it has shut down.public void shutdown() throws X extends Exception
public static <T extends PoolableObject<None>> Pool<T,None> throwingNone(PoolConfig config, Supplier<T> supplier, PoolLogger logger)
T
- The type of objects in the pool.config
- The configuration to use.supplier
- A supplier to serve as object factory.logger
- The logger to use to log events triggered by the pool or pooled objects.NullPointerException
- If the given configuration, supplier or logger is null
.Copyright © 2022. All rights reserved.