Package org.apache.commons.pool2.impl
Class DefaultPooledObject<T>
- java.lang.Object
-
- org.apache.commons.pool2.impl.DefaultPooledObject<T>
-
- Type Parameters:
T- the type of object in the pool
- All Implemented Interfaces:
java.lang.Comparable<PooledObject<T>>,PooledObject<T>
- Direct Known Subclasses:
PooledSoftReference
public class DefaultPooledObject<T> extends java.lang.Object implements PooledObject<T>
This wrapper is used to track the additional information, such as state, for the pooled objects.This class is intended to be thread-safe.
- Since:
- 2.0
- Version:
- $Revision: $
-
-
Constructor Summary
Constructors Constructor Description DefaultPooledObject(T object)Create a new instance that wraps the provided object so that the pool can track the state of the pooled object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanallocate()Allocates the object.intcompareTo(PooledObject<T> other)Orders instances based on idle time - i.e.booleandeallocate()booleanendEvictionTest(java.util.Deque<PooledObject<T>> idleQueue)Called to inform the object that the eviction test has ended.longgetActiveTimeMillis()Obtain the time in milliseconds that this object last spent in the the active state (it may still be active in which case subsequent calls will return an increased value).longgetBorrowedCount()Get the number of times this object has been borrowed.longgetCreateTime()Obtain the time (using the same basis asSystem.currentTimeMillis()) that this object was created.longgetIdleTimeMillis()Obtain the time in milliseconds that this object last spend in the the idle state (it may still be idle in which case subsequent calls will return an increased value).longgetLastBorrowTime()Obtain the time the wrapped object was last borrowed.longgetLastReturnTime()Obtain the time the wrapped object was last returned.longgetLastUsedTime()Return an estimate of the last time this object was used.TgetObject()Obtain the underlying object that is wrapped by this instance ofPooledObject.PooledObjectStategetState()Returns the state of this object.voidinvalidate()Sets the state toINVALIDvoidmarkAbandoned()Marks the pooled object as abandoned.voidmarkReturning()Marks the object as returning to the pool.voidprintStackTrace(java.io.PrintWriter writer)Prints the stack trace of the code that borrowed this pooled object and the stack trace of the last code to use this object (if available) to the supplied writer.voidsetLogAbandoned(boolean logAbandoned)Is abandoned object tracking being used? If this is true the implementation will need to record the stack trace of the last caller to borrow this object.booleanstartEvictionTest()Attempt to place the pooled object in thePooledObjectState.EVICTIONstate.java.lang.StringtoString()Provides a String form of the wrapper for debug purposes.voiduse()Record the current stack trace as the last time the object was used.-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.pool2.PooledObject
equals, hashCode
-
-
-
-
Constructor Detail
-
DefaultPooledObject
public DefaultPooledObject(T object)
Create a new instance that wraps the provided object so that the pool can track the state of the pooled object.- Parameters:
object- The object to wrap
-
-
Method Detail
-
getObject
public T getObject()
Description copied from interface:PooledObjectObtain the underlying object that is wrapped by this instance ofPooledObject.- Specified by:
getObjectin interfacePooledObject<T>- Returns:
- The wrapped object
-
getCreateTime
public long getCreateTime()
Description copied from interface:PooledObjectObtain the time (using the same basis asSystem.currentTimeMillis()) that this object was created.- Specified by:
getCreateTimein interfacePooledObject<T>- Returns:
- The creation time for the wrapped object
-
getActiveTimeMillis
public long getActiveTimeMillis()
Description copied from interface:PooledObjectObtain the time in milliseconds that this object last spent in the the active state (it may still be active in which case subsequent calls will return an increased value).- Specified by:
getActiveTimeMillisin interfacePooledObject<T>- Returns:
- The time in milliseconds last spent in the active state
-
getIdleTimeMillis
public long getIdleTimeMillis()
Description copied from interface:PooledObjectObtain the time in milliseconds that this object last spend in the the idle state (it may still be idle in which case subsequent calls will return an increased value).- Specified by:
getIdleTimeMillisin interfacePooledObject<T>- Returns:
- The time in milliseconds last spent in the idle state
-
getLastBorrowTime
public long getLastBorrowTime()
Description copied from interface:PooledObjectObtain the time the wrapped object was last borrowed.- Specified by:
getLastBorrowTimein interfacePooledObject<T>- Returns:
- The time the object was last borrowed
-
getLastReturnTime
public long getLastReturnTime()
Description copied from interface:PooledObjectObtain the time the wrapped object was last returned.- Specified by:
getLastReturnTimein interfacePooledObject<T>- Returns:
- The time the object was last returned
-
getBorrowedCount
public long getBorrowedCount()
Get the number of times this object has been borrowed.- Returns:
- The number of times this object has been borrowed.
- Since:
- 2.1
-
getLastUsedTime
public long getLastUsedTime()
Return an estimate of the last time this object was used. If the class of the pooled object implementsTrackedUse, what is returned is the maximum ofTrackedUse.getLastUsed()andgetLastBorrowTime(); otherwise this method gives the same value asgetLastBorrowTime().- Specified by:
getLastUsedTimein interfacePooledObject<T>- Returns:
- the last time this object was used
-
compareTo
public int compareTo(PooledObject<T> other)
Description copied from interface:PooledObjectOrders instances based on idle time - i.e. the length of time since the instance was returned to the pool. Used by the GKOP idle object evictor.Note: This class has a natural ordering that is inconsistent with equals if distinct objects have the same identity hash code.
- Specified by:
compareToin interfacejava.lang.Comparable<T>- Specified by:
compareToin interfacePooledObject<T>
-
toString
public java.lang.String toString()
Description copied from interface:PooledObjectProvides a String form of the wrapper for debug purposes. The format is not fixed and may change at any time.- Specified by:
toStringin interfacePooledObject<T>- Overrides:
toStringin classjava.lang.Object
-
startEvictionTest
public boolean startEvictionTest()
Description copied from interface:PooledObjectAttempt to place the pooled object in thePooledObjectState.EVICTIONstate.- Specified by:
startEvictionTestin interfacePooledObject<T>- Returns:
trueif the object was placed in thePooledObjectState.EVICTIONstate otherwisefalse
-
endEvictionTest
public boolean endEvictionTest(java.util.Deque<PooledObject<T>> idleQueue)
Description copied from interface:PooledObjectCalled to inform the object that the eviction test has ended.- Specified by:
endEvictionTestin interfacePooledObject<T>- Parameters:
idleQueue- The queue of idle objects to which the object should be returned- Returns:
- Currently not used
-
allocate
public boolean allocate()
Allocates the object.- Specified by:
allocatein interfacePooledObject<T>- Returns:
trueif the original state wasIDLE
-
deallocate
public boolean deallocate()
- Specified by:
deallocatein interfacePooledObject<T>- Returns:
trueif the state wasALLOCATED
-
invalidate
public void invalidate()
Sets the state toINVALID- Specified by:
invalidatein interfacePooledObject<T>
-
use
public void use()
Description copied from interface:PooledObjectRecord the current stack trace as the last time the object was used.- Specified by:
usein interfacePooledObject<T>
-
printStackTrace
public void printStackTrace(java.io.PrintWriter writer)
Description copied from interface:PooledObjectPrints the stack trace of the code that borrowed this pooled object and the stack trace of the last code to use this object (if available) to the supplied writer.- Specified by:
printStackTracein interfacePooledObject<T>- Parameters:
writer- The destination for the debug output
-
getState
public PooledObjectState getState()
Returns the state of this object.- Specified by:
getStatein interfacePooledObject<T>- Returns:
- state
-
markAbandoned
public void markAbandoned()
Marks the pooled object as abandoned.- Specified by:
markAbandonedin interfacePooledObject<T>
-
markReturning
public void markReturning()
Marks the object as returning to the pool.- Specified by:
markReturningin interfacePooledObject<T>
-
setLogAbandoned
public void setLogAbandoned(boolean logAbandoned)
Description copied from interface:PooledObjectIs abandoned object tracking being used? If this is true the implementation will need to record the stack trace of the last caller to borrow this object.- Specified by:
setLogAbandonedin interfacePooledObject<T>- Parameters:
logAbandoned- The new configuration setting for abandoned object tracking
-
-