com.github.tobato.fastdfs.conn.PooledConnectionFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fastdfs-client Show documentation
Show all versions of fastdfs-client Show documentation
基于yuqih发布的代码与fastdfs-client 官方1.26版本的重构
package com.github.tobato.fastdfs.conn;
import java.net.InetSocketAddress;
import java.nio.charset.Charset;
import org.apache.commons.pool2.BaseKeyedPooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import com.github.tobato.fastdfs.FdfsClientConstants;
/**
* pooled FdfsSocket factory
*
*
* 定义了被池化的对象的创建,初始化,激活,钝化以及销毁功能
*
*
* @author tobato
*
*/
@Component
@ConfigurationProperties(prefix = FdfsClientConstants.ROOT_CONFIG_PREFIX)
public class PooledConnectionFactory extends BaseKeyedPooledObjectFactory {
/** 读取时间 */
private int soTimeout;
/** 连接超时时间 */
private int connectTimeout;
/** 字符集 */
private Charset charset;
/** 默认字符集 */
private static final String DEFAULT_CHARSET_NAME = "UTF-8";
/** 设置默认字符集 */
private String charsetName = DEFAULT_CHARSET_NAME;
/**
* 创建连接
*/
@Override
public Connection create(InetSocketAddress address) throws Exception {
// 初始化字符集
if (null == charset) {
charset = Charset.forName(charsetName);
}
return new DefaultConnection(address, soTimeout, connectTimeout, charset);
}
/**
* 将对象池化pooledObject
*/
@Override
public PooledObject wrap(Connection conn) {
return new DefaultPooledObject(conn);
}
public int getSoTimeout() {
return soTimeout;
}
public void setSoTimeout(int soTimeout) {
this.soTimeout = soTimeout;
}
public int getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
public Charset getCharset() {
return charset;
}
@Override
public void destroyObject(InetSocketAddress key, PooledObject p) throws Exception {
p.getObject().close();
}
@Override
public boolean validateObject(InetSocketAddress key, PooledObject p) {
return p.getObject().isValid();
}
public void setCharsetName(String charsetName) {
this.charsetName = charsetName;
}
}