All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.luhuiguo.fastdfs.conn.PooledConnectionFactory Maven / Gradle / Ivy

The newest version!
package com.luhuiguo.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;

/**
 * pooled FdfsSocket factory
 * 
 * 
 * 定义了被池化的对象的创建,初始化,激活,钝化以及销毁功能
 * 
 * 
 * @author luhuiguo
 *
 */
public class PooledConnectionFactory extends BaseKeyedPooledObjectFactory {

    /** 读取时间 */
    private int soTimeout = 1000;
    /** 连接超时时间 */
    private int connectTimeout = 1000;
    /** 字符集 */
    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;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy