Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Pool resources using Apache's Commons Pool Framework

Create a simple thread pool to handle concurrent requests

Pooling resources (also called object pooling) among multiple clients is a technique used to promote object reuse and to reduce the overhead of creating new resources, resulting in better performance and throughput. Imagine a heavy-duty Java server application that sends hundreds of SQL queries by opening and closing connections for every SQL request. Or a Web server that serves hundreds of HTTP requests, handling each request by spawning a separate thread. Or imagine creating an XML parser instance for every request to parse a document without reusing the instances. These are some of the scenarios that warrant optimization of the resources being used.

Resource usage could prove critical at times for heavy-duty applications. Some famous Websites have shut down because of their inability to handle heavy loads. Most problems related to heavy loads can be handled, at a macro level, using clustering and load-balancing capabilities. Concerns remain at the application level with respect to excessive object creation and the availability of limited server resources like memory, CPU, threads, and database connections, which could represent potential bottlenecks and, when not utilized optimally, bring down the whole server.

In some situations, the database usage policy could enforce a limit on the number of concurrent connections. Also, an external application could dictate or restrict the number of concurrent open connections. A typical example is a domain registry (like Verisign) that limits the number of available active socket connections for registrars (like BulkRegister). Pooling resources has proven to be one of the best options in handling these types of issues and, to a certain extent, also helps in maintaining the required service levels for enterprise applications.

Most J2EE application server vendors provide resource pooling as an integral part of their Web and EJB (Enterprise JavaBean) containers. For database connections, the server vendor usually provides an implementation of the DataSource interface, which works in conjunction with the JDBC (Java Database Connectivity) driver vendor's ConnectionPoolDataSource implementation. The ConnectionPoolDataSource implementation serves as a resource manager connection factory for pooled java.sql.Connection objects. Similarly, EJB instances of stateless session beans, message-driven beans, and entity beans are pooled in EJB containers for higher throughput and performance. XML parser instances are also candidates for pooling, because the creation of parser instances consumes much of a system's resources.

A successful open source resource-pooling implementation is the Commons Pool framework's DBCP, a database connection pooling component from the Apace Software Foundation that is extensively used in production-class enterprise applications. In this article, I briefly discuss the internals of the Commons Pool framework and then use it to implement a thread pool.

Let's first look at what the framework provides.

Commons Pool framework

The Commons Pool framework offers a basic and robust implementation for pooling arbitrary objects. Several implementations are provided, but for this article's purposes, we use the most generic implementation, the GenericObjectPool. It uses a CursorableLinkedList, which is a doubly-linked-list implementation (part of the Jakarta Commons Collections), as the underlying datastructure for holding the objects being pooled.

On top, the framework provides a set of interfaces that supply lifecycle methods and helper methods for managing, monitoring, and extending the pool.

The interface org.apache.commons.PoolableObjectFactory defines the following lifecycle methods, which prove essential for implementing a pooling component:

   // Creates an instance that can be returned by the pool
    public Object makeObject() {}
    // Destroys an instance no longer needed by the pool
    public void destroyObject(Object obj) {}
    // Validate the object before using it
    public boolean validateObject(Object obj) {}
    // Initialize an instance to be returned by the pool
    public void activateObject(Object obj) {}
    // Uninitialize an instance to be returned to the pool
    public void passivateObject(Object obj) {}


As you can make out by the method signatures, this interface primarily deals with the following:

  • makeObject(): Implement the object creation
  • destroyObject(): Implement the object destruction
  • validateObject(): Validate the object before it is used
  • activateObject(): Implement the object initialization code
  • passivateObject(): Implement the object uninitialization code


Another core interface—org.apache.commons.ObjectPool—defines the following methods for managing and monitoring the pool:

  // Obtain an instance from my pool
    Object borrowObject() throws Exception;
    // Return an instance to my pool
    void returnObject(Object obj) throws Exception;
    // Invalidates an object from the pool
    void invalidateObject(Object obj) throws Exception;
    // Used for pre-loading a pool with idle objects
    void addObject() throws Exception;
    // Return the number of idle instances
    int getNumIdle() throws UnsupportedOperationException;
    // Return the number of active instances
    int getNumActive() throws UnsupportedOperationException;
    // Clears the idle objects
    void clear() throws Exception, UnsupportedOperationException;
    // Close the pool
    void close() throws Exception;
    //Set the ObjectFactory to be used for creating instances
    void setFactory(PoolableObjectFactory factory) throws IllegalStateException,
    UnsupportedOperationException;


The ObjectPool interface's implementation takes a PoolableObjectFactory as an argument in its constructors, thereby delegating object creation to its subclasses. I don't talk much about design patterns here since that is not our focus. For readers interested in looking at the UML class diagrams, please see Resources.

As mentioned above, the class org.apache.commons.GenericObjectPool is only one implementation of the org.apache.commons.ObjectPool interface. The framework also provides implementations for keyed object pools, using the interfaces org.apache.commons.KeyedObjectPoolFactory and org.apache.commons.KeyedObjectPool, where one can associate a pool with a key (as in HashMap) and thus manage multiple pools.

1 | 2 | 3 | 4 | 5 |  Next >

Discuss

Start a new discussion or jump into one of the threads below:

Subject Replies Last post
. Pool resources using Apache's Commons Pool Frame
By JavaWorldAdministrator
12 04/22/08 06:03 AM
by Anonymous
. setMinEvictableIdle used instead of correct param
By Peter Andrews
2 04/22/08 06:02 AM
by Anonymous


Resources