|
 |
|
Depends: |
log |
Related: |
n/a |
|
|
|
Description
The 'objpool' service provides general purpose object pooling facility. Main features of the object pool service
include but not limited to:
- Ability to pool plain Java objects (POJOs).
- Having flexiable growth policies.
- Statistical information for object and thread pools.
- Ability to programatically control pool's size.
- Notification and invalidation capabilities for pool objects.
- Thread pool that is specifically designed to work with threads.
- Pre-built pool objects and factories for primitive type arrays.
- Unified XML-based configuration.
Top
Configuration
'objpool' service acts as a factory for the object pools and as a container. Once object pool is created,
it is stored in the service and can later be retrived by name or deleted. Object pool can be created at runtime
or specified in XML configuration. Object pools specified in configuration are created automatically and can
be access at runtime by their names.
'objpool' service is configured via pre-defined xtier_objpool.xml configuration file.
This file follows standard xTier service configuration pattern that can be demonstrated by the following complete
example of object pool configuration:
| 1 |  | <xtier-objpool> |
| 2 |  | <region name="examples"> |
| 3 |  | <!----> |
| 4 |  | <pool name="my.pool.xml" size="5" lazy="true"> |
| 5 |  | <factory> |
| 6 |  | <ioc policy="new"> |
| 7 |  | <java class="com.fitechlabs.xtier.examples. |
| 8 |  | services.objpool.FooBarFactory"/> |
| 9 |  | </ioc> |
| 10 |  | </factory> |
| 11 |  | |
| 12 |  | <resize-policy> |
| 13 |  | <ioc policy="new"> |
| 14 |  | <java class="com.fitechlabs.xtier.services. |
| 15 |  | objpool.policies.ObjectPoolGrowPolicy"> |
| 16 |  | <ctor> |
| 17 |  | <!-- |
| 18 |  | |
| 19 |  | |
| 20 |  | |
| 21 |  | --> |
| 22 |  | <arg type="float32">1</arg> |
| 23 |  | </ctor> |
| 24 |  | </java> |
| 25 |  | </ioc> |
| 26 |  | </resize-policy> |
| 27 |  | </pool> |
| 28 |  | </region> |
| 29 |  | </xtier-objpool> |
'objpool' configuration region consists of zero or more <pool> tags each defining an object pool.
Each <pool> tag contains mandatory <factory> IoC-based tag that defines object pool factory to be used with this object pool.
Each <pool> and <thread-pool> tag also contains optional <resize-policy> IoC-based tag that defines object pool resizing policy to be used with this object pool.
For specifics of IoC and IoC-based configuration see IocService.
Top
Examples
Usage of 'objpool' service follows the standard pattern of using xTier service: you need to obtain an instance of xTier kernel that serves as a service registry.
Once you have xTier kernel you can get an instance of any service, in our case the object pool service. Once the service instance is obtained the service API can be used.
The following code snippet illustrates the basic usage pattern for the 'objpool' service:
| 1 |  | |
| 2 |  | XtierKernel xtier = XtierKernel.getInstance(); |
| 3 |  | |
| 4 |  | |
| 5 |  | objpool = xtier.objpool(); |
| 6 |  | |
| 7 |  | |
| 8 |  | ObjectPool pool1 = objpool.getPool("my.pool.xml"); |
| 9 |  | |
| 10 |  | |
| 11 |  | ObjectPool pool2 = objpool.createPool( |
| 12 |  | "my.pool.runtime", |
| 13 |  | 5, |
| 14 |  | ObjectPoolService.POLICY_OVERFLOW, |
| 15 |  | new FooBarFactory(), |
| 16 |  | false); |
| 17 |  | |
| 18 |  | |
| 19 |  | Object obj = pool2.acquire(); |
| 20 |  | ... |
| 21 |  | |
| 22 |  | |
| 23 |  | objpool.deletePool(pool2.getName()); |
| 24 |  | |
| 25 |  | |
| 26 |  | ThreadPool pool3 = objpool.getThreadPool("my.threadpool.xml"); |
| 27 |  | |
| 28 |  | |
| 29 |  | ThreadPool pool4 = objpool.createThreadPool( |
| 30 |  | "my.threadpool.runtime", |
| 31 |  | 10, |
| 32 |  | ObjectPoolService.POLICY_GROW, |
| 33 |  | true, |
| 34 |  | Thread.NORM_PRIORITY); |
| 35 |  | |
| 36 |  | |
| 37 |  | pool4.addTask(new Runnable() { |
| 38 |  | public void run() { |
| 39 |  | |
| 40 |  | ... |
| 41 |  | } |
| 42 |  | }); |
| 43 |  | ... |
| 44 |  | |
| 45 |  | |
| 46 |  | |
| 47 |  | objpool.deleteWaitThreadPool(pool4.getName()); |
Download xTier for full examples and documentation.
Top
|