Object Pool对象池化技术

本文探讨了对象池技术的适用场景及其实现原理。通过分析不同类型的对象,指出并非所有对象都适合池化,并以ResumeableVendorOrderProcessor为例,展示了如何使用Apache Commons Pool实现对象池,以减少昂贵对象创建的开销。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值