利用Apache?Common將java對(duì)象池化的問題
什么是對(duì)象池化?
對(duì)象被創(chuàng)建后,使用完畢不是立即銷毀回收對(duì)象,而是將對(duì)象放到一個(gè)容器保存起來,下次使用的時(shí)候不用創(chuàng)建對(duì)象,而是從容器中直接獲取。
什么樣的對(duì)象需要池化?
一般需要池化的對(duì)象往往都是比"重量級(jí)"較的對(duì)象,創(chuàng)建和銷毀都比較耗時(shí),比如我們的"線程","數(shù)據(jù)庫鏈接對(duì)象","tcp鏈接對(duì)象", "FTP鏈接對(duì)象" 等等。
對(duì)象池化的好處?
這些對(duì)象池化后,之后使用的時(shí)候不用創(chuàng)建,直接使用即可,可以大大縮短程序的運(yùn)行時(shí)間,以及創(chuàng)建對(duì)象時(shí)對(duì)CPU資源的消耗,以及對(duì)系統(tǒng)資源的控制(池化的對(duì)象數(shù)量有限,不會(huì)一直創(chuàng)建對(duì)象,導(dǎo)致系統(tǒng)資源耗盡,或者造成程序OOM的情況)進(jìn)而提高系統(tǒng)的穩(wěn)定性。
對(duì)象池化后需要注意什么?
這些被池化的對(duì)象都有一個(gè)特點(diǎn),都是"活的",比如數(shù)據(jù)庫鏈接對(duì)象內(nèi)部一般保存了一個(gè)TCP鏈接,所以,這個(gè)對(duì)象"能用"的前提是這個(gè)TCP鏈接是有效的,線程對(duì)象"能用"的前提是線程的狀態(tài)不是"凋亡"狀態(tài),所以我們有必要定期對(duì)對(duì)象的"健康狀態(tài)"進(jìn)行檢查,剔除掉"不能用"的對(duì)象,并填充新的對(duì)象給"對(duì)象池"。
使用apache-common-pool池化對(duì)象
- 引入依賴
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.9.0</version>
</dependency>- 需要池化的對(duì)象示例
public class Foo {
private final String username;
public Foo(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
}- 構(gòu)建對(duì)象創(chuàng)建工廠
可以直接實(shí)現(xiàn)org.apache.commons.pool2.PooledObjectFactory<T>接口實(shí)現(xiàn)創(chuàng)建、銷毀、鈍化、取消等接口,也可以使用他的抽象類,實(shí)現(xiàn)創(chuàng)建和包裝方法即可。
public class FooPoolObjectFactory extends BasePooledObjectFactory<Foo> {
@Override
public Foo create() throws Exception {
return new Foo(String.valueOf(RandomUtils.randomInt(0, 10)));
}
@Override
public PooledObject<Foo> wrap(Foo obj) {
return new DefaultPooledObject<>(obj);
}
}- 實(shí)現(xiàn)驅(qū)逐策略
一般數(shù)據(jù)庫鏈接對(duì)象,要定期進(jìn)行心跳,確保鏈接可用,如果鏈接斷開,需要銷毀對(duì)象,并重新創(chuàng)建新的對(duì)象。common-pool中,我們可以實(shí)現(xiàn)驅(qū)逐策略,對(duì)對(duì)象進(jìn)行定期檢查
public class FooEvictionPolicy implements EvictionPolicy<Foo> {
@Override
public boolean evict(EvictionConfig config, PooledObject<Foo> underTest, int idleCount) {
// todo 定期檢查對(duì)象某些功能是否可用
return true;
}
}- 構(gòu)建&配置對(duì)象池
public GenericObjectPool<Foo> fooGenericObjectPool() {
GenericObjectPoolConfig<Foo> poolConfig = new GenericObjectPoolConfig<>();
poolConfig.setEvictionPolicy(new FooEvictionPolicy());
poolConfig.setBlockWhenExhausted(true);
poolConfig.setJmxEnabled(false);
poolConfig.setMaxWaitMillis(1000 * 10);
poolConfig.setTimeBetweenEvictionRunsMillis(60 * 1000);
poolConfig.setMinEvictableIdleTimeMillis(20 * 1000);
poolConfig.setTestWhileIdle(true);
poolConfig.setTestOnReturn(true);
poolConfig.setTestOnBorrow(true);
poolConfig.setMaxTotal(3);
// 設(shè)置拋棄策略
AbandonedConfig abandonedConfig = new AbandonedConfig();
abandonedConfig.setRemoveAbandonedOnMaintenance(true);
abandonedConfig.setRemoveAbandonedOnBorrow(true);
return new GenericObjectPool<>(new FooPoolObjectFactory(), poolConfig, abandonedConfig);
}如果我們使用的是spring容器,一般我們需要將該對(duì)象交由spring管理。
- 獲取&歸還對(duì)象
private final GenericObjectPool<Foo> fooGenericObjectPool = fooGenericObjectPool();
public Foo borrowFoo () throws Exception {
return fooGenericObjectPool.borrowObject();
}
public void returnObject(Foo foo){
fooGenericObjectPool.returnObject(foo);
}到此這篇關(guān)于利用Apache Common將java對(duì)象“池化”的文章就介紹到這了,更多相關(guān)Apache Common java對(duì)象池化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux下的mongodb服務(wù)監(jiān)視腳本(啟動(dòng)服務(wù))
這篇文章主要介紹了Linux下的mongodb服務(wù)監(jiān)視腳本(啟動(dòng)服務(wù)),需要的朋友可以參考下2015-10-10
apache密碼生成工具h(yuǎn)tpasswd使用詳解
本文主要介紹了1、 htpasswd的作用與安裝,2、 htpasswd命令詳解,3、 htpasswd的實(shí)例,4、 htpasswd的應(yīng)用,有需要的小伙伴參考下2015-01-01
Linux系統(tǒng)網(wǎng)卡設(shè)置教程
這篇文章主要介紹了Linux系統(tǒng)網(wǎng)卡的設(shè)置教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
淺談Linux下修改/設(shè)置環(huán)境變量JAVA_HOME的方法
這篇文章主要介紹了淺談Linux下修改/設(shè)置環(huán)境變量JAVA_HOME的方法,環(huán)境變量一般是指在操作系統(tǒng)中用來指定操作系統(tǒng)運(yùn)行環(huán)境的一些參數(shù)。環(huán)境變量是在操作系統(tǒng)中一個(gè)具有特定名字的對(duì)象,它包含了一個(gè)或者多個(gè)應(yīng)用程序所將使用到的信息。感興趣的可以了解一下2020-07-07

