|
xmlBlaster 2.2.0 client 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
,
org.xmlBlaster.test.classtest.TestPoolManager
Field Summary | |
---|---|
static java.lang.String |
GENERATE_RANDOM
Use this constant with the reserve() method, the PoolManager generates a random, unique in universe session ID |
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. |
void |
destroy()
Cleanup everything. |
void |
erase(java.lang.String instanceId)
Explicitly remove a resource. |
protected void |
finalize()
Cleanup. |
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. |
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 |
---|
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
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()
public 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
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
public void erase(java.lang.String instanceId)
instanceId
- The unique resource IDpublic void destroy()
|
xmlBlaster 2.2.0 client API | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |