public abstract class KObjectPool<T> extends java.lang.Object implements KSchedulerClient
The Object pool consists of
-minimumSize : the initial number of objects
-maximumSize : the maximum number of objects
-cleanupPeriod : the amount of time between cleanups. During a clean up all the
  objects created after the initial creation (objects over the minimum)
  that are inactive for a period longer than the shrink period will be closed.
-shrinkTime : the amount of time extra objects can remain inactive before it is
  eligible for clean up
-recyclePeriod : the amount of time an initial object can remain inactive before it is recycled
  (objects are closed and reopen)
Modifier and Type | Field and Description |
---|---|
protected long |
cleanupPeriod |
protected long |
recyclePeriod |
protected long |
shrinkTime |
static long |
TIMEOUT_UNDEFINED |
static long |
TIMEOUT_UNLIMITED |
Constructor and Description |
---|
KObjectPool()
Creates a new instance
|
Modifier and Type | Method and Description |
---|---|
protected void |
apply(java.util.function.Consumer<T> consumer)
Apply a non-blocking operation on each pool workers.
|
void |
checkIn(T o)
This method sets the object available
|
T |
checkOut()
This method makes a request for an unlocked object from the pool.
|
T |
checkOut(long timeout,
java.util.concurrent.TimeUnit unit)
This method makes a request for an unlocked object from the pool.
|
void |
close()
This method closes one by one all the objects currently opened for this
the pool
|
abstract T |
create()
Creates the object encapsulating the appropriate object
|
void |
createUnlockedInstance()
Creates an unlocked instance of the appropriate object
and adds it to the pool.
|
abstract void |
expire(T o)
This method closes the object
|
long |
getDefaultTimeout()
Gets the default timeout to use on checkout action if no timeout is specified.
|
protected int |
getId() |
PoolMonitoringInfo |
getMonitoringInfo()
This method gets the PoolMonitorInfo associated with the KObject pool instance.
|
int |
getPoolMaximumSize()
Gets the maximum number of concurrent objects allowed
|
int |
getPoolSize()
Gets the number of objects currently used
|
int |
getPoolUsageCount()
Gets the number of currently used objects
|
int |
getPoolUsagePeak()
Gets the peak number of objects used simultaneously
this method is used for statistic reasons.
|
protected long |
getSystemDefaultTimeout() |
void |
initialize(int minimumSize)
Initializes a KObjectPool instance with the specified minimum size
|
void |
initialize(int minimumSize,
long recyclePeriod)
Initializes a KObjectPool instance with the specified minimum size and
recycle period
|
void |
initialize(int minimumSize,
long shrinkTime,
long cleanupPeriod,
long recyclePeriod)
Initializes a KObjectPool instance with the specified minimum size,
recycle period, clean up period and shrink period
|
protected void |
internalErrorOccurred(java.lang.Exception e) |
void |
reset()
This will close and reinitialize the pool.
|
protected void |
resourceAdded(T o)
No-op hook that allows implementation instructions after resource creation.
|
void |
schCallback(int jobId)
This method is automatically called by the KScheduler
to either perform the clean up or object recycle jobs
|
void |
setDefaultTimeout(long defaultTimeout)
Sets the default timeout to use on checkout action if no timeout is specified.
|
void |
setPoolMaximumSize(int size)
Sets the maximum number of objects allowed
|
void |
setPoolMinimumSize(int size)
Sets the pool minimum size
|
abstract boolean |
validate(T o)
This method checks if the object is valid
|
public static final long TIMEOUT_UNDEFINED
public static final long TIMEOUT_UNLIMITED
protected long shrinkTime
protected long cleanupPeriod
protected long recyclePeriod
public void initialize(int minimumSize) throws java.lang.Exception
minimumSize
- the minimum number of objects in this pooljava.lang.Exception
- if an error occurred while initializing the object poolpublic void initialize(int minimumSize, long recyclePeriod) throws java.lang.Exception
minimumSize
- the minimum number of objects in this poolrecyclePeriod
- the amount of time an initial object can remain inactive before
it is recycled (closed than reopen).java.lang.Exception
- if an error occurred while initializing the object poolpublic void initialize(int minimumSize, long shrinkTime, long cleanupPeriod, long recyclePeriod) throws java.lang.Exception
minimumSize
- the minimum number of objects in this poolshrinkTime
- the amount of time extra objects can remain inactive before it
eligible for cleanupcleanupPeriod
- the amount of time between cleanupsrecyclePeriod
- the amount of time an initial object can remain inactive before
it is recycled (closed than reopen).java.lang.Exception
- if an error occurred while initializing the object poolpublic abstract T create() throws java.lang.Exception
java.lang.Exception
- if an error occurred while creating the objectpublic abstract boolean validate(T o)
o
- object to validatepublic abstract void expire(T o)
o
- the object to be expiredpublic int getPoolSize()
public int getPoolMaximumSize()
protected int getId()
protected void internalErrorOccurred(java.lang.Exception e)
public void setPoolMaximumSize(int size)
size
- the corresponding integer numberpublic void setPoolMinimumSize(int size)
size
- the corresponding integer valuepublic int getPoolUsageCount()
public int getPoolUsagePeak()
public long getDefaultTimeout()
public void setDefaultTimeout(long defaultTimeout)
defaultTimeout
- value in millisecondsprotected long getSystemDefaultTimeout()
public void createUnlockedInstance() throws java.lang.Exception
java.lang.Exception
- if an error occurred while creating the objectjava.util.concurrent.TimeoutException
- if the operation is not executed within the default timeout delay.public T checkOut() throws java.lang.Exception
java.lang.Exception
- if an error occurred while checking out an objectjava.util.concurrent.TimeoutException
- if the operation is not executed within the default timeout delay.public T checkOut(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.Exception
timeout
- the maximum time to waitunit
- the time unit of the timeout argumentjava.lang.Exception
- if an error occurred while checking out an objectjava.util.concurrent.TimeoutException
- if the operation is not executed within the specified timeout delay.public void checkIn(T o)
o
- the corresponding objectpublic void close()
public void reset() throws java.lang.Exception
java.lang.Exception
public void schCallback(int jobId)
schCallback
in interface KSchedulerClient
jobId
- the job Id to determine which job will be donepublic PoolMonitoringInfo getMonitoringInfo()
PoolMonitoringInfo
protected void resourceAdded(T o)
o
- the corresponding objectprotected void apply(java.util.function.Consumer<T> consumer)