T
- the type of the resource@ThreadSafe public abstract class DynamicResourcePool<T> extends Object implements Pool<T>
Modifier and Type | Class and Description |
---|---|
static class |
DynamicResourcePool.Options
Options to initialize a Dynamic resource pool.
|
protected class |
DynamicResourcePool.ResourceInternal<R>
A wrapper on the resource to include the last time at which it was used.
|
Modifier and Type | Field and Description |
---|---|
protected java.time.Clock |
mClock |
Constructor and Description |
---|
DynamicResourcePool(DynamicResourcePool.Options options)
Creates a dynamic pool instance.
|
Modifier and Type | Method and Description |
---|---|
T |
acquire()
Acquires a resource of type {code T} from the pool.
|
T |
acquire(long time,
TimeUnit unit)
Acquires a resource of type {code T} from the pool.
|
void |
close()
Closes the pool and clears all the resources.
|
protected abstract void |
closeResource(T resource)
Closes the resource.
|
protected abstract T |
createNewResource()
Creates a new resource.
|
protected abstract boolean |
isHealthy(T resource)
Checks whether a resource is healthy or not.
|
void |
release(T resource)
Releases the resource to the pool.
|
protected abstract boolean |
shouldGc(DynamicResourcePool.ResourceInternal<T> resourceInternal) |
int |
size() |
public DynamicResourcePool(DynamicResourcePool.Options options)
options
- the optionspublic T acquire() throws IOException
acquire
in interface Pool<T>
IOException
public T acquire(long time, TimeUnit unit) throws TimeoutException, IOException
acquire()
, but it will time out if an object cannot be
acquired before the specified amount of time.acquire
in interface Pool<T>
time
- an amount of time to waitunit
- the unit to use for timeTimeoutException
- if it fails to acquire because of time outIOException
- if the thread is interrupted while acquiring the resourcepublic void release(T resource)
release(Object)
and acquire()
must be
paired. Do not release the resource acquired multiple times. The behavior is undefined if
that happens.public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
protected abstract boolean shouldGc(DynamicResourcePool.ResourceInternal<T> resourceInternal)
resourceInternal
- the resource to checkprotected abstract boolean isHealthy(T resource)
resource
- the resource to checkprotected abstract void closeResource(T resource) throws IOException
resource
- the resource to closeIOException
protected abstract T createNewResource() throws IOException
IOException
Copyright © 2023. All Rights Reserved.