对象池commons-pool2

1.序言

对象池就是存访对象的池,跟线程池,数据库连接池等一样,典型的池化设计思想

对象池的优点:

1.集中管理池中对象,

2.减少频繁创建和销毁长期使用的对象,提升复用性,以节约资源的消耗

3.可以有效避免频繁为对象分配内存和释放堆中内存,减轻jvm垃圾收集器的负担

common-pocol2 是Apache提供的一个通用对象池技术实现,可以方便定制化自己需要的对象池。

先说怎么用?

这里以fastdfs举例,fastdfs对象是典型线程不安全,底层是socket连接,每使用一次就new一个socket对象,完成后关闭对象。

2.pom.xml引入

<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-pool2</artifactId>
	<version>2.11.1</version>
</dependency>

3.构建要池化的对象,对象池,对象工厂

需要池化的对象FastdfsClient

import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerServer;

/**
 * 封装自建的fastdfs对象
 */
public class FastdfsClient extends StorageClient1 {
    private String name;
    private boolean active;

    public FastdfsClient(){
        super();
    }

    public FastdfsClient(TrackerServer trackerServer, StorageServer storageServer){
        super(trackerServer, storageServer);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public void destroy(){
        this.active = false;
    }

    public void active(){
        this.active = true;
    }
}

对象池FastDFSClientPool

import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.AbandonedConfig;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

public class FastDFSClientPool extends GenericObjectPool<FastdfsClient> {
    public FastDFSClientPool(PooledObjectFactory<FastdfsClient> factory) {
        super(factory);
    }

    public FastDFSClientPool(PooledObjectFactory<FastdfsClient> factory, GenericObjectPoolConfig<FastdfsClient> config) {
        super(factory, config);
    }

    public FastDFSClientPool(PooledObjectFactory<FastdfsClient> factory, GenericObjectPoolConfig<FastdfsClient> config, AbandonedConfig abandonedConfig) {
        super(factory, config, abandonedConfig);
    }
}

对象工厂FastdfsClientFactory,用来激活,销毁,构建,钝化,校验对象的地方

import com.achi.common.model.ExceptionCode;
import com.achi.common.service.ConfigService;
import com.achi.core.exception.ServiceException;
import com.achi.core.utils.RandomGUID;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.log4j.Logger;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class FastdfsClientFactory implements PooledObjectFactory<FastdfsClient> {
    protected final static Logger log = Logger.getLogger(FastdfsClientFactory.class);

    public List<String> uriList = new ArrayList<>();
    public static final String PRE = "fastdfs.client_";

    @Autowired
    private ConfigService configService;

    /**
     * 激活对象,使其可用
     * @param pooledObject
     * @throws Exception
     */
    @Override
    public void activateObject(PooledObject<FastdfsClient> pooledObject) throws Exception {
        FastdfsClient fastDFSClient = pooledObject.getObject();
//        log.info(fastDFSClient.getName() + ",激活对象,使其可用");
        fastDFSClient.active();
    }

    /**
     * 销毁对象
     * @param pooledObject
     * @throws Exception
     */
    @Override
    public void destroyObject(PooledObject<FastdfsClient> pooledObject) throws Exception {
        FastdfsClient fastDFSClient = pooledObject.getObject();
        log.info(fastDFSClient.getName() + ",销毁对象");
        fastDFSClient.destroy();
    }

    /**
     * 构建对象
     * @return
     * @throws Exception
     */
    @Override
    public PooledObject<FastdfsClient> makeObject() throws Exception {
        FastdfsClient fastDFSClient = getFastdfsClient();
        if(fastDFSClient == null){
            throw new ServiceException(ExceptionCode.MESSAGE, "create FastdfsClient error.");
        }
        log.info(fastDFSClient.getName() + ",销毁对象");
        return new DefaultPooledObject<>(fastDFSClient);
    }

    /**
     * 钝化对象,反初始化 此"对象"暂且需要"休息"一下
     * @param pooledObject
     * @throws Exception
     */
    @Override
    public void passivateObject(PooledObject<FastdfsClient> pooledObject) throws Exception {
//        log.info(pooledObject.getObject().getName() + ",钝化一个对象");
    }

    /**
     * 验证对象是否可用
     * @param pooledObject
     * @return
     */
    @Override
    public boolean validateObject(PooledObject<FastdfsClient> pooledObject) {
        FastdfsClient fastDFSClient = pooledObject.getObject();
        return fastDFSClient.isActive();
    }

    private void init() throws ServiceException {
        Map<String, Object> configMap = configService.getRootConfig();
        if (configMap == null) {
            return;
        }
        Map<String, Object> fdfsMap = (Map<String, Object>)configMap.get("fdfs");
        if (fdfsMap == null) {
            return;
        }
        uriList = (List<String>)fdfsMap.get("uri");
        if (uriList == null) {
            throw new ServiceException(ExceptionCode.NULL_PARAMETER, "No uri of fdfs in Config.");
        }
    }

    private FastdfsClient getFastdfsClient() throws ServiceException{
        FastdfsClient fastdfsClient = null;
        try {
            if(uriList == null || uriList.isEmpty()){
                init();
            }
            ClientGlobal.init(getClass().getResource("/fdfs_client.conf").getPath());
            InetSocketAddress[] trackerServers = new InetSocketAddress[uriList.size()];
            for (int i = 0; i < uriList.size(); i++) {
                String[] address = uriList.get(i).split(":");
                trackerServers[i] = new InetSocketAddress(address[0].trim(), Integer.parseInt(address[1].trim()));
            }
            ClientGlobal.setG_tracker_group(new TrackerGroup(trackerServers));
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();
            if (trackerServer == null) {
                return null;
            }
            ProtoCommon.activeTest(trackerServer.getSocket());
            StorageServer storageServer = null;
            fastdfsClient = new FastdfsClient(trackerServer, storageServer);
            fastdfsClient.setName(PRE + new RandomGUID());
            fastdfsClient.active();
        } catch (IOException | MyException e) {
            log.error(e.getMessage(), e);
        }
        return fastdfsClient;
    }


}

4.与spring集成,构建对象池bean

import com.achi.config.fastdfs.FastDFSClientPool;
import com.achi.config.fastdfs.FastdfsClient;
import com.achi.config.fastdfs.FastdfsClientFactory;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PreDestroy;
import java.util.concurrent.TimeUnit;

@Configuration
public class FastdfsPoolConfiguration {

    private FastDFSClientPool fastDFSClientPool;

    @Autowired
    private FastdfsClientFactory fastdfsClientFactory;

    /**
     * maxActive: 链接池中最大连接数,默认为8.
     * maxIdle: 链接池中最大空闲的连接数,默认为8.
     * minIdle: 连接池中最少空闲的连接数,默认为0.
     * maxWait: 当连接池资源耗尽时,调用者最大阻塞的时间,超时将跑出异常。单位,毫秒数;默认为-1.表示永不超时.
     * minEvictableIdleTimeMillis: 连接空闲的最小时间,达到此值后空闲连接将可能会被移除。负值(-1)表示不移除。
     * softMinEvictableIdleTimeMillis: 连接空闲的最小时间,达到此值后空闲链接将会被移除,且保留“minIdle”个空闲连接数。默认为-1.
     * numTestsPerEvictionRun: 对于“空闲链接”检测线程而言,每次检测的链接资源的个数。默认为3.
     * testOnBorrow: 向调用者输出“链接”资源时,是否检测是有有效,如果无效则从连接池中移除,并尝试获取继续获取。默认为false。建议保持默认值.
     * testOnReturn:  向连接池“归还”链接时,是否检测“链接”对象的有效性。默认为false。建议保持默认值.
     * testWhileIdle:  向调用者输出“链接”对象时,是否检测它的空闲超时;默认为false。如果“链接”空闲超时,将会被移除。建议保持默认值.
     * timeBetweenEvictionRunsMillis:  “空闲链接”检测线程,检测的周期,毫秒数。如果为负值,表示不运行“检测线程”。默认为-1.
     *  whenExhaustedAction: 当“连接池”中active数量达到阀值时,即“链接”资源耗尽时,连接池需要采取的手段, 默认为1:
     *  -> 0 : 抛出异常,
     *  -> 1 : 阻塞,直到有可用链接资源
     *  -> 2 : 强制创建新的链接资源
     * @return
     */
    @Bean
    protected FastDFSClientPool fastDFSClientPool(){
        GenericObjectPoolConfig<FastdfsClient> config = new GenericObjectPoolConfig<>();
        //链接池中最大连接数,默认为8
        config.setMaxTotal(8);
        //链接池中最大空闲的连接数,默认也为8
        config.setMaxIdle(8);
        //连接池中最少空闲的连接数,默认为0
        config.setMinIdle(0);
        //当这个值为true的时候,maxWaitMillis参数才能生效。为false的时候,当连接池没资源,则立马抛异常。默认为true
        config.setBlockWhenExhausted(true);
        //默认false,borrow的时候检测是有有效,如果无效则从连接池中移除,并尝试继续获取
        config.setTestOnBorrow(true);
        //默认false,return的时候检测是有有效,如果无效则从连接池中移除,并尝试继续获取
        config.setTestOnReturn(true);
        //默认false,在evictor线程里头,当evictionPolicy.evict方法返回false时,而且testWhileIdle为true的时候则检测是否有效,如果无效则移除
        config.setTestWhileIdle(true);
        //一定要关闭jmx,不然启动会报已经注册了某个jmx的错误
        config.setJmxEnabled(false);

        fastDFSClientPool = new FastDFSClientPool(fastdfsClientFactory, config);
        return fastDFSClientPool;
    }

    @PreDestroy
    public void destroy(){
        System.out.println("对象池关闭-start");
        if( null != fastDFSClientPool ){ fastDFSClientPool.close(); }
        System.out.println("对象池关闭-end");
    }
}

5.对象池的使用方式

引入对象池,完成上传和下载

@Autowired
private FastDFSClientPool fastDFSClientPool;

/**
 * 上传,使用对象池化模式,解决fastdfs不能多线程并发的问题
 * @param group_name
 * @param local_filename
 * @return
 */
public String uploadWrapper(String group_name, String local_filename) {
	log.info("Upload FastDFS file - " + group_name + " - " + local_filename);
	FastdfsClient fastdfsClient = null;
	String uploadResult = null;
	try {
		fastdfsClient = fastDFSClientPool.borrowObject();
		if (fastdfsClient == null) {
			log.error("uploadWrapper FastdfsClient get error");
			return null;
		}
		uploadResult = fastdfsClient.upload_file1(group_name, local_filename, null, null);
	} catch (Exception e) {
		log.error(e.getMessage(), e);
	} finally {
		if(fastdfsClient != null){
			fastDFSClientPool.returnObject(fastdfsClient);
		}
	}
	return uploadResult;
}

/**
 * 下载,使用对象池化模式,解决fastdfs不能多线程并发的问题
 * @param filePath
 * @return
 */
public byte[] downloadWrapper(String filePath) {
	log.info("Download FastDFS file - " + filePath);
	FastdfsClient fastdfsClient = null;
	byte[] fileContent = null;
	try {
		fastdfsClient = fastDFSClientPool.borrowObject();
		if (fastdfsClient == null) {
			log.error("downloadWrapper FastdfsClient get error");
			return null;
		}
		fileContent = fastdfsClient.download_file1(filePath);
	} catch (Exception e) {
		log.error(e.getMessage(), e);
	} finally {
		if(fastdfsClient != null){
			fastDFSClientPool.returnObject(fastdfsClient);
		}
	}
	return fileContent;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彼岸花@开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值