net.csdn.modules.thrift.pool
Class BaseObjectPool<K,V>

java.lang.Object
  extended by net.csdn.modules.thrift.pool.BaseObjectPool<K,V>
All Implemented Interfaces:
ObjectPool<K,V>

public class BaseObjectPool<K,V>
extends Object
implements ObjectPool<K,V>

The basic implementation of ObjectPool for high performance and scalability This class should be thread-safe.

Author:
Bongjae Chang

Nested Class Summary
static class BaseObjectPool.Builder<K,V>
           
 
Method Summary
 V borrowObject(K key, long timeoutInMillis)
          Obtains an instance from this pool

Instances returned from this method will have been either newly created with createObject or will be a previously idle object and then validated with validateObject.

 void createAllMinObjects(K key)
          Create objects using the factory until pool's minimum size, and then place them in the idle object pool

createAllMinObjects is useful for "pre-loading" a pool with idle objects.

 void destroy()
          Destroy this pool, and free any resources associated with it

Calling other methods such as createAllMinObjects or borrowObject, returnObject or removeObject or removeAllObjects after invoking this method on a pool will cause them to throw an IllegalStateException.

 int getActiveCount(K key)
          Returns the number of instances currently borrowed from but not yet returned to the pool
 int getIdleCount(K key)
          Returns the number of instances currently idle in this pool
 long getKeepAliveTimeoutInSecs()
           
 int getMax()
           
 int getMin()
           
 int getPeakCount(K key)
          Returns the total peak number of instances
 int getPoolSize(K key)
          Returns the total number of instances
 boolean isBorrowValidation()
           
 boolean isDisposable()
           
 boolean isReturnValidation()
           
 void removeAllObjects(K key)
          Clears the specified pool, removing all pooled instances corresponding to the given key
 void removeObject(K key, V value)
          Removes(invalidates) an object from the pool

By contract, value should have been obtained using borrowObject using a key that is equivalent to the one used to borrow the instance in the first place.

 void returnObject(K key, V value)
          Return an instance to the pool

By contract, value should have been obtained using borrowObject using a key that is equivalent to the one used to borrow the instance in the first place.

 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Method Detail

createAllMinObjects

public void createAllMinObjects(K key)
                         throws NoValidObjectException
Create objects using the factory until pool's minimum size, and then place them in the idle object pool

createAllMinObjects is useful for "pre-loading" a pool with idle objects.

Specified by:
createAllMinObjects in interface ObjectPool<K,V>
Parameters:
key - the key new instances should be added to
Throws:
NoValidObjectException

removeAllObjects

public void removeAllObjects(K key)
Clears the specified pool, removing all pooled instances corresponding to the given key

Specified by:
removeAllObjects in interface ObjectPool<K,V>
Parameters:
key - the key to clear

borrowObject

public V borrowObject(K key,
                      long timeoutInMillis)
               throws PoolExhaustedException,
                      NoValidObjectException,
                      InterruptedException
Obtains an instance from this pool

Instances returned from this method will have been either newly created with createObject or will be a previously idle object and then validated with validateObject.

By contract, clients should return the borrowed instance using returnObject, removeObject

When the pool has been exhausted, a PoolExhaustedException will be thrown.

Specified by:
borrowObject in interface ObjectPool<K,V>
Parameters:
key - the key used to obtain the object
timeoutInMillis - the max time(milli-second) for borrowing the object. If the pool cannot return an instance in given time, PoolExhaustedException will be thrown.
Returns:
an instance from this pool
Throws:
PoolExhaustedException - when the pool is exhausted
NoValidObjectException - when the pool cannot or will not return another instance
InterruptedException - when the pool is interrupted

returnObject

public void returnObject(K key,
                         V value)
Return an instance to the pool

By contract, value should have been obtained using borrowObject using a key that is equivalent to the one used to borrow the instance in the first place.

Specified by:
returnObject in interface ObjectPool<K,V>
Parameters:
key - the key used to obtain the object
value - a borrowed instance to be returned

removeObject

public void removeObject(K key,
                         V value)
Removes(invalidates) an object from the pool

By contract, value should have been obtained using borrowObject using a key that is equivalent to the one used to borrow the instance in the first place.

This method should be used when an object that has been borrowed is determined (due to an exception or other problem) to be invalid.

Specified by:
removeObject in interface ObjectPool<K,V>
Parameters:
key - the key used to obtain the object
value - a borrowed instance to be removed

destroy

public void destroy()
Destroy this pool, and free any resources associated with it

Calling other methods such as createAllMinObjects or borrowObject, returnObject or removeObject or removeAllObjects after invoking this method on a pool will cause them to throw an IllegalStateException.

Specified by:
destroy in interface ObjectPool<K,V>

getPoolSize

public int getPoolSize(K key)
Returns the total number of instances

Specified by:
getPoolSize in interface ObjectPool<K,V>
Parameters:
key - the key to query
Returns:
the total number of instances corresponding to the given key currently idle and active in this pool or a negative value if unsupported

getPeakCount

public int getPeakCount(K key)
Returns the total peak number of instances

Specified by:
getPeakCount in interface ObjectPool<K,V>
Parameters:
key - the key to query
Returns:
the peak number of instances corresponding to the given key or a negative value if unsupported

getActiveCount

public int getActiveCount(K key)
Returns the number of instances currently borrowed from but not yet returned to the pool

Specified by:
getActiveCount in interface ObjectPool<K,V>
Parameters:
key - the key to query
Returns:
the number of instances corresponding to the given key currently borrowed in this pool or a negative value if unsupported

getIdleCount

public int getIdleCount(K key)
Returns the number of instances currently idle in this pool

Specified by:
getIdleCount in interface ObjectPool<K,V>
Parameters:
key - the key to query
Returns:
the number of instances corresponding to the given key currently idle in this pool or a negative value if unsupported

getMin

public int getMin()

getMax

public int getMax()

isBorrowValidation

public boolean isBorrowValidation()

isReturnValidation

public boolean isReturnValidation()

isDisposable

public boolean isDisposable()

getKeepAliveTimeoutInSecs

public long getKeepAliveTimeoutInSecs()

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2014. All Rights Reserved.