com.ebmwebsourcing.easycommons.pooling
Class GenericResourcePool<T>

java.lang.Object
  extended by com.ebmwebsourcing.easycommons.pooling.GenericResourcePool<T>

public class GenericResourcePool<T>
extends java.lang.Object

A pool of resources. The resource life is managed thank to its ResourceHandler. A minimum number of resources are created at the initialization of the pool. The maximum number of resources corresponds to the limit of resources existing at the same time. Two policy are available for the pool: - WAIT: if a resource is taking from the pool while there is no one anymore, the thread waits an available resource (releasing by another thread) - REJECT: if a resource is taking from the pool while there is no one anymore, an exception is thrown.

Author:
aruffie, Nicolas Oddoux - EBM WebSourcing

Constructor Summary
GenericResourcePool(ResourceHandler<T> ressourceHandler, int minSize, int maxSize, PoolPolicy poolPolicy)
          Instantiate a new resource pool with specified resource handler, minimum size and maximum size of the pool and pool policy
 
Method Summary
 void release(T resource)
          Release the specified resource After putting back a resource in the pool, the method onRelease() of the resource handler is called.
 T take()
          Take one unused resource in the current pool.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericResourcePool

public GenericResourcePool(ResourceHandler<T> ressourceHandler,
                           int minSize,
                           int maxSize,
                           PoolPolicy poolPolicy)
Instantiate a new resource pool with specified resource handler, minimum size and maximum size of the pool and pool policy

Parameters:
ressourceHandler - A ResourceHandler in order to manage resources of current pool. it must not be null.
minSize - the minimum number of resources in the current pool (created at the initialization).
maxSize - the maximum number of resources in the current pool (limit of the pool). It must be greater or equals to the specified minSize. The maximum value is Integer.MAX_VALUE
poolPolicy - the PoolPolicy to adopt when the maximum size is reached. it must not be null.
Method Detail

take

public final T take()
Take one unused resource in the current pool. After getting a resource from the pool and before returning resource, the method onTake() of the resource handler is called.

WARNING: The following pattern must be use to avoid not to release some resources
 String st = null;
 try {
     st = stringPool.take();
     // ...
 } catch (PoolException e) { // Optional catch clause to treat the pool exception
     // ...
 } finally { // Mandatory finally clause to release the resource in any cases
     if(st != null) {
         stringPool.release(st);
     }
 }
 

Returns:
one
Throws:
PoolException - if the current thread is interrupted for the pool policy WAIT or if there is no more available resource in the pool for the pool policy REJECT

release

public final void release(T resource)
Release the specified resource After putting back a resource in the pool, the method onRelease() of the resource handler is called.

Parameters:
resource - The resource to release


Copyright © 2012 Petals Link. All Rights Reserved.