Class KObjectPool<T>

java.lang.Object
com.kheops.util.KObjectPool<T>
All Implemented Interfaces:
KSchedulerClient
Direct Known Subclasses:
DatabaseConnectionPool, JMapNetworkConnectionPool, JMapServerRemoteConnectionPool, JMapServerThreadPool, KSocketConnectionPool, SdeConnectionPool

public abstract class KObjectPool<T> extends Object implements KSchedulerClient
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: '&nbsp' objects created after the initial creation (objects over the minimum)
invalid input: '&nbsp' 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: '&nbsp' eligible for clean up

-recyclePeriod : the amount of time an initial object can remain inactive before it is recycled
invalid input: '&nbsp' (objects are closed and reopen)

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected long
     
    protected long
     
    protected long
     
    static final long
     
    static final long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new instance
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    apply(Consumer<T> consumer)
    Apply a non-blocking operation on each pool workers.
    void
    This method sets the object available
    This method makes a request for an unlocked object from the pool.
    checkOut(long timeout, TimeUnit unit)
    This method makes a request for an unlocked object from the pool.
    void
    This method closes one by one all the objects currently opened for this the pool
    abstract T
    Creates the object encapsulating the appropriate object
    void
    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
    Gets the default timeout to use on checkout action if no timeout is specified.
    protected int
     
    This method gets the PoolMonitorInfo associated with the KObject pool instance.
    int
    Gets the maximum number of concurrent objects allowed
    int
    Gets the number of objects currently used
    int
    Gets the number of currently used objects
    int
    Gets the peak number of objects used simultaneously this method is used for statistic reasons.
    protected long
     
    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
     
    void
    This will close and reinitialize the pool.
    protected void
    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
    Sets the maximum number of objects allowed
    void
    Sets the pool minimum size
    abstract boolean
    This method checks if the object is valid

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public void initialize(int minimumSize) throws Exception
      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

      public void initialize(int minimumSize, long recyclePeriod) throws Exception
      Initializes a KObjectPool instance with the specified minimum size and recycle period
      Parameters:
      minimumSize - the minimum number of objects in this pool
      recyclePeriod - 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 pool
      shrinkTime - the amount of time extra objects can remain inactive before it eligible for cleanup
      cleanupPeriod - the amount of time between cleanups
      recyclePeriod - 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

      public abstract T create() throws Exception
      Creates the object encapsulating the appropriate object
      Returns:
      the corresponding object
      Throws:
      Exception - if an error occurred while creating the object
    • validate

      public abstract boolean validate(T o)
      This method checks if the object is valid
      Parameters:
      o - object to validate
      Returns:
      true if the object is valid false otherwise
    • expire

      public abstract void expire(T o)
      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

      protected void internalErrorOccurred(Exception e)
    • 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

      public void createUnlockedInstance() throws Exception
      Creates an unlocked instance of the appropriate object and adds it to the pool.
      Throws:
      Exception - if an error occurred while creating the object
      TimeoutException - if the operation is not executed within the default timeout delay.
    • checkOut

      public T checkOut() throws Exception
      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 object
      TimeoutException - if the operation is not executed within the default timeout delay.
    • checkOut

      public T checkOut(long timeout, TimeUnit unit) throws Exception
      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 wait
      unit - the time unit of the timeout argument
      Returns:
      an unlocked free object
      Throws:
      Exception - if an error occurred while checking out an object
      TimeoutException - if the operation is not executed within the specified timeout delay.
      Since:
      7.0
    • checkIn

      public void checkIn(T o)
      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

      public void reset() throws Exception
      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:
      schCallback in interface KSchedulerClient
      Parameters:
      jobId - the job Id to determine which job will be done
    • getMonitoringInfo

      public PoolMonitoringInfo 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

      protected void resourceAdded(T o)
      No-op hook that allows implementation instructions after resource creation.
      Parameters:
      o - the corresponding object
    • apply

      protected void apply(Consumer<T> consumer)
      Apply a non-blocking operation on each pool workers.