|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.picocontainer.defaults.DecoratingComponentAdapter
org.picocontainer.gems.adapters.PoolingComponentAdapter
public class PoolingComponentAdapter
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 availailabe 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 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 lifecylce. 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 |
PoolingComponentAdapter.Context
Context of the PoolingComponentAdapter used to initialize it. |
static class |
PoolingComponentAdapter.DefaultContext
The default context for a PoolingComponentAdapter. |
(package private) static class |
PoolingComponentAdapter.LifecycleResetter
|
Field Summary | |
---|---|
private boolean |
autostartGC
|
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. |
private java.util.List |
components
|
static int |
DEFAULT_MAX_SIZE
DEFAULT_MAX_SIZE is the default size of the pool. |
static com.thoughtworks.proxy.kit.Resetter |
DEFAULT_RESETTER
DEFAULT_RESETTER is a NoOperationResetter that is used by default. |
private boolean |
delegateHasLifecylce
|
private boolean |
disposed
|
static int |
FAIL_ON_WAIT
FAIL_ON_WAIT forces the pool to fail none is immediately available. |
private int |
maxPoolSize
|
private com.thoughtworks.proxy.toys.pool.Pool |
pool
|
private int |
serializationMode
|
private static long |
serialVersionUID
|
private boolean |
started
|
static int |
UNLIMITED_SIZE
UNLIMITED_SIZE is the value to set the maximum size of the pool to unlimited (Integer.MAX_VALUE
in fact). |
private int |
waitMilliSeconds
|
Constructor Summary | |
---|---|
protected |
PoolingComponentAdapter()
Construct an empty ComponentAdapter, used for serialization with reflection only. |
|
PoolingComponentAdapter(org.picocontainer.ComponentAdapter delegate)
Construct a PoolingComponentAdapter with default settings. |
|
PoolingComponentAdapter(org.picocontainer.ComponentAdapter delegate,
PoolingComponentAdapter.Context context)
Construct a PoolingComponentAdapter. |
Method Summary | |
---|---|
void |
dispose(org.picocontainer.PicoContainer container)
Dispose of the container will dispose all returning objects. |
java.lang.Object |
getComponentInstance(org.picocontainer.PicoContainer container)
|
private void |
readObject(java.io.ObjectInputStream in)
|
int |
size()
Retrieve the current size of the pool. |
void |
start(org.picocontainer.PicoContainer container)
Start of the container ensures that at least one pooled component has been started. |
void |
stop(org.picocontainer.PicoContainer container)
Stop of the container has no effect for the pool. |
private void |
writeObject(java.io.ObjectOutputStream out)
|
Methods inherited from class org.picocontainer.defaults.DecoratingComponentAdapter |
---|
accept, changeMonitor, currentMonitor, dispose, getComponentImplementation, getComponentKey, getDelegate, hasLifecycle, hasLifecycle, start, stop, toString, verify |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface org.picocontainer.LifecycleManager |
---|
hasLifecycle |
Field Detail |
---|
private static final long serialVersionUID
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 com.thoughtworks.proxy.kit.Resetter DEFAULT_RESETTER
DEFAULT_RESETTER
is a NoOperationResetter
that is used by default.
private int maxPoolSize
private int waitMilliSeconds
private com.thoughtworks.proxy.toys.pool.Pool pool
private int serializationMode
private boolean autostartGC
private boolean started
private boolean disposed
private boolean delegateHasLifecylce
private transient java.util.List components
Constructor Detail |
---|
public PoolingComponentAdapter(org.picocontainer.ComponentAdapter delegate)
delegate
- the delegated ComponentAdapterpublic PoolingComponentAdapter(org.picocontainer.ComponentAdapter delegate, PoolingComponentAdapter.Context context)
ComponentAdapter
does not cache.
delegate
- the delegated ComponentAdaptercontext
- the PoolingComponentAdapter.Context
of the pool
java.lang.IllegalArgumentException
- if the maximum pool size or the serialization mode is
invalidprotected PoolingComponentAdapter()
Method Detail |
---|
public java.lang.Object getComponentInstance(org.picocontainer.PicoContainer container)
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 PoolingComponentAdapter.Context
) for a returning object.
getComponentInstance
in interface org.picocontainer.ComponentAdapter
getComponentInstance
in class org.picocontainer.defaults.DecoratingComponentAdapter
PoolException
- if the pool is exhausted or waiting for a returning object timed out or
was interruptedpublic int size()
public void start(org.picocontainer.PicoContainer container)
ComponentAdapter
supports a lifecylce by implementing
LifecycleStrategy
.
start
in interface org.picocontainer.LifecycleManager
start
in class org.picocontainer.defaults.DecoratingComponentAdapter
java.lang.IllegalStateException
- if pool was already disposedpublic void stop(org.picocontainer.PicoContainer container)
ComponentAdapter
supports a lifecylce by implementing LifecycleStrategy
.
stop
in interface org.picocontainer.LifecycleManager
stop
in class org.picocontainer.defaults.DecoratingComponentAdapter
java.lang.IllegalStateException
- if pool was already disposedpublic void dispose(org.picocontainer.PicoContainer container)
ComponentAdapter
supports a lifecylce by
implementing LifecycleStrategy
.
dispose
in interface org.picocontainer.LifecycleManager
dispose
in class org.picocontainer.defaults.DecoratingComponentAdapter
java.lang.IllegalStateException
- if pool was already disposedprivate void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
java.io.IOException
private void readObject(java.io.ObjectInputStream in) throws java.io.IOException, java.lang.ClassNotFoundException
java.io.IOException
java.lang.ClassNotFoundException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |