|
xmlBlaster 2.2.0 API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.xmlBlaster.util.pool.PoolManager
public final class PoolManager
A little framework to handle a pool of limited resources.
You can use this pool as a base class handling of your limited resources like a 'JDBC connection pool' or a 'thread pool'. The important attributes of a resource are gathered in theResourceWrapper
class.
You can easily handle bigger number of resources with good performance.
To find out how to use it see the TestPool example in the main() method of this class.
State chart of resource handling: +<------- reserve() if(numIdle==0) ---------------------+ | | | +<- reserve() --+ +<-preReserve() *-+ | | | if(numIdle>0) | | | | | | | | | | ######### ########## ########## # # # # # # # busy # # idle # # undef # # # # # # # ######### ########## ########## | | | | | | | | | | | | | | | Explicit | | | | | Explicit | | | | | | +-- release() ->+ | | | +-- erase() ----->+ | | | | | | | | | | | | +busyToIdleTimeout()>+ | +idleToEraseTimeout()>+ | | | | | | | +-- erase on max cycles *->+ | | | +--------- busyToEraseTimeout() since creation *-------->+There are three states:
Note that state transitions marked with '*' are not yet implemented. If you need one of them, code it and contribute it please.
You can choose which states you wish for your resource and how timeouts
are used to handle state transitions.
For example if you want to pool user login sessions and want to do
an auto logout after 60 minutes, you would use the busyToIdleTimeout and set it to 60*60*1000.
If a user is active you can refresh the session with busyRefresh().
Often you want to use your own generated sessionId as the primary key
for this resource, you can pass it as the instanceId argument to reserve(sessionId).
(See example [2] in this main() method)
If you want to pool JDBC connections, reserve() a connection before you do your query
and release() it immediately again. If you
want to close connections after peak usage, you could set a idleToEraseTimeout,
to erase your JDBC connection after some time not used (reducing the current pool size).
Note that in this example the connections are anonymous (all are logged in to the database
with the same user name), it is not important which you receive.
(See example [1] in this main() method)
For an implementation example see TestPoolManager.java
This code is derived from the org.jutils.pool package.
ResourceWrapper
,
TestPoolManager
Field Summary | |
---|---|
private java.util.Hashtable |
busy
Holds busy resources |
private long |
busyToIdleTimeout
Default maximum busy time of a resource, on timeout it changes state from 'busy' to 'idle' |
private I_PoolManager |
callback
The callback into the using application, which needs to implement the interface I_PoolManager |
private long |
counter
Unique counter to generate IDs |
static java.lang.String |
GENERATE_RANDOM
Use this constant with the reserve() method, the PoolManager generates a random, unique in universe session ID |
private java.util.Vector |
idle
Holds free resources |
private long |
idleToEraseTimeout
Default maximum idle span of a resource, on timeout it changes state from 'idle' to 'undef' (it is deleted) |
private int |
maxInstances
Default maximum pool size (number of resources) |
private java.lang.String |
ME
Nice, unique name for logging output |
private java.lang.Object |
meetingPoint
|
private java.lang.String |
poolName
A nice name for the generated id |
private Timeout |
transitionTimer
Triggers transitions |
static java.lang.String |
USE_HASH_CODE
Use this constant with the reserve() method, the PoolManager uses the hashCode() of your object |
static java.lang.String |
USE_OBJECT_REF
Use this constant with the reserve() method, the PoolManager uses the toString() of your object, if no toString() exists the object reference is used, e.g. |
Constructor Summary | |
---|---|
PoolManager(java.lang.String poolName,
I_PoolManager callback,
int maxInstances,
long busyToIdleTimeout,
long idleToEraseTimeout)
Create a new pool instance with the desired behavior. |
Method Summary | |
---|---|
void |
busyRefresh(java.lang.String instanceId)
Restart countdown for resource life cycle. |
private java.lang.String |
createId(java.lang.String instanceId)
Generate a unique resource ID |
void |
destroy()
Cleanup everything. |
private void |
erase(ResourceWrapper rw)
Remove a resource. |
void |
erase(java.lang.String instanceId)
Explicitly remove a resource. |
protected void |
finalize()
Cleanup. |
private ResourceWrapper |
findBusySilent(java.lang.String instanceId)
Find a resource in busy list. |
private ResourceWrapper |
findIdleSilent(java.lang.String instanceId)
Find a resource in idle list. |
private ResourceWrapper |
findLow(java.lang.String instanceId)
Find a resource in busy list. |
int |
getNumBusy()
Number of resources in the 'busy' list. |
int |
getNumIdle()
Number of resources in the 'idle' list. |
java.lang.String |
getState()
Dump the current state of this pool. |
Timeout |
getTransistionTimer()
|
boolean |
isBusy(java.lang.String instanceId)
Test if the resource is busy. |
void |
release(java.lang.String instanceId)
Release a resource explicitly from 'busy' into the 'idle' pool. |
ResourceWrapper |
reserve()
Get a new resource. |
ResourceWrapper |
reserve(long localBusyToIdleTimeout,
long localIdleToEraseTimeout,
java.lang.String instanceId)
Get a new resource. |
ResourceWrapper |
reserve(java.lang.String instanceId)
Get a new resource. |
private void |
setBusyToIdleTimeout(long busyToIdleTimeout)
On timeout, the resource changes state from 'busy' to 'idle'. |
private void |
setIdleToEraseTimeout(long idleToEraseTimeout)
Set the max. |
private void |
setMaxInstances(int maxInstances)
Set the maximum pool size. |
private void |
swap(ResourceWrapper rw,
boolean toBusy)
Idle - busy swapper. |
(package private) void |
timeoutBusyToIdle(ResourceWrapper rw)
Recycle busy resource after timeout. |
(package private) void |
timeoutIdleToErase(ResourceWrapper rw)
Erase an idle resource after timeout. |
java.lang.String |
toXml()
Dump state of this object into a XML ASCII string. |
java.lang.String |
toXml(java.lang.String extraOffset)
Dump state of this object into a XML ASCII string. |
Methods inherited from class java.lang.Object |
---|
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private java.lang.String ME
private java.lang.String poolName
private I_PoolManager callback
private java.util.Hashtable busy
private java.util.Vector idle
private int maxInstances
private long busyToIdleTimeout
private long idleToEraseTimeout
private long counter
public static final java.lang.String USE_HASH_CODE
public static final java.lang.String USE_OBJECT_REF
public static final java.lang.String GENERATE_RANDOM
private Timeout transitionTimer
private final java.lang.Object meetingPoint
Constructor Detail |
---|
public PoolManager(java.lang.String poolName, I_PoolManager callback, int maxInstances, long busyToIdleTimeout, long idleToEraseTimeout)
poolName
- A nice name for this pool manager instance.callback
- The interface 'I_PoolManager' callbackmaxInstances
- Max. number of resources in this pool.busyToIdleTimeout
- Max. busy time of this resource in milli secondsidleToEraseTimeout
- Max. idle time span of this resource in milli secondsMethod Detail |
---|
public Timeout getTransistionTimer()
private void setMaxInstances(int maxInstances)
maxInstances
- How many resources are allowedprivate void setBusyToIdleTimeout(long busyToIdleTimeout)
busyToIdleTimeout
- Max. busy time of this resource in milli secondsprivate void setIdleToEraseTimeout(long idleToEraseTimeout)
idleToEraseTimeout
- Max. idle time of this resource in milli secondspublic ResourceWrapper reserve() throws XmlBlasterException
XmlBlasterException
- Error with random generatorpublic ResourceWrapper reserve(java.lang.String instanceId) throws XmlBlasterException
instanceId
- See description in other reserve() method.
XmlBlasterException
- Error with random generatorpublic ResourceWrapper reserve(long localBusyToIdleTimeout, long localIdleToEraseTimeout, java.lang.String instanceId) throws XmlBlasterException
localBusyToIdleTimeout
- Max. busy time of this resource in milli seconds,
only for this current resourcelocalIdleToEraseTimeout
- Max. idle time of this resource in milli seconds,
only for this current resourceinstanceId
- If given and string length > 1, the delivered ID is used:
If in busy list found, this is returned, else a new is created.XmlBlasterException
- Error with random generatorpublic void release(java.lang.String instanceId) throws XmlBlasterException
instanceId
- The unique resource ID
XmlBlasterException
private java.lang.String createId(java.lang.String instanceId) throws XmlBlasterException
XmlBlasterException
- random generatorprivate void swap(ResourceWrapper rw, boolean toBusy)
private ResourceWrapper findBusySilent(java.lang.String instanceId)
instanceId
- The unique resource ID
private ResourceWrapper findIdleSilent(java.lang.String instanceId)
instanceId
- The unique resource ID
private ResourceWrapper findLow(java.lang.String instanceId) throws XmlBlasterException
instanceId
- The unique resource ID
XmlBlasterException
- "ResourceNotFound"public boolean isBusy(java.lang.String instanceId)
instanceId
- The unique resource ID
public void busyRefresh(java.lang.String instanceId) throws XmlBlasterException
instanceId
- The unique resource ID
XmlBlasterException
- ResourceNotFoundpublic int getNumBusy()
public int getNumIdle()
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
public java.lang.String getState()
public final java.lang.String toXml()
public final java.lang.String toXml(java.lang.String extraOffset)
extraOffset
- indenting of tags for nice output
void timeoutBusyToIdle(ResourceWrapper rw)
void timeoutIdleToErase(ResourceWrapper rw)
public void erase(java.lang.String instanceId)
instanceId
- The unique resource IDprivate void erase(ResourceWrapper rw)
rw
- The resource wrapper objectpublic void destroy()
|
xmlBlaster 2.2.0 API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |