springboot使用commons-pool2实现对象池
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
springboot使⽤commons-pool2实现对象池springboot使⽤commons-pool2实现对象池
⼀. 连接池概述
频繁的创建和销毁,会极⼤的降低系统的性能,⽽对象池会在初始化的时候会创建⼀定数量的对象,每次访问只需从对象池中获取对象,使⽤完毕后再放回对象池,并不是直接销毁,这样可以保证程序重复使⽤同⼀个对象⽽不需要每次访问都创建和销毁对象,从⽽提⾼系统性能。
⼆. commons-pool2介绍
加⼊pool2依赖:
<dependency>
<groupId>mons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.2</version>
</dependency>
pool2的组成
PooledObject(池化对象) PooledObjectFactory(对象⼯⼚) ObjectPool (对象池)
需要池化的对象
/**
* 需要池化的对象
*
* @author lz
* @date 2019/7/30
*/
public class TestObject {
private String name;
private boolean isActive;
public TestObject() {
}
public TestObject(String name) {
= name;
}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
public boolean isActive() {
return isActive;
}
public void setActive(boolean active) {
isActive = active;
}
public void destroy(){
}
}
TestObject ⼯⼚
在commons-pool2中有两种⼯⼚:PooledObjectFactory 和KeyedPooledObjectFactory,我们使⽤前者。
public interface PooledObjectFactory<T> {
//创建对象
PooledObject<T> makeObject();
//激活对象
void activateObject(PooledObject<T> obj);
//钝化对象
void passivateObject(PooledObject<T> obj);
//验证对象
boolean validateObject(PooledObject<T> obj);
//销毁对象
void destroyObject(PooledObject<T> obj);
}
创建TestObjectFactory只需要继承BasePooledObjectFactory这个抽象类 ,⽽它则实现了PooledObjectFactory,也可以直接实现PooledObjectFactory接⼝;我们使⽤实现PooledObjectFactory接⼝:
import mons.pool2.PooledObject;
import mons.pool2.PooledObjectFactory;
import mons.pool2.impl.DefaultPooledObject;
/**
* TestObject ⼯⼚类
*
* @author lz
* @date 2019/7/30
*/
public class TestObjectFactory implements PooledObjectFactory<TestObject> {
/**
* /构造⼀个封装对象
*
* @return
* @throws Exception
*/
@Override
public PooledObject<TestObject> makeObject() throws Exception {
return new DefaultPooledObject<>(new TestObject());
}
/**
* 销毁对象
*
* @param p
* @throws Exception
*/
@Override
public void destroyObject(PooledObject<TestObject> p) throws Exception {
p.getObject().destroy();
}
/**
* 验证对象是否可⽤
*
* @param p
* @return
*/
@Override
public boolean validateObject(PooledObject<TestObject> p) {
return p.getObject().isActive();
}
/**
* 激活⼀个对象,使其可⽤⽤
*
* @param p
* @throws Exception
*/
@Override
public void activateObject(PooledObject<TestObject> p) throws Exception {
p.getObject().setActive(true);
}
/**
* 钝化⼀个对象,也可以理解为反初始化
*
* @param p
* @throws Exception
*/
@Override
public void passivateObject(PooledObject<TestObject> p) throws Exception {
}
}
创建⼀个对象池
import mons.pool2.PooledObjectFactory;
import mons.pool2.impl.AbandonedConfig;
import mons.pool2.impl.GenericObjectPool;
import mons.pool2.impl.GenericObjectPoolConfig;
/**
* ⾃定义对象池
*
* @author lz
* @date 2019/7/30
*/
public class TestObjectPool extends GenericObjectPool<TestObject> {
public TestObjectPool(PooledObjectFactory<TestObject> factory) {
super(factory);
}
public TestObjectPool(PooledObjectFactory<TestObject> factory, GenericObjectPoolConfig<TestObject> config) {
super(factory, config);
}
public TestObjectPool(PooledObjectFactory<TestObject> factory, GenericObjectPoolConfig<TestObject> config, AbandonedConfig abandonedConfig) { super(factory, config, abandonedConfig);
}
}
spring 部分
创建对象池配置类
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 对象池配置
*
* @author lz
* @date 2019/7/30
*/
@ConfigurationProperties(prefix = PoolProperties.PROJECT_PREFIX)
public class PoolProperties {
public static final String PROJECT_PREFIX = "project.object";
/**
* 最⼤空闲
*/
private int maxIdle = 5;
/**
* 最⼤总数
*/
private int maxTotal = 20;
/**
* 最⼩空闲
*/
private int minIdle = 2;
/**
* 初始化连接数
*/
private int initialSize = 3;
public int getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
}
public int getMaxTotal() {
return maxTotal;
}
public void setMaxTotal(int maxTotal) {
this.maxTotal = maxTotal;
}
public int getMinIdle() {
return minIdle;
}
public void setMinIdle(int minIdle) {
this.minIdle = minIdle;
}
public int getInitialSize() {
return initialSize;
}
public void setInitialSize(int initialSize) {
this.initialSize = initialSize;
}
}
创建⾃动配置类
import mons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PreDestroy;
/**
* 对象池⾃动装配
*
* @author lz
* @date 2019/7/30
*/
@EnableConfigurationProperties(PoolProperties.class)
@Configuration
public class PoolAutoConfiguration {
private final PoolProperties poolProperties;
private TestObjectPool pool;
@Autowired
public PoolAutoConfiguration(PoolProperties poolProperties) {
this.poolProperties = poolProperties;
}
@ConditionalOnClass({TestObjectFactory.class})
@Bean
protected TestObjectPool faceSDKPool() {
TestObjectFactory faceSDKFactory = new TestObjectFactory();
//设置对象池的相关参数
GenericObjectPoolConfig<TestObject> poolConfig = new GenericObjectPoolConfig<>(); poolConfig.setMaxIdle(poolProperties.getMaxIdle());
poolConfig.setMaxTotal(poolProperties.getMaxTotal());
poolConfig.setMinIdle(poolProperties.getMinIdle());
poolConfig.setBlockWhenExhausted(true);
poolConfig.setTestOnBorrow(true);
poolConfig.setTestOnReturn(true);
poolConfig.setTestWhileIdle(true);
poolConfig.setTimeBetweenEvictionRunsMillis(1000 * 60 * 30);
//⼀定要关闭jmx,不然springboot启动会报已经注册了某个jmx的错误
poolConfig.setJmxEnabled(false);
//新建⼀个对象池,传⼊对象⼯⼚和配置
pool = new TestObjectPool(faceSDKFactory, poolConfig);
initPool(poolProperties.getInitialSize(), poolProperties.getMaxIdle());
return pool;
}
/**
* 预先加载testObject对象到对象池中
*
* @param initialSize 初始化连接数
* @param maxIdle 最⼤空闲连接数
*/
private void initPool(int initialSize, int maxIdle) { if (initialSize <= 0) {
return;
}
int size = Math.min(initialSize, maxIdle);
for (int i = 0; i < size; i++) {
try {
pool.addObject();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
@PreDestroy
public void destroy() {
if (pool != null) {
pool.close();
}
}
}
使⽤
@Autowired
private TestObjectPool testObjectPool;
public void test() {
TestObject testObject = null;
try {
testObject = testObjectPool.borrowObject(); //省略业务代码...
} catch (Exception e) {
e.printStackTrace();
} finally {
if (testObject != null) {
//最终归还对象到对象池
testObjectPool.returnObject(testObject); }
}
}
转载:。