Object Pool对象池化技术
并非所有对象都适合拿来池化??因为维护对象池也要造成一定开销。对生成时开销不大的对象进行池化,反而可能会出现“维护对象池的开销”大于“生成新对象的开销”,从而使性能降低的情况。但是对于生成时开销可观的对象,池化技术就是提高性能的有效策略了。
This is my ObjectFactory using object pool:
import org.apache.commons.pool.KeyedPoolableObjectFactory;
import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.apache.commons.pool.impl.GenericKeyedObjectPool.Config;
public class StateMachineFactory implements KeyedPoolableObjectFactory
{
private static GenericKeyedObjectPool _objectPool = null;
public Object makeObject(Object key) throws Exception
{
ResumeableVendorOrderProcessor resumeableVendorOrderProcessor = null;
Class aClass = null;
if ((null != key))
{
try
{
aClass = Class.forName("the process class name, you need change this", true, this.getClass().getClassLoader());
resumeableVendorOrderProcessor = (ResumeableVendorOrderProcessor) aClass.newInstance();
}
catch (Exception e)
{
Log.error(this, "makeObject() exception:" + e);
}
}
return (resumeableVendorOrderProcessor);
}
public void destroyObject(Object key, Object object) throws Exception
{
}
public boolean validateObject(Object key, Object object)
{
return (true);
}
public static ResumeableVendorOrderProcessor getNewInstance(Object key)
{
Log.info(StateMachineFactory.class, "getNewInstance() - key: " + key);
ResumeableVendorOrderProcessor resumeableVendorOrderProcessor = null;
Class aClass = null;
if ((null != key))
{
try
{
aClass = Class.forName("the process class name, you need change this");
resumeableVendorOrderProcessor = (ResumeableVendorOrderProcessor) aClass.newInstance();
}
catch (Exception e)
{
Log.error(StateMachineFactory.class, "getNewInstance() exception:" + e);
}
}
return (resumeableVendorOrderProcessor);
}
synchronized public static ResumeableVendorOrderProcessor getInstance(Object key)
{
ResumeableVendorOrderProcessor resumeableVendorOrderProcessor = null;
String vendorId = "";
try
{
if (null == _objectPool)
{
initObjectPool(false);
}
if ((null != key) && (null != _objectPool))
{
vendorId = (String) key;
if (vendorId != null && !"".equals(vendorId))
{
resumeableVendorOrderProcessor = (ResumeableVendorOrderProcessor) _objectPool.borrowObject(vendorId);
}
}
}
catch (Throwable e)
{
Log.error(StateMachineFactory.class, "makeObject() exception:" + e);
}
return (resumeableVendorOrderProcessor);
}
synchronized public static void returnInstance(Object key, ResumeableVendorOrderProcessor resumeableVendorOrderProcessor)
{
String vendorId = null;
if ((null != key) && (null != _objectPool))
{
try
{
vendorId = (String) key;
Log.info(StateMachineFactory.class, "returnInstance() - Returning " + key);
_objectPool.returnObject(vendorId, resumeableVendorOrderProcessor);
}
catch (Throwable aThrowable)
{
aThrowable.printStackTrace();
}
}
}
synchronized public static void initObjectPool(boolean forceInit)
{
Log.info(StateMachineFactory.class, "initObjectPool()");
Config poolConfiguation = null;
if ((null == _objectPool) || forceInit)
{
_objectPool = new GenericKeyedObjectPool(new StateMachineFactory());
poolConfiguation = getPoolConfiguration();
_objectPool.setConfig(poolConfiguation);
}
}
private static Config getPoolConfiguration()
{
Log.info(StateMachineFactory.class, "getPoolConfiguration()");
Config poolConfiguration = new Config();
// Per key
poolConfiguration.maxActive = 25;
// For entire pool
poolConfiguration.maxTotal = 2500;
return (poolConfiguration);
}
public void activateObject(Object key, Object object) throws Exception
{
Log.info(StateMachineFactory.class, "activeObject");
}
public void passivateObject(Object key, Object object) throws Exception
{
Log.info(StateMachineFactory.class, "passivateObject");
}
}
We can call getInstance and returnObject to use object in pool
Everytime you returnObject, you need confirm that the object is stateless, or you need to re-init them.
并非所有对象都适合拿来池化??因为维护对象池也要造成一定开销。对生成时开销不大的对象进行池化,反而可能会出现“维护对象池的开销”大于“生成新对象的开销”,从而使性能降低的情况。但是对于生成时开销可观的对象,池化技术就是提高性能的有效策略了。
This is my ObjectFactory using object pool:
import org.apache.commons.pool.KeyedPoolableObjectFactory;
import org.apache.commons.pool.impl.GenericKeyedObjectPool;
import org.apache.commons.pool.impl.GenericKeyedObjectPool.Config;
public class StateMachineFactory implements KeyedPoolableObjectFactory
{
private static GenericKeyedObjectPool _objectPool = null;
public Object makeObject(Object key) throws Exception
{
ResumeableVendorOrderProcessor resumeableVendorOrderProcessor = null;
Class aClass = null;
if ((null != key))
{
try
{
aClass = Class.forName("the process class name, you need change this", true, this.getClass().getClassLoader());
resumeableVendorOrderProcessor = (ResumeableVendorOrderProcessor) aClass.newInstance();
}
catch (Exception e)
{
Log.error(this, "makeObject() exception:" + e);
}
}
return (resumeableVendorOrderProcessor);
}
public void destroyObject(Object key, Object object) throws Exception
{
}
public boolean validateObject(Object key, Object object)
{
return (true);
}
public static ResumeableVendorOrderProcessor getNewInstance(Object key)
{
Log.info(StateMachineFactory.class, "getNewInstance() - key: " + key);
ResumeableVendorOrderProcessor resumeableVendorOrderProcessor = null;
Class aClass = null;
if ((null != key))
{
try
{
aClass = Class.forName("the process class name, you need change this");
resumeableVendorOrderProcessor = (ResumeableVendorOrderProcessor) aClass.newInstance();
}
catch (Exception e)
{
Log.error(StateMachineFactory.class, "getNewInstance() exception:" + e);
}
}
return (resumeableVendorOrderProcessor);
}
synchronized public static ResumeableVendorOrderProcessor getInstance(Object key)
{
ResumeableVendorOrderProcessor resumeableVendorOrderProcessor = null;
String vendorId = "";
try
{
if (null == _objectPool)
{
initObjectPool(false);
}
if ((null != key) && (null != _objectPool))
{
vendorId = (String) key;
if (vendorId != null && !"".equals(vendorId))
{
resumeableVendorOrderProcessor = (ResumeableVendorOrderProcessor) _objectPool.borrowObject(vendorId);
}
}
}
catch (Throwable e)
{
Log.error(StateMachineFactory.class, "makeObject() exception:" + e);
}
return (resumeableVendorOrderProcessor);
}
synchronized public static void returnInstance(Object key, ResumeableVendorOrderProcessor resumeableVendorOrderProcessor)
{
String vendorId = null;
if ((null != key) && (null != _objectPool))
{
try
{
vendorId = (String) key;
Log.info(StateMachineFactory.class, "returnInstance() - Returning " + key);
_objectPool.returnObject(vendorId, resumeableVendorOrderProcessor);
}
catch (Throwable aThrowable)
{
aThrowable.printStackTrace();
}
}
}
synchronized public static void initObjectPool(boolean forceInit)
{
Log.info(StateMachineFactory.class, "initObjectPool()");
Config poolConfiguation = null;
if ((null == _objectPool) || forceInit)
{
_objectPool = new GenericKeyedObjectPool(new StateMachineFactory());
poolConfiguation = getPoolConfiguration();
_objectPool.setConfig(poolConfiguation);
}
}
private static Config getPoolConfiguration()
{
Log.info(StateMachineFactory.class, "getPoolConfiguration()");
Config poolConfiguration = new Config();
// Per key
poolConfiguration.maxActive = 25;
// For entire pool
poolConfiguration.maxTotal = 2500;
return (poolConfiguration);
}
public void activateObject(Object key, Object object) throws Exception
{
Log.info(StateMachineFactory.class, "activeObject");
}
public void passivateObject(Object key, Object object) throws Exception
{
Log.info(StateMachineFactory.class, "passivateObject");
}
}
We can call getInstance and returnObject to use object in pool
Everytime you returnObject, you need confirm that the object is stateless, or you need to re-init them.