java对象池commons-pool-1.6详解(一)

自己的项目中用到了 对象池 commons-pool:

package com.sankuai.qcs.regulation.protocol.client; import com.dianping.cat.Cat; import com.dianping.cat.message.Transaction; import com.sankuai.qcs.regulation.constant.CatConstant; import com.sankuai.qcs.regulation.exception.QcsSysErrorEnum; import com.sankuai.qcs.regulation.exception.QcsSysException; import lombok.Data; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPSClient; import org.apache.commons.pool2.PooledObject; import org.apache.commons.pool2.PooledObjectFactory; import org.apache.commons.pool2.impl.DefaultPooledObject; import org.apache.commons.pool2.impl.GenericObjectPool; import org.apache.commons.pool2.impl.GenericObjectPoolConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.ByteArrayInputStream; import java.io.IOException; public class PoolingFTPSClient implements FileClient { private static final Logger LOGGER = LoggerFactory.getLogger(PoolingFTPSClient.class); private Config config; private GenericObjectPool<FTPSClient> ftpsClientPool; public PoolingFTPSClient(Config config) { this.config = config; ftpsClientPool = new GenericObjectPool<>(new FTPSClientFactory(config), config.poolConfig); } @Override public void storeFile(String fileName, byte[] data) throws Exception { Transaction transaction = Cat.newTransaction(CatConstant.TRANSACTION_FTPS_REQUEST.getKey(), CatConstant.TRANSACTION_FTPS_REQUEST.getValue()); FTPSClient client = null; try { client = ftpsClientPool.borrowObject(); if (!client.storeFile(fileName, new ByteArrayInputStream(data))) { String errorInfo = String.format("上传文件:%s 到%s失败.", fileName, config.getName()); LOGGER.error(errorInfo); throw QcsSysErrorEnum.SYS_ERROR.formException(errorInfo); } if (client.rename(fileName, String.format("%s/%s", config.getFinalFileDir(), fileName))) { String errorInfo = String.format("%s文件:%s 移动操作失败.", config.getName(), fileName); LOGGER.error(errorInfo); throw QcsSysErrorEnum.SYS_ERROR.formException(errorInfo); } transaction.setSuccessStatus(); } catch (Exception e) { transaction.setStatus(e); LOGGER.error("PoolingFTPSClient.storeFile error.(config:{},fileName:{})", config.toString(), fileName, e); throw e; } finally { if (client != null) { ftpsClientPool.returnObject(client); } transaction.complete(); } } public void destroy() { if (!ftpsClientPool.isClosed()) { ftpsClientPool.close(); } } private static final class FTPSClientFactory implements PooledObjectFactory<FTPSClient> { private Config config; FTPSClientFactory(Config config) { this.config = config; } @Override public PooledObject<FTPSClient> makeObject() throws Exception { FTPSClient client = new FTPSClient(); client.setControlEncoding("utf-8"); try { client.connect(config.serverIp, config.serverPort); boolean isOk = client.login(config.user, config.pwd); if (!isOk) { String errorInfo = String.format("ftps服务登录失败:config:%s", config); LOGGER.error(errorInfo); throw QcsSysErrorEnum.SYS_ERROR.formException(errorInfo); } if (!client.changeWorkingDirectory(config.tempFileDir)) { client.makeDirectory(config.tempFileDir); if (!client.changeWorkingDirectory(config.tempFileDir)) { String errorInfo = String.format("切换%s的工作目录失败", config.getName()); LOGGER.error(errorInfo); throw QcsSysErrorEnum.SYS_ERROR.formException(errorInfo); } } client.setFileType(FTP.BINARY_FILE_TYPE); return new DefaultPooledObject(client); } catch (Exception e) { client.disconnect(); if (e instanceof QcsSysException) { throw e; } else { throw QcsSysErrorEnum.SYS_ERROR.formException(String.format("构造FTPSClient异常,errorInfo:%s,config:%s", e.getMessage(), config)); } } } @Override public void destroyObject(PooledObject<FTPSClient> pooledObject) throws Exception { FTPSClient client = pooledObject.getObject(); if (client.isConnected()) { /* 不需要再调用 退出 指令 */ client.disconnect(); } LOGGER.warn("{}的一个ftps连接已经关闭.", config.getName()); } @Override public boolean validateObject(PooledObject<FTPSClient> pooledObject) { FTPSClient client = pooledObject.getObject(); try { if (client.sendNoOp()) { return true; } } catch (IOException e) { LOGGER.warn("{}的一个ftps连接验证失败.", config.getName(), e); return false; } return false; } @Override public void activateObject(PooledObject<FTPSClient> pooledObject) { //TODO:待定 } @Override public void passivateObject(PooledObject<FTPSClient> pooledObject) { //TODO:待定 } } @Data public static final class Config { private String name; private String serverIp; private int serverPort; private String user; private String pwd; private String tempFileDir; private String finalFileDir; private GenericObjectPoolConfig poolConfig; } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgfdgw.html