Interfaces and Implementation


The Pool library provides both a suite of interfaces and a set of concrete implementations. This allows a developer to maintain compatibility with a wide variety of applications, while also adding new functionality as needed. In turn, the Pool library contains two hierarchiesone for a generic pool, and one for keyed pools. The hierarchy for generic pools is shown in Figure 5-1.

Figure 5-1. Generic pool class hierarchy.


The semantics for an object pool are fairly straightforwardyou must first implement the interface org.apache.commons.pool.PoolableObjectFactory (a no-operation abstract class, org.apache.commons.pool.BasePoolableObjectFactory, can be subclassed if preferred). At a minimum, you will need to provide an implementation of the makeObject() method, to define how and what should be created.

Next, you will need to create an ObjectPool. You can create an ObjectPool by using an ObjectPoolFactory or simply by allocating it using new, passing your PoolableObjectFactory implementation in to the constructor. Depending on the ObjectPool implementation you use, your object pool will have different behavior. The GenericObjectPool uses a first in, first out behavior, suitable for situations in which you want to ensure that all of the objects are used frequently. The StackObjectPool, on the other hand, uses a last in, first out behavior. A useful alternative to the StackObjectPool is the SoftReferenceObjectPool, which allows garbage collection of unused objects.

The KeyedObjectPool hierarchy, as shown in Figure 5-2, is similar to the GenericPool, but with the addition of a key value. This allows you to maintain a single pool with the elements retrievable by key. The default implementations allow one or more objects per key, but you could provide an implementation that restricts use to a single object per key.

Figure 5-2. Keyed pool class hierarchy.




    Apache Jakarta Commons(c) Reusable Java Components
    Real World Web Services
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 137
    Authors: Will Iverson

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net