Java Doc for ObjectPool.java in  » Database-JDBC-Connection-Pool » DBPool » snaq » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database JDBC Connection Pool » DBPool » snaq.util 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   snaq.util.LogUtil
      snaq.util.ObjectPool

All known Subclasses:   snaq.db.ConnectionPool,
ObjectPool
abstract public class ObjectPool extends LogUtil (Code)
Base class for a pool system implementation. This class provides all the base functionality required and can be easily extended to provide pooling support for many different types of object.

New objects are retrieved on demand according to specified limits, and the pool can also ensure an object's validity before returning it. The limits which can be set for a pool include the number of items to be held in the pool, and the maximum number to ever be created.

The pool will try to maintain poolSize items open and ready for use (unless that number has not yet been created), but if expiry is enabled this number will reduce if the items are not used frequently. If maxSize is specified then the pool will never create more than this number of items, and another can only be obtained from the pool if it is handed back by another client.

ObjectPool should be sub-classed to override the following methods:

 protected Reusable create() throws Exception
 protected boolean isValid(final Reusable o)
 protected void destroy(final Reusable o)
 

It is recommended that the sub-class implements methods for obtaining and returning items within the pool, casting the objects returned by this class as required to the appropriate class type.

ObjectPool also support asynchronous destruction of items, which can be useful in circumstances where destruction of items held can take a long time which would delay the checkIn method. This also applies to the release of the pool after it's final use, which should always be done using either release or releaseAsync.
author:
   Giles Winstanley


Inner Class :final class Cleaner extends Thread


Constructor Summary
protected  ObjectPool(String name, int poolSize, int maxSize, long expiryTime)
     Creates new object pool.
protected  ObjectPool(String name, int poolSize, int maxSize, int expiryTime)
     Creates new object pool.

Method Summary
final public  voidaddObjectPoolListener(ObjectPoolListener x)
     Adds an ObjectPoolListener to the event notification list.
final protected  voidcheckIn(Reusable o)
     Checks an object into the pool, and notify other threads that may be waiting for one.
final protected synchronized  ReusablecheckOut()
     Checks out an item from the pool.
final protected synchronized  ReusablecheckOut(long timeout)
     Checks out an item from the pool.
abstract protected  Reusablecreate()
     Object creation method.
abstract protected  voiddestroy(Reusable o)
     Object destruction method.
public  voidfinalize()
     Shuts down the object pool.
final public  voidflush()
     Flushes the pool of all currently available items, emptying the pool.
final public synchronized  intgetCheckedOut()
     Returns the number of items that are currently checked-out.
final public  longgetExpiryTime()
     Returns the expiry time for unused items in the pool.
final public synchronized  intgetFreeCount()
     Returns the number of items held in the pool that are free to be checked-out.
final public  floatgetHitRate()
     Returns hit rate of the pool as a percentage.
final public  intgetMaxSize()
     Returns the maximum number of items that can be created.
final public  StringgetName()
     Returns the pool name.
protected  ClassgetPoolClass()
     Returns the class to use for the pool collection.
final public  intgetPoolSize()
     Returns the maximum size of the pool.
final public synchronized  intgetSize()
     Returns the total number of objects held (available and checked-out).
final public  voidinit(int num)
     Initializes the given number of items in the pool.
final public  booleanisAsyncDestroy()
     Returns whether asynchronous object destruction is enabled.
abstract protected  booleanisValid(Reusable o)
     Object validation method.
public  voidlog(String logEntry)
     Writes a message to the log.
public  voidlog(Throwable e, String logEntry)
     Writes a message with an Exception to the log file.
final synchronized  booleanpurge()
     Purges expired objects from the pool.
final public  voidrelease()
     Releases all items from the pool, and shuts the pool down.
final public synchronized  voidreleaseAsync()
     Releases all items from the pool, and shuts the pool down.
final public  voidreleaseForcibly()
     Forcibly releases all items from the pool, and shuts the pool down.
final public  voidremoveObjectPoolListener(ObjectPoolListener x)
     Removes an ObjectPoolListener from the event notification list.
final protected  voidresetHitCounter()
     Resets the counter for determining the pool's hit rate.
final protected  voidsetAccessFIFO()
     Sets the pool access method to FIFO (first-in, first-out: a queue).
final protected  voidsetAccessLIFO()
     Sets the pool access method to LIFO (last-in, first-out: a stack).
final protected  voidsetAccessRandom()
     Sets the pool access method to random (a random connection is selected for check-out).
final public  voidsetAsyncDestroy(boolean b)
     Determines whether to perform asynchronous object destruction.
final public  voidsetParameters(int poolSize, int maxSize, long expiryTime)
     Sets the pooling parameters.


Constructor Detail
ObjectPool
protected ObjectPool(String name, int poolSize, int maxSize, long expiryTime)(Code)
Creates new object pool.
Parameters:
  name - pool name
Parameters:
  poolSize - maximum number of pooled objects, or 0 for no limit
Parameters:
  maxSize - maximum number of possible objects, or 0 for no limit
Parameters:
  expiryTime - expiry time (milliseconds) for pooled object, or 0 for no expiry



ObjectPool
protected ObjectPool(String name, int poolSize, int maxSize, int expiryTime)(Code)
Creates new object pool.
Parameters:
  name - pool name
Parameters:
  poolSize - maximum number of pooled objects, or 0 for no limit
Parameters:
  maxSize - maximum number of possible objects, or 0 for no limit
Parameters:
  expiryTime - expiry time (milliseconds) for pooled object, or 0 for no expiry




Method Detail
addObjectPoolListener
final public void addObjectPoolListener(ObjectPoolListener x)(Code)
Adds an ObjectPoolListener to the event notification list.



checkIn
final protected void checkIn(Reusable o)(Code)
Checks an object into the pool, and notify other threads that may be waiting for one.
Parameters:
  o - object to check in



checkOut
final protected synchronized Reusable checkOut() throws Exception(Code)
Checks out an item from the pool. If no free item is available, a new item is created unless the maximum number of items has been reached. If a free item is not valid it is removed from the pool and another is retrieved. item from the pool, or null if nothing available
exception:
  Exception - if there is an error creating a new object



checkOut
final protected synchronized Reusable checkOut(long timeout) throws Exception(Code)
Checks out an item from the pool. If there is no pooled item available and the maximum number possible has not been reached, another is created. If a free item is detected as being invalid it is removed from the pool and the another is retrieved. If an item is not available and the maximum number possible has been reached, the method waits for the timeout period for one to become available by being checked in.
Parameters:
  timeout - timeout value in milliseconds item from the pool, or null if nothing available within timeout period
exception:
  Exception - if there is an error creating a new object



create
abstract protected Reusable create() throws Exception(Code)
Object creation method. This method is called when a new item needs to be created following a call to one of the check-out methods.
exception:
  Exception - if unable to create the item



destroy
abstract protected void destroy(Reusable o)(Code)
Object destruction method. This method is called when an object needs to be destroyed due to pool pruning/cleaning, or final release of the pool.



finalize
public void finalize()(Code)
Shuts down the object pool. If overridden the sub-class should make sure super.finalize() is called.



flush
final public void flush()(Code)
Flushes the pool of all currently available items, emptying the pool.



getCheckedOut
final public synchronized int getCheckedOut()(Code)
Returns the number of items that are currently checked-out.



getExpiryTime
final public long getExpiryTime()(Code)
Returns the expiry time for unused items in the pool.



getFreeCount
final public synchronized int getFreeCount()(Code)
Returns the number of items held in the pool that are free to be checked-out.



getHitRate
final public float getHitRate()(Code)
Returns hit rate of the pool as a percentage. The hit rate is the proportion of requests for a connection which result in the return of a connection which is in the pool, rather than which results in the creation of a new connection.



getMaxSize
final public int getMaxSize()(Code)
Returns the maximum number of items that can be created.



getName
final public String getName()(Code)
Returns the pool name.



getPoolClass
protected Class getPoolClass()(Code)
Returns the class to use for the pool collection. This can be over-ridden by a sub-class to provide a different List type for the pool, which may give performance benefits in certain situations. Only instances of List collections should be used. (Default: java.util.ArrayList.class) For those wanting to override this method, pool items are checked-out from the front of the List - remove(0) - and replaced at the end of the List when checked-in again - add(Object).



getPoolSize
final public int getPoolSize()(Code)
Returns the maximum size of the pool.



getSize
final public synchronized int getSize()(Code)
Returns the total number of objects held (available and checked-out).



init
final public void init(int num)(Code)
Initializes the given number of items in the pool. This spawns a new thread to create them in the background.



isAsyncDestroy
final public boolean isAsyncDestroy()(Code)
Returns whether asynchronous object destruction is enabled. (Default: false)



isValid
abstract protected boolean isValid(Reusable o)(Code)
Object validation method. This method is called when checking-out an item to see if it is valid for use. When overridden by the sub-class it is recommended that this method perform suitable checks to ensure the object can be used without problems.



log
public void log(String logEntry)(Code)
Writes a message to the log.



log
public void log(Throwable e, String logEntry)(Code)
Writes a message with an Exception to the log file.



purge
final synchronized boolean purge()(Code)
Purges expired objects from the pool. This method is called by the cleaner thread to purge expired items. false if pool is empty after purging (no further purge required until items added), true otherwise



release
final public void release()(Code)
Releases all items from the pool, and shuts the pool down. If any items are still checked-out, this method waits until all items have been checked-in before returning.



releaseAsync
final public synchronized void releaseAsync()(Code)
Releases all items from the pool, and shuts the pool down. This method returns immediately; a background thread is created to perform the release.



releaseForcibly
final public void releaseForcibly()(Code)
Forcibly releases all items from the pool, and shuts the pool down. If any items are still checked-out, this method forcibly destroys them and then returns.



removeObjectPoolListener
final public void removeObjectPoolListener(ObjectPoolListener x)(Code)
Removes an ObjectPoolListener from the event notification list.



resetHitCounter
final protected void resetHitCounter()(Code)
Resets the counter for determining the pool's hit rate.



setAccessFIFO
final protected void setAccessFIFO()(Code)
Sets the pool access method to FIFO (first-in, first-out: a queue).



setAccessLIFO
final protected void setAccessLIFO()(Code)
Sets the pool access method to LIFO (last-in, first-out: a stack).



setAccessRandom
final protected void setAccessRandom()(Code)
Sets the pool access method to random (a random connection is selected for check-out).



setAsyncDestroy
final public void setAsyncDestroy(boolean b)(Code)
Determines whether to perform asynchronous object destruction. If set to true then each time an object is destroyed (invalid object during pool operation, or when the pool is finally released) the operation is done in a separate thread, allowing the method to return immediately. This can be useful when calling the destroy method on an object takes a long time to complete.



setParameters
final public void setParameters(int poolSize, int maxSize, long expiryTime)(Code)
Sets the pooling parameters. Any items currently in the pool will remain, subject to the new parameters. (The hit rate counters are reset when this method is called.)
Parameters:
  poolSize - number of items to be keep in pool
Parameters:
  maxSize - maximum number of items to be created
Parameters:
  expiryTime - expiry time for unused items (milliseconds) (0 = no expiry)



Fields inherited from snaq.util.LogUtil
protected DateFormat dateFormatddf(Code)(Java Doc)
protected boolean debug(Code)(Java Doc)
protected PrintStream log(Code)(Java Doc)
protected boolean logging(Code)(Java Doc)

Methods inherited from snaq.util.LogUtil
public synchronized void close()(Code)(Java Doc)
public PrintStream getLogStream()(Code)(Java Doc)
public boolean isDebug()(Code)(Java Doc)
public boolean isLogging()(Code)(Java Doc)
public synchronized void log(String prefix, String logEntry)(Code)(Java Doc)
public void log(String logEntry)(Code)(Java Doc)
public void log(Throwable e, String prefix, String logEntry)(Code)(Java Doc)
public void log(Throwable e, String logEntry)(Code)(Java Doc)
public void log(Throwable e)(Code)(Java Doc)
public synchronized void setDateFormat(DateFormat df)(Code)(Java Doc)
public void setDebug(boolean b)(Code)(Java Doc)
public synchronized void setLog(OutputStream out)(Code)(Java Doc)
public synchronized void setLog(PrintStream ps)(Code)(Java Doc)
public void setLogging(boolean b)(Code)(Java Doc)

Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.