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.
|
static class |
DynamicResourcePool.SelectionPolicy
A policy specifying in what order to pick a resource item from a pool.
|
Modifier and Type | Field and Description |
---|---|
protected java.time.Clock |
mClock |
protected ConcurrentHashMap<T,DynamicResourcePool.ResourceInternal<T>> |
mResources |
protected DynamicResourcePool.SelectionPolicy |
mSelectionPolicy
the selection policy of the resource pool.
|
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 com.codahale.metrics.Counter |
getMetricCounter() |
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() |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
acquireCloseable, acquireCloseable
protected final DynamicResourcePool.SelectionPolicy mSelectionPolicy
DynamicResourcePool.SelectionPolicy
for detailsprotected final ConcurrentHashMap<T,DynamicResourcePool.ResourceInternal<T>> mResources
protected java.time.Clock mClock
public DynamicResourcePool(DynamicResourcePool.Options options)
options
- the optionsprotected abstract com.codahale.metrics.Counter getMetricCounter()
public 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.