|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.picocontainer.behaviors.AbstractBehavior<T> org.picocontainer.gems.behaviors.Pooled<T>
public final class Pooled<T>
ComponentAdapter
implementation that pools components.
The implementation utilizes a delegated ComponentAdapter to create the instances of the pool. The
pool can be configured to grow unlimited or to a maximum size. If a component is requested from
this adapter, the implementation returns an available instance from the pool or will create a
new one, if the maximum pool size is not reached yet. If none is available, the implementation
can wait a defined time for a returned object before it throws a Pooled.PoolException
.
This implementation uses the Pool
toy from the ProxyToys project. This ensures, that any component,
that is out of scope will be automatically returned to the pool by the garbage collector.
Additionally will every component instance also implement
Poolable
, that can be used to return the instance
manually. After returning an instance it should not be used in client code anymore.
Before a returning object is added to the available instances of the pool again, it should be reinitialized to a normalized state. By providing a proper Resetter implementation this can be done automatically. If the object cannot be reused anymore it can also be dropped and the pool may request a new instance.
The pool supports components with a lifecycle. If the delegated ComponentAdapter
implements a LifecycleStrategy
, any component retrieved form the pool will be started
before and stopped again, when it returns back into the pool. Also if a component cannot be
resetted it will automatically be disposed. If the container of the pool is disposed, that any
returning object is also disposed and will not return to the pool anymore. Note, that current
implementation cannot dispose pooled objects.
Nested Class Summary | |
---|---|
static interface |
Pooled.Context
Context of the Pooled used to initialize it. |
static class |
Pooled.DefaultContext
The default context for a Pooled. |
static class |
Pooled.PoolException
Exception thrown from the Pooled. |
Nested classes/interfaces inherited from interface org.picocontainer.ComponentAdapter |
---|
ComponentAdapter.NOTHING |
Field Summary | |
---|---|
static int |
BLOCK_ON_WAIT
BLOCK_ON_WAIT forces the pool to wait until an object of the pool is returning
in case none is immediately available. |
static int |
DEFAULT_MAX_SIZE
DEFAULT_MAX_SIZE is the default size of the pool. |
static Resetter |
DEFAULT_RESETTER
DEFAULT_RESETTER is a NoOperationResetter that is used by default. |
static int |
FAIL_ON_WAIT
FAIL_ON_WAIT forces the pool to fail none is immediately available. |
static int |
UNLIMITED_SIZE
UNLIMITED_SIZE is the value to set the maximum size of the pool to unlimited (Integer.MAX_VALUE
in fact). |
Fields inherited from class org.picocontainer.behaviors.AbstractBehavior |
---|
delegate |
Constructor Summary | |
---|---|
protected |
Pooled()
Construct an empty ComponentAdapter, used for serialization with reflection only. |
|
Pooled(ComponentAdapter delegate,
Pooled.Context context)
Construct a Pooled. |
Method Summary | |
---|---|
void |
dispose(PicoContainer container)
Dispose of the container will dispose all returning objects. |
T |
getComponentInstance(PicoContainer container,
Type into)
|
String |
getDescriptor()
|
int |
size()
Retrieve the current size of the pool. |
void |
start(PicoContainer container)
Start of the container ensures that at least one pooled component has been started. |
void |
stop(PicoContainer container)
Stop of the container has no effect for the pool. |
Methods inherited from class org.picocontainer.behaviors.AbstractBehavior |
---|
accept, changeMonitor, componentHasLifecycle, currentMonitor, dispose, findAdapterOfType, getComponentImplementation, getComponentInstance, getComponentKey, getDelegate, hasLifecycle, isLazy, isStarted, start, stop, toString, verify |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final int UNLIMITED_SIZE
UNLIMITED_SIZE
is the value to set the maximum size of the pool to unlimited (Integer.MAX_VALUE
in fact).
public static final int DEFAULT_MAX_SIZE
DEFAULT_MAX_SIZE
is the default size of the pool.
public static final int BLOCK_ON_WAIT
BLOCK_ON_WAIT
forces the pool to wait until an object of the pool is returning
in case none is immediately available.
public static final int FAIL_ON_WAIT
FAIL_ON_WAIT
forces the pool to fail none is immediately available.
public static final Resetter DEFAULT_RESETTER
DEFAULT_RESETTER
is a NoOperationResetter
that is used by default.
Constructor Detail |
---|
public Pooled(ComponentAdapter delegate, Pooled.Context context)
ComponentAdapter
does not cache.
delegate
- the delegated ComponentAdaptercontext
- the Pooled.Context
of the pool
IllegalArgumentException
- if the maximum pool size or the serialization mode is
invalidprotected Pooled()
Method Detail |
---|
public T getComponentInstance(PicoContainer container, Type into)
As long as the maximum size of the pool is not reached and the pool is exhausted, the
implementation will request its delegate for a new instance, that will be managed by the
pool. Only if the maximum size of the pool is reached, the implementation may wait (depends
on the initializing Pooled.Context
) for a returning object.
getComponentInstance
in interface ComponentAdapter<T>
getComponentInstance
in class AbstractBehavior<T>
Pooled.PoolException
- if the pool is exhausted or waiting for a returning object timed out or
was interruptedpublic String getDescriptor()
public int size()
public void start(PicoContainer container)
ComponentAdapter
supports a lifecylce by implementing
LifecycleStrategy
.
start
in interface ComponentLifecycle<T>
start
in class AbstractBehavior<T>
IllegalStateException
- if pool was already disposedpublic void stop(PicoContainer container)
ComponentAdapter
supports a lifecylce by implementing LifecycleStrategy
.
stop
in interface ComponentLifecycle<T>
stop
in class AbstractBehavior<T>
IllegalStateException
- if pool was already disposedpublic void dispose(PicoContainer container)
ComponentAdapter
supports a lifecylce by
implementing LifecycleStrategy
.
dispose
in interface ComponentLifecycle<T>
dispose
in class AbstractBehavior<T>
IllegalStateException
- if pool was already disposed
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |