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 the ResourceWrapper
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.
@author "Marcel Ruff"
|
|
|
|
|
|