- 浏览: 2542046 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
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.
发表评论
-
Update Site will come soon
2021-06-02 04:10 1672I am still keep notes my tech n ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 422Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 364Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 444VPN Server 2020(2)Docker on Cen ... -
Nginx Deal with OPTIONS in HTTP Protocol
2020-02-15 01:33 346Nginx Deal with OPTIONS in HTTP ... -
PDF to HTML 2020(1)pdftohtml Linux tool or PDFBox
2020-01-29 07:37 399PDF to HTML 2020(1)pdftohtml Li ... -
Elasticsearch Cluster 2019(2)Kibana Issue or Upgrade
2020-01-12 03:25 710Elasticsearch Cluster 2019(2)Ki ... -
Spark Streaming 2020(1)Investigation
2020-01-08 07:19 291Spark Streaming 2020(1)Investig ... -
Hadoop Docker 2019 Version 3.2.1
2019-12-10 07:39 288Hadoop Docker 2019 Version 3.2. ... -
MongoDB 2019(3)Security and Auth
2019-11-16 06:48 236MongoDB 2019(3)Security and Aut ... -
MongoDB 2019(1)Install 4.2.1 Single and Cluster
2019-11-11 05:07 286MongoDB 2019(1) Follow this ht ... -
Monitor Tool 2019(1)Monit Installation and Usage
2019-10-17 08:22 320Monitor Tool 2019(1)Monit Insta ... -
Ansible 2019(1)Introduction and Installation on Ubuntu and CentOS
2019-10-12 06:15 305Ansible 2019(1)Introduction and ... -
Timezone and Time on All Servers and Docker Containers
2019-10-10 11:18 328Timezone and Time on All Server ... -
Kafka Cluster 2019(6) 3 Nodes Cluster on CentOS7
2019-10-05 23:28 273Kafka Cluster 2019(6) 3 Nodes C ... -
K8S Helm(1)Understand YAML and Kubectl Pod and Deployment
2019-10-01 01:21 316K8S Helm(1)Understand YAML and ... -
Rancher and k8s 2019(5)Private Registry
2019-09-27 03:25 352Rancher and k8s 2019(5)Private ... -
Jenkins 2019 Cluster(1)Version 2.194
2019-09-12 02:53 438Jenkins 2019 Cluster(1)Version ... -
Redis Cluster 2019(3)Redis Cluster on CentOS
2019-08-17 04:07 365Redis Cluster 2019(3)Redis Clus ...
相关推荐
### Apache Commons Pool:Java对象池化技术详解 #### 一、引言 在现代软件开发过程中,尤其是在基于Java的企业级应用开发中,资源管理和优化变得尤为重要。创建和销毁对象的成本通常较高,特别是在高并发场景下,...
在Unity3D游戏开发中,"Object Pool"是一种优化技术,用于管理游戏对象的创建和销毁,以提高性能并减少垃圾回收(Garbage Collection)的压力。本Demo是针对Unity3D内置对象池的一个实例,通过对比使用和不使用对象...
对象池(Object Pool)是一种设计模式,用于管理对特定类型对象的创建和复用,以减少频繁创建和销毁对象带来的开销。在某些场景下,如游戏开发、数据库连接管理和线程池等,对象的创建和销毁过程可能非常耗时,这时...
《Apache Commons Pool 1.5.4:对象池化技术详解》 Apache Commons Pool 是一个广泛使用的开源组件,主要用于实现对象池化技术。版本号"pool-1.5.4"代表着这一组件的一个稳定版本,它在之前的基础上进行了优化和...
对象池化是一种优化资源管理的技术,通过预先创建并维护一组可重用对象,避免频繁地创建和销毁对象带来的性能开销。在Java中,commons-pool库为开发者提供了通用的对象池API,支持各种类型的对象池化需求。 1. **...
- **Poolable接口**:对象池化的基础,定义了对象在池中的生命周期方法,如`activateObject()`用于激活对象,`passivateObject()`用于准备对象返回到池中,`validateObject()`用于检查对象是否有效,以及`wrap()`...
在Java世界里,对象池化是一种优化资源管理的重要技术,通过对象池可以减少频繁创建和销毁对象带来的性能开销,提升系统效率。这里我们聚焦于其最新版本——`commons-pool2-2.10.0.jar`,深入探讨这个库的核心特性和...
对象池化是一种设计模式,它允许程序预先创建一组对象并保持它们处于就绪状态,以便在需要时快速分配,而不是每次需要时都创建新对象。这种技术在处理昂贵的对象创建或系统资源有限的情况下特别有用。 **对象池的...
### get-keypool Object Pool get-keypool使用单个 arity 函数来创建对象。 ( def pool ( get-keypool ( fn [k] ( str :heavy k)))) ( borrow pool :object ) ; => ":heavy:object" ; Return key and object ( ...
Apache Commons Pool2是一款开源的Java对象池库,它提供了对象池化的服务,能够有效地管理并复用对象,减少频繁创建和销毁对象带来的性能开销。"commons-pool2-2.4.3-bin.zip"是这个库的2.4.3版本的二进制发布包,...
### Java对象池技术的原理及其实现 #### 摘要 本文深入探讨了Java对象池技术的基本原理及其实现方式,旨在帮助读者理解如何利用对象池优化Java应用程序的性能。通过对对象池技术核心概念——缓存与共享的阐述,本文...
通过研究Apache Commons Pool2的源码,开发者不仅可以学习到对象池化的概念,还能了解到如何通过代码实现和优化这种技术。对于想要深入理解Java并发编程和资源管理的开发者来说,这是一个宝贵的参考资料。
这个库是Apache软件基金会开发的,版本号为2.9.0,它提供了高效的对象池化机制,有助于提高应用程序的性能和稳定性。 对象池化是一种设计模式,它通过预先创建一组对象并维护一个池来减少对象的创建和销毁次数。在`...
总结来说,Apache Commons DBCP 1.4和Apache Commons POOL 1.5.6是两个用于优化数据库访问效率的Java工具包,它们通过对象池化技术提高了系统性能和资源利用率。结合使用这两个组件,可以构建高效、稳定的数据库连接...
它为开发者提供了对象池化的基础设施,帮助开发者有效地管理资源,避免频繁创建和销毁对象带来的性能开销。在本篇文章中,我们将深入探讨Apache Commons Pool 1.5.4版本的源码,理解其核心原理和使用方法。 一、...
Apache Commons Pool 是一个开源项目,由Apache软件基金会维护,它提供了一个对象池化的实现,用于高效管理资源,如数据库连接、线程或者其他昂贵创建的对象。源码版本为1.5.5,是我们今天要深入探讨的重点。 ...
Apache Commons Pool2是一个Java对象池库,用于管理可重用对象。...通过对象池化,可以显著降低系统资源消耗,提高应用程序的运行效率。在使用DBCP进行数据库连接管理时,Apache Commons Pool2起着至关重要的作用。
总的来说,Unity3D的PoolManager是游戏开发中的一个高效工具,它通过对象复用和池化技术,帮助开发者优化资源管理,提升游戏运行效率,是每一个大型游戏项目不可或缺的一部分。理解并熟练运用PoolManager,将有助于...