Package com.kheops.util
Class KObjectPool<T>
java.lang.Object
com.kheops.util.KObjectPool<T>
- All Implemented Interfaces:
KSchedulerClient
- Direct Known Subclasses:
DatabaseConnectionPool,JMapNetworkConnectionPool,JMapServerRemoteConnectionPool,JMapServerThreadPool,KSocketConnectionPool,SdeConnectionPool
This abstract class encapsulates a pool of objects.
Subclasses will override the method to manage these objects.
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
invalid input: ' ' objects created after the initial creation (objects over the minimum)
invalid input: ' ' 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
invalid input: ' ' eligible for clean up
-recyclePeriod : the amount of time an initial object can remain inactive before it is recycled
invalid input: ' ' (objects are closed and reopen)
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected longprotected longprotected longstatic final longstatic final long -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidApply a non-blocking operation on each pool workers.voidThis method sets the object availablecheckOut()This method makes a request for an unlocked object from the pool.This method makes a request for an unlocked object from the pool.voidclose()This method closes one by one all the objects currently opened for this the poolabstract Tcreate()Creates the object encapsulating the appropriate objectvoidCreates an unlocked instance of the appropriate object and adds it to the pool.abstract voidThis method closes the objectlongGets the default timeout to use on checkout action if no timeout is specified.protected intgetId()This method gets the PoolMonitorInfo associated with the KObject pool instance.intGets the maximum number of concurrent objects allowedintGets the number of objects currently usedintGets the number of currently used objectsintGets the peak number of objects used simultaneously this method is used for statistic reasons.protected longvoidinitialize(int minimumSize) Initializes a KObjectPool instance with the specified minimum sizevoidinitialize(int minimumSize, long recyclePeriod) Initializes a KObjectPool instance with the specified minimum size and recycle periodvoidinitialize(int minimumSize, long shrinkTime, long cleanupPeriod, long recyclePeriod) Initializes a KObjectPool instance with the specified minimum size, recycle period, clean up period and shrink periodprotected voidvoidreset()This will close and reinitialize the pool.protected voidresourceAdded(T o) No-op hook that allows implementation instructions after resource creation.voidschCallback(int jobId) This method is automatically called by the KScheduler to either perform the clean up or object recycle jobsvoidsetDefaultTimeout(long defaultTimeout) Sets the default timeout to use on checkout action if no timeout is specified.voidsetPoolMaximumSize(int size) Sets the maximum number of objects allowedvoidsetPoolMinimumSize(int size) Sets the pool minimum sizeabstract booleanThis method checks if the object is valid
-
Field Details
-
TIMEOUT_UNDEFINED
public static final long TIMEOUT_UNDEFINED- See Also:
-
TIMEOUT_UNLIMITED
public static final long TIMEOUT_UNLIMITED- See Also:
-
shrinkTime
protected long shrinkTime -
cleanupPeriod
protected long cleanupPeriod -
recyclePeriod
protected long recyclePeriod
-
-
Constructor Details
-
KObjectPool
public KObjectPool()Creates a new instance
-
-
Method Details
-
initialize
Initializes a KObjectPool instance with the specified minimum size- Parameters:
minimumSize- the minimum number of objects in this pool- Throws:
Exception- if an error occurred while initializing the object pool
-
initialize
Initializes a KObjectPool instance with the specified minimum size and recycle period- Parameters:
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).- Throws:
Exception- if an error occurred while initializing the object pool
-
initialize
public void initialize(int minimumSize, long shrinkTime, long cleanupPeriod, long recyclePeriod) throws Exception Initializes a KObjectPool instance with the specified minimum size, recycle period, clean up period and shrink period- Parameters:
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).- Throws:
Exception- if an error occurred while initializing the object pool
-
create
Creates the object encapsulating the appropriate object- Returns:
- the corresponding object
- Throws:
Exception- if an error occurred while creating the object
-
validate
This method checks if the object is valid- Parameters:
o- object to validate- Returns:
- true if the object is valid false otherwise
-
expire
This method closes the object- Parameters:
o- the object to be expired
-
getPoolSize
public int getPoolSize()Gets the number of objects currently used- Returns:
- the number of objects currently used
-
getPoolMaximumSize
public int getPoolMaximumSize()Gets the maximum number of concurrent objects allowed- Returns:
- maximumSize the maximum number of allowed objects
-
getId
protected int getId() -
internalErrorOccurred
-
setPoolMaximumSize
public void setPoolMaximumSize(int size) Sets the maximum number of objects allowed- Parameters:
size- the corresponding integer number
-
setPoolMinimumSize
public void setPoolMinimumSize(int size) Sets the pool minimum size- Parameters:
size- the corresponding integer value
-
getPoolUsageCount
public int getPoolUsageCount()Gets the number of currently used objects- Returns:
- the corresponding integer value
-
getPoolUsagePeak
public int getPoolUsagePeak()Gets the peak number of objects used simultaneously this method is used for statistic reasons.- Returns:
- the corresponding integer value
-
getDefaultTimeout
public long getDefaultTimeout()Gets the default timeout to use on checkout action if no timeout is specified. This value either comes from the server configuration or can be configured directly for this instance.- Returns:
- timeout value in milliseconds
- Since:
- 7.0
-
setDefaultTimeout
public void setDefaultTimeout(long defaultTimeout) Sets the default timeout to use on checkout action if no timeout is specified.- Parameters:
defaultTimeout- value in milliseconds- Since:
- 7.0
-
getSystemDefaultTimeout
protected long getSystemDefaultTimeout() -
createUnlockedInstance
Creates an unlocked instance of the appropriate object and adds it to the pool.- Throws:
Exception- if an error occurred while creating the objectTimeoutException- if the operation is not executed within the default timeout delay.
-
checkOut
This method makes a request for an unlocked object from the pool. Afterward the returned object becomes locked and cannot be used by another source unless it is checkIn.- Returns:
- an unlocked free object
- Throws:
Exception- if an error occurred while checking out an objectTimeoutException- if the operation is not executed within the default timeout delay.
-
checkOut
This method makes a request for an unlocked object from the pool. Afterward the returned object becomes locked and cannot be used by another source unless it is checkIn. Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available. Timeout value must be greater than 0 to be supported.- Parameters:
timeout- the maximum time to waitunit- the time unit of the timeout argument- Returns:
- an unlocked free object
- Throws:
Exception- if an error occurred while checking out an objectTimeoutException- if the operation is not executed within the specified timeout delay.- Since:
- 7.0
-
checkIn
This method sets the object available- Parameters:
o- the corresponding object
-
close
public void close()This method closes one by one all the objects currently opened for this the pool -
reset
This will close and reinitialize the pool.- Throws:
Exception
-
schCallback
public void schCallback(int jobId) This method is automatically called by the KScheduler to either perform the clean up or object recycle jobs- Specified by:
schCallbackin interfaceKSchedulerClient- Parameters:
jobId- the job Id to determine which job will be done
-
getMonitoringInfo
This method gets the PoolMonitorInfo associated with the KObject pool instance. The PoolMonitorInfo maintains the pool statistics- Returns:
- the corresponding PoolMonitorInfo instance
- See Also:
-
resourceAdded
No-op hook that allows implementation instructions after resource creation.- Parameters:
o- the corresponding object
-
apply
Apply a non-blocking operation on each pool workers.
-