|
Back to JOC 10.1.2 Tutorial Index
Pools
A pool is a collection of identical object instances that are managed by the Cache Service. Objects in a pool are "checked out," used for a while then returned to the pool to be used by someone else. A pool is created in the context of a region and can be associated with a group. To create a pool, use the createPool method of the CacheAccess class. createPool requires a PoolInstanceFactory object (see Creating a PoolInstanceFactory for more details).
import oracle.ias.cache.*;
try
{
CacheAccess cacc = CacheAccess.getAccess("Stock-Market");
Attributes attr = new Attributes();
QuoteFactory poolFac = new QuoteFactory();
attr.setIdleTime(180); // set idle time for an object in the pool to 3 minutes
// create a pool in the "Stock-Market" region with a minimum of
// 5 and a maximum of 10 object instances in the pool
cacc.createPool("get Quote", poolFac, attr, 5, 10);
cacc.close();
}
catch (CacheException ex)
{
// handle exception
}
To access objects in a pool use the PoolAccess class. The static method PoolAccess.getPool() will return a handle to the specified pool. The get method will return an instance of an object from the pool. The returnToPool method should be called when the object is no longer needed and close should be called when the pool handle is no longer needed.
PoolAccess pacc = PoolAccess.getPool("Stock-Market", "get Quote");
GetQuote gq = (GetQuote)pacc.get(); // get an object from the pool
// do something useful with the gq object
pacc.returnToPool(gq);
pacc.close();
Pool Instance Factory
The PoolInstanceFactory is used by the cache service to create the object instances in a pool. The PoolInstanceFactory is an abstract class with two methods, createInstance and destroyInstance. The createInstance method is called to create instances of the object being pooled. The destroyInstance method is called by the cache service when an instance of the object is being removed from the cache. The object instance is passed into destroyInstance. To implement a PoolInstanceFactory:
import oracle.ias.cache.*;
public class MyPoolFactory implements PoolInstanceFactory
{
public Object createInstance()
{
MyObject obj = new MyObject();
obj.init();
return obj;
}
public void destroyInstance(Object obj)
{
((MyObject)obj).cleanup();
}
}
Pool Object Affinity
The PoolAffinityFactory is used by the cache service to create affinity object instances in a pool. When a pool handle requests an object, sometimes it needs to set certain states in the object before using the object. An affinity object keeps track of its associated handle and these states even after being returned to the pool. The next time the same handle requests an object from the pool, the same object is returned. This can save cleanup and setup work in-between the two requests. This association between object and handle is only broken when release is called on the handle, when the handle is closed, or when the object has been handed over to another handle.
When another handle tries to obtain the affinity object after the object has been returned to the pool, PoolAffinityFactory.affinityRelease() is called to determine whether the original handle is willing to break the association with the object. If yes, affinityRelease should clean up handle-dependent states in the object and return true; otherwise, affinityRelease should return false, and the other handle will be denied access to the object. The example below demonstrates implementing a PoolAffinityFactory.
import oracle.ias.cache.*;
public class MyPoolFactory implements PoolAffinityFactory
{
public Object createInstance()
{
MyObject obj = new MyObject();
obj.init();
return obj;
}
public void destroyInstance(Object obj)
{
((MyObject)obj).cleanup();
}
public boolean affinityRelease(Object obj)
{
if (((MyObject)obj).canBeReleased())
{
((MyObject)obj).cleanupState();
return true;
}
else
{
return false;
}
}
}
Back to JOC 10.1.2 Tutorial Index
|