Jakarta Commons Project Research - Pool 1.pool Project What is pool, simply, is to build some Object, put it in pool, when you need it, you can get directly from Pool, no need Re-establish .. The most commonly heard is Database Connection Pooling, because it is a time-consuming job for establishing database links. If we first build a link, you can save this time. DatabaseConnection Pooling is DBCP. 1. The base class is the base class pool structure: public interface ObjectPool {Object borrowObject (); void returnObject (Object borrowed);} major extension class: public interface KeyedObjectPool {Object borrowObject (Object key); void returnObject (Object key, Object borrowed );} Index the object of pool according to Key
The basic class only provides the most basic two functions to create and return to the POOL object. 2. To achieve the basic class BaseObjectPool - PoolableObjectFactory - BasePoolableObjectFactory KeyedObjectPool - KeyedPoolableObjectFactory - BaseKeyedPoolableObjectFactory StackObjectPool - StackKeyedObjectPool-- instances can be created in the initialization, a limited number of idle GenericObjectPool - GenericKeyedObjectPool-- comprising idle setting, Active The quantity and the settings of the settings in Pool, SoftReferenceObjectPool, can be increased, and his recycling is a general in the garbage collection station that provides Pool's API mainly three aspects: providing a general object POL The interface can be simply used and implemented. For example, BaseObjectPool and KeyedObjectPool. Provides a widget to create a modular Pool. For example, STACKObjectPool. ImplemericObjectPool.3. Implementation
Objectpool
Object obj = null; try {// Create an object obj = pool.borrowObject (); // Capture exception} catch (exception e) {} finally {// confirm that the object has returned if (null! = Obj) {pool. ReturnObject (OBJ);}}
KeyedObjectPool
Object obj = null; object key = "key";
Try {// Create Object Obj = pool.borrowObject (key); // Capture exception} catch (exception e) {} finally {// confirmation Return IF (null! = obj) {pool.returnObject (key, obj); }} Other types of inheritance can be 4. Example (taken from Apache) Readerutil.java
Import java.io.reader; import java.io.ioException;
Public class readERUTIL {
Public ReaderUtil () {}
. / ** * Dumps the contents of the {@link Reader} to a * String, closing the {@link Reader} when done * / public String readToString (Reader in) throws IOException {StringBuffer buf = new StringBuffer (); try {for (int C = in.read (); c! = -1; c = in.read ()) {buf.append ((char) c);} return buf.tostring ();} catch (IOException e ) {Throw e;} finally {Try {in.close ();} catch (exception e) {// ignored}}}}}}}}
Conversion of the order, get POOL first, and then take the value (recommended)
import org.apache.commons.pool.ObjectPool; import java.io.Reader; import java.io.IOException; public class ReaderUtil {private ObjectPool pool; public ReaderUtil (ObjectPool pool) {this.pool = pool;} / ** * Dumps the contents of the {@link Reader} to a * String, closing the {@link Reader} when done * / public String readToString (Reader in) throws IOException {StringBuffer buf = null;. try {buf = (StringBuffer) (Pool.borrowObject ()); for (int C = in.read (); c! = -1; c = in.read ()) {buf.append ((char) c);} return buf.tostring );} Catch (ooException e) {throw e;} catch (exception e) {throw new runtimeException ("unable to borrow buffer from pool" e.tostring ());} finally {TRY {in.close (); } catch (exception e) {// ignored} Try {if (null! = Buf) {pool.returnObject (buf);}} catch (exception e) {// ignored}}}} Do Pool's example with StringBuffer (not recommended, for familiar knowledge)
StringBufferFactory.java
import org.apache.commons.pool.BasePoolableObjectFactory; public class StringBufferFactory extends BasePoolableObjectFactory {// for makeObject we'll simply return a new buffer public Object makeObject () {return new StringBuffer ();} // when an object is returned to The pool, // We'll Clear It Out Public Void PassivateObject (Object Obj) {StringBuffer BUF = (StringBuffer) Obj; buf.setlength (0);} // for all other methods, the no-op // Implementation in BasepooLableObjectFactory // Will Suffice} Modifying Readerutil by StringBufferFactory Pool gets StringBuffer.
NEW READERUTIL (New STRINGBUFFERFAACTORY ()) 5. Summary We usually use the pool mechanism in portions of the IO, reduce some time to establish access, and is more important for the most time consuming data inventory, I This will be introduced in the Commons-DBCP topic. This article is the second one of this series. It will be launched later. The next theme is DBCP, please continue to pay attention .6. Reference - related Bibliographic or related articles * Jakarta Commons: http: //jakarta.apache.org/commons / * jakarta commons pool http://jakarta.apache.org/commons/po / * Jakarta Commons pool: http://jakarta.apache.org/commons/pool/apidocs/ Index.html * OREILLY: USING THE JAKARTA COMMONS, Part 3: # 3 http://www.onjava.com/pub/a/onjava/2003/07/23/commons.html?page=3
All rights reserved in this article, not allowable, please contact, please contact: topojuly@tom.com