|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.tivoli.pd.jutil.ContextPool<C>
public class ContextPool<C extends PDContext>
This class provides a generic pooling management behavior to manages a pool of shared contexts.
Applications are encouraged to obtain the PDContext
from the pool
to avoid the overhead of creating a new PDContext every time
when one is needed.
ContextPool
The ContextPool
must be initialized with an appropriate
PooledContextBuilder
. A default PooledContextBuilder
is provided as DefaultPooledPDContextBuilder
. However applications are
free to provide their own
Refer to PooledContextBuilder
and
DefaultPooledContextBuilder
javadoc for more information on how
to implement a custom PooledContextBuilder
.
For example, to create a PooledContextBuilder
for a
PDContext
pool.
URL url = new URL("file:///path/to/configFile"); PooledContextBuilder<PDContext>builder = new DefaultPooledPDContextBuilder(url);
The PooledContextBuilder
is responsible for creating the actual
context instances within the pool. Setup a PooledContextBuilder
by passing it as a parameter in ContextPool
constructor. Client
must call the initailize() method to setup the pool before it can be used.
ContextPool<PDContext> pool = new ContextPool<PDContext>(builder); pool.initalize();
PDContext
from the PoolPDContext
, the client should call one of the two
getContext()
methods to remove a shared PDContext
from the pool. The client can then proceed to the use the PDContext
as normal.
PDContext context = pool.getContext();
PDContext
back into the pool
The context should be release back into the pool by calling the
PDContext.close()
method when it is not needed. Failure to do
so will result in ContextPool
exhaustion.
context.close();
ContextPool
And finally, when the pool is no longer needed, its resources can be released
by calling the ContextPool.close()
. The pool cannot be called on
after that.
pool.close();
PooledContextBuilder
,
DefaultPooledPDContextBuilder
Constructor Summary | |
---|---|
ContextPool(PooledContextBuilder<C> builder)
Creates a ContextPool with a default pool size of 10
PDContext . |
|
ContextPool(PooledContextBuilder<C> builder,
int size)
Creates a ContextPool with the given pool size. |
Method Summary | |
---|---|
void |
close()
Close the context pool. |
C |
getContext()
Gets a PDContext from the pool. |
C |
getContext(int waitTime)
Gets a PDContext from the pool. |
void |
initialize()
Initializes the pool. |
void |
reclaim(C context)
Reclaims a shared context back into the pool. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ContextPool(PooledContextBuilder<C> builder)
ContextPool
with a default pool size of 10
PDContext
.
builder
- PooledContextBuilder
to create the context
to be managed by this context pool.public ContextPool(PooledContextBuilder<C> builder, int size) throws java.lang.IllegalArgumentException
ContextPool
with the given pool size.
builder
- PooledContextBuilder
to create the contextsize
- the PDContext
pool size.
java.lang.IllegalArgumentException
- If pool size is less then 1.Method Detail |
---|
public void initialize() throws PDException
Initializes the pool.
This method must be called before obtaining any
PDContext
from the pool.
PDException
- when an error occurred.public C getContext() throws PDException
Gets a PDContext
from the pool.
If there is no available context in the pool, this method will wait indefinitely until one is made available.
PDException
- when an error occurred.public C getContext(int waitTime) throws PDException
Gets a PDContext
from the pool.
If the pool does not have any available context, this method will wait
for the specified time. After which if there is still no context available,
it will throw a NoPDContextAvailableException
.
waitTime
- time (in milliseconds) to wait for a context. A wait time
of zero or less will wait indefinately.
NoPDContextAvailableException
- when wait time exceeded.
PDException
- an error has occurred.public void reclaim(C context)
Reclaims a shared context back into the pool.
NOTE: This method is for the consumption of context pool implementation.
Client code using a pooled context should call the PDContext.close()
method to return the pooled context to the pool instead of this method.
context
- the context to be returned into the pool.public void close()
Close the context pool.
All resources held by the contexts within the pool will be released. The pool should not be used after calling this method.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |