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

com.github.yingzhuo.fastdfs.springboot.domain.conn.AbstractConnectionManager Maven / Gradle / Ivy

The newest version!
package com.github.yingzhuo.fastdfs.springboot.domain.conn;

import com.github.yingzhuo.fastdfs.springboot.domain.proto.Command;
import com.github.yingzhuo.fastdfs.springboot.exception.FastDFSException;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;

/**
 * @author 应卓
 */
@Slf4j
abstract class AbstractConnectionManager {

    private final ConnectionPool pool;

    public AbstractConnectionManager(ConnectionPool pool) {
        this.pool = pool;
    }

    protected final  T doExecute(InetSocketAddress address, Connection conn, Command command) {
        boolean isException = false;
        try {
            return command.execute(conn);
        } catch (FastDFSException e) {
            isException = true;
            throw e;
        } catch (Exception e) {
            isException = true;
            throw new RuntimeException("execute fdfs command error", e);
        } finally {
            if (isException) {
                removeConnect(address, conn);
            } else {
                returnConnect(address, conn);
            }
        }
    }

    private void removeConnect(InetSocketAddress address, Connection conn) {
        try {
            if (null != conn) {
                //移除pool
                pool.invalidateObject(address, conn);
            }
        } catch (Exception e) {
            log.warn("remove pooled connection error", e);
        }
    }

    private void returnConnect(InetSocketAddress address, Connection conn) {
        try {
            if (null != conn) {
                pool.returnObject(address, conn);
            }
        } catch (Exception e) {
            log.error("return pooled connection error", e);
        }
    }

    protected Connection getConnection(InetSocketAddress address) {
        final Connection conn;
        try {
            // 获取连接
            conn = pool.borrowObject(address);
            //dumpPoolInfo(address);
        } catch (FastDFSException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Unable to borrow buffer from pool", e);
        }
        return conn;
    }

    public final void dumpPoolInfo(InetSocketAddress address) {

        if (log.isDebugEnabled()) {
            log.debug("===============================");
            log.debug("Address={}", address);
            log.debug("连接池最大连接数配置{}", pool.getMaxTotal());
            log.debug("每个Key最大连接数配置{}", pool.getMaxTotalPerKey());
            log.debug("每个key对应连接池最大空闲连接数{}", pool.getMaxIdlePerKey());
            log.debug("每个key对应连接池最小空闲连接数{}", pool.getMinIdlePerKey());
            log.debug("活动连接{}", pool.getNumActive(address));
            log.debug("空闲连接{}", pool.getNumIdle(address));
            log.debug("获取前测试连接状态{}", pool.getTestOnBorrow());
            log.debug("归还前测试连接状态{}", pool.getTestOnReturn());
            log.debug("空闲时测试连接状态{}", pool.getTestWhileIdle());
            log.debug("连接获取总数统计{}", pool.getBorrowedCount());
            log.debug("连接返回总数统计{}", pool.getReturnedCount());
            log.debug("连接销毁总数统计{}", pool.getDestroyedCount());
            log.debug("JmxName={}", pool.getJmxName());
            log.debug("===============================");
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy