Raod пре 4 година
родитељ
комит
01923ae1f4

+ 0 - 71
report-core/src/main/java/com/anjiplus/template/gaea/business/modules/dataSource/pool/constant/PooledConst.java

@@ -1,71 +0,0 @@
-package com.anjiplus.template.gaea.business.modules.dataSource.pool.constant;
-
-/**
- * 线程池常量
- * @since 1.1.0
- */
-public final class PooledConst {
-
-    private PooledConst(){}
-
-    /**
-     * 默认的最小连接数
-     * @since 1.1.0
-     */
-    public static final int DEFAULT_MIN_SIZE = 10;
-
-    /**
-     * 默认最大的连接数
-     * @since 1.1.0
-     */
-    public static final int DEFAULT_MAX_SIZE = 300;
-
-    /**
-     * 默认最大的等待毫秒数
-     *
-     * 默认:1 min
-     *
-     * @since 1.3.0
-     */
-    public static final int DEFAULT_MAX_WAIT_MILLS = 60 * 1000;
-
-    /**
-     * 默认验证查询的语句
-     * @since 1.5.0
-     */
-    public static final String DEFAULT_VALID_QUERY = "select 1 from dual";
-
-    /**
-     * 默认的验证的超时时间
-     * @since 1.5.0
-     */
-    public static final int DEFAULT_VALID_TIME_OUT_SECONDS = 5;
-
-    /**
-     * 获取连接时,默认不校验
-     * @since 1.5.0
-     */
-    public static final boolean DEFAULT_TEST_ON_BORROW = false;
-
-
-    /**
-     * 归还连接时,默认不校验
-     * @since 1.5.0
-     */
-    public static final boolean DEFAULT_TEST_ON_RETURN = false;
-
-    /**
-     * 默认闲暇的时候,进行校验
-     *
-     * @since 1.5.0
-     */
-    public static final boolean DEFAULT_TEST_ON_IDLE = true;
-
-    /**
-     * 1min 自动校验一次
-     *
-     * @since 1.5.0
-     */
-    public static final long DEFAULT_TEST_ON_IDLE_INTERVAL_SECONDS = 60;
-
-}

+ 0 - 68
report-core/src/main/java/com/anjiplus/template/gaea/business/modules/dataSource/pool/datasource/AbstractDataSourceConfig.java

@@ -1,68 +0,0 @@
-package com.anjiplus.template.gaea.business.modules.dataSource.pool.datasource;
-
-/**
- * @author binbin.hou
- * @since 1.0.0
- */
-public class AbstractDataSourceConfig extends DataSourceConfigAdaptor {
-
-    /**
-     * 驱动类
-     * @since 1.0.0
-     */
-    protected String driverClass;
-
-    /**
-     * jdbc url
-     * @since 1.0.0
-     */
-    protected String jdbcUrl;
-
-    /**
-     * 用户
-     * @since 1.0.0
-     */
-    protected String user;
-
-    /**
-     * 密码
-     * @since 1.0.0
-     */
-    protected String password;
-
-    public String getDriverClass() {
-        return driverClass;
-    }
-
-    @Override
-    public void setDriverClass(String driverClass) {
-        this.driverClass = driverClass;
-    }
-
-    public String getJdbcUrl() {
-        return jdbcUrl;
-    }
-
-    @Override
-    public void setJdbcUrl(String jdbcUrl) {
-        this.jdbcUrl = jdbcUrl;
-    }
-
-    public String getUser() {
-        return user;
-    }
-
-    @Override
-    public void setUser(String user) {
-        this.user = user;
-    }
-
-    public String getPassword() {
-        return password;
-    }
-
-    @Override
-    public void setPassword(String password) {
-        this.password = password;
-    }
-}

+ 0 - 159
report-core/src/main/java/com/anjiplus/template/gaea/business/modules/dataSource/pool/datasource/AbstractPooledDataSourceConfig.java

@@ -1,159 +0,0 @@
-package com.anjiplus.template.gaea.business.modules.dataSource.pool.datasource;
-
-import com.anjiplus.template.gaea.business.modules.dataSource.pool.api.ILifeCycle;
-import com.anjiplus.template.gaea.business.modules.dataSource.pool.api.IPooledDataSourceConfig;
-import com.anjiplus.template.gaea.business.modules.dataSource.pool.constant.PooledConst;
-
-/**
- * @author binbin.hou
- * @since 1.1.0
- */
-public abstract class AbstractPooledDataSourceConfig extends AbstractDataSourceConfig
-        implements IPooledDataSourceConfig, ILifeCycle {
-
-    /**
-     * 最小尺寸
-     * @since 1.1.0
-     */
-    protected int minSize = PooledConst.DEFAULT_MIN_SIZE;
-
-    /**
-     * 最大尺寸
-     * @since 1.1.0
-     */
-    protected int maxSize = PooledConst.DEFAULT_MAX_SIZE;
-
-    /**
-     * 最大的等待时间
-     * @since 1.3.0
-     */
-    protected long maxWaitMills = PooledConst.DEFAULT_MAX_WAIT_MILLS;
-
-    /**
-     * 验证查询
-     * @since 1.5.0
-     */
-    protected String validQuery = PooledConst.DEFAULT_VALID_QUERY;
-
-    /**
-     * 验证的超时时间
-     * @since 1.5.0
-     */
-    protected int validTimeOutSeconds = PooledConst.DEFAULT_VALID_TIME_OUT_SECONDS;
-
-    /**
-     * 获取时验证
-     * @since 1.5.0
-     */
-    protected boolean testOnBorrow = PooledConst.DEFAULT_TEST_ON_BORROW;
-
-    /**
-     * 归还时验证
-     * @since 1.5.0
-     */
-    protected boolean testOnReturn = PooledConst.DEFAULT_TEST_ON_RETURN;
-
-    /**
-     * 闲暇时验证
-     * @since 1.5.0
-     */
-    protected boolean testOnIdle = PooledConst.DEFAULT_TEST_ON_IDLE;
-
-    /**
-     * 闲暇时验证的时间间隔
-     * @since 1.5.0
-     */
-    protected long testOnIdleIntervalSeconds = PooledConst.DEFAULT_TEST_ON_IDLE_INTERVAL_SECONDS;
-
-    public int getMinSize() {
-        return minSize;
-    }
-
-    @Override
-    public void setMinSize(int minSize) {
-        this.minSize = minSize;
-    }
-
-    public int getMaxSize() {
-        return maxSize;
-    }
-
-    @Override
-    public void setMaxSize(int maxSize) {
-        this.maxSize = maxSize;
-    }
-
-    public long getMaxWaitMills() {
-        return maxWaitMills;
-    }
-
-    @Override
-    public void setMaxWaitMills(long maxWaitMills) {
-        this.maxWaitMills = maxWaitMills;
-    }
-
-    public String getValidQuery() {
-        return validQuery;
-    }
-
-    @Override
-    public void setValidQuery(String validQuery) {
-        this.validQuery = validQuery;
-    }
-
-    public int getValidTimeOutSeconds() {
-        return validTimeOutSeconds;
-    }
-
-    @Override
-    public void setValidTimeOutSeconds(int validTimeOutSeconds) {
-        this.validTimeOutSeconds = validTimeOutSeconds;
-    }
-
-    public boolean isTestOnBorrow() {
-        return testOnBorrow;
-    }
-
-    @Override
-    public void setTestOnBorrow(boolean testOnBorrow) {
-        this.testOnBorrow = testOnBorrow;
-    }
-
-    public boolean isTestOnReturn() {
-        return testOnReturn;
-    }
-
-    @Override
-    public void setTestOnReturn(boolean testOnReturn) {
-        this.testOnReturn = testOnReturn;
-    }
-
-    public boolean isTestOnIdle() {
-        return testOnIdle;
-    }
-
-    @Override
-    public void setTestOnIdle(boolean testOnIdle) {
-        this.testOnIdle = testOnIdle;
-    }
-
-    public long getTestOnIdleIntervalSeconds() {
-        return testOnIdleIntervalSeconds;
-    }
-
-    @Override
-    public void setTestOnIdleIntervalSeconds(long testOnIdleIntervalSeconds) {
-        this.testOnIdleIntervalSeconds = testOnIdleIntervalSeconds;
-    }
-
-    @Override
-    public void init() {
-
-    }
-
-    @Override
-    public void destroy() {
-
-    }
-
-}

+ 0 - 82
report-core/src/main/java/com/anjiplus/template/gaea/business/modules/dataSource/pool/datasource/DataSourceConfigAdaptor.java

@@ -1,82 +0,0 @@
-package com.anjiplus.template.gaea.business.modules.dataSource.pool.datasource;
-
-import com.anjiplus.template.gaea.business.modules.dataSource.pool.api.IDataSourceConfig;
-
-import java.io.PrintWriter;
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.sql.SQLFeatureNotSupportedException;
-import java.util.logging.Logger;
-
-/**
- * @author binbin.hou
- * @since 1.0.0
- */
-public class DataSourceConfigAdaptor implements IDataSourceConfig {
-
-    @Override
-    public Connection getConnection() throws SQLException {
-        return null;
-    }
-
-    @Override
-    public Connection getConnection(String username, String password) throws SQLException {
-        return null;
-    }
-
-    @Override
-    public <T> T unwrap(Class<T> iface) throws SQLException {
-        return null;
-    }
-
-    @Override
-    public boolean isWrapperFor(Class<?> iface) throws SQLException {
-        return false;
-    }
-
-    @Override
-    public PrintWriter getLogWriter() throws SQLException {
-        return null;
-    }
-
-    @Override
-    public void setLogWriter(PrintWriter out) throws SQLException {
-
-    }
-
-    @Override
-    public void setLoginTimeout(int seconds) throws SQLException {
-
-    }
-
-    @Override
-    public int getLoginTimeout() throws SQLException {
-        return 0;
-    }
-
-    @Override
-    public Logger getParentLogger() throws SQLFeatureNotSupportedException {
-        return null;
-    }
-
-    @Override
-    public void setDriverClass(String driverClass) {
-
-    }
-
-    @Override
-    public void setJdbcUrl(String jdbcUrl) {
-
-    }
-
-    @Override
-    public void setUser(String user) {
-
-    }
-
-    @Override
-    public void setPassword(String password) {
-
-    }
-
-}

+ 0 - 240
report-core/src/main/java/com/anjiplus/template/gaea/business/modules/dataSource/pool/datasource/PooledDataSource.java

@@ -1,240 +0,0 @@
-package com.anjiplus.template.gaea.business.modules.dataSource.pool.datasource;
-
-import com.anjiplus.template.gaea.business.modules.dataSource.pool.connection.IPooledConnection;
-import com.anjiplus.template.gaea.business.modules.dataSource.pool.connection.PooledConnection;
-import com.anjiplus.template.gaea.business.modules.dataSource.pool.exception.JdbcPoolException;
-import com.anjiplus.template.gaea.business.modules.dataSource.pool.util.DriverClassUtil;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.StringUtils;
-
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.SQLException;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Optional;
-import java.util.concurrent.Executors;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
-
-/**
- * 池化的数据源
- *
- * @author binbin.hou
- * @since 1.1.0
- */
-@Slf4j
-public class PooledDataSource extends AbstractPooledDataSourceConfig {
-
-    /**
-     * 内置的队列
-     *
-     * @since 1.1.0
-     */
-    private List<IPooledConnection> pool = new ArrayList<>();
-
-    @Override
-    public synchronized void init() {
-        DriverClassUtil.loadDriverClass(super.driverClass, super.jdbcUrl);
-
-        this.initJdbcPool();
-
-        // 初始化 idle check
-        this.initTestOnIdle();
-    }
-
-    @Override
-    public synchronized Connection getConnection() throws SQLException {
-        //1. 获取第一个不是 busy 的连接
-        Optional<IPooledConnection> connectionOptional = getFreeConnectionFromPool();
-        if (connectionOptional.isPresent()) {
-            return connectionOptional.get();
-        }
-
-        //2. 考虑是否可以扩容
-        if (pool.size() >= maxSize) {
-            //2.1 立刻返回
-            if (maxWaitMills <= 0) {
-                throw new JdbcPoolException("Can't get connection from pool!");
-            }
-
-
-            //2.2 循环等待
-            final long startWaitMills = System.currentTimeMillis();
-            final long endWaitMills = startWaitMills + maxWaitMills;
-            while (System.currentTimeMillis() < endWaitMills) {
-                Optional<IPooledConnection> optional = getFreeConnectionFromPool();
-                if (optional.isPresent()) {
-                    return optional.get();
-                }
-
-                try {
-                    TimeUnit.MILLISECONDS.sleep(1);
-                } catch (InterruptedException e) {
-                    Thread.currentThread().interrupt();
-                }
-                log.debug("等待连接池归还,wait for 1 mills");
-            }
-
-            //2.3 等待超时
-            throw new JdbcPoolException("Can't get connection from pool, wait time out for mills: " + maxWaitMills);
-        }
-
-        //3. 扩容(暂时只扩容一个)
-        log.debug("开始扩容连接池大小,step: 1");
-        IPooledConnection pooledConnection = createPooledConnection();
-        pooledConnection.setBusy(true);
-        this.pool.add(pooledConnection);
-        log.debug("从扩容后的连接池中获取连接");
-        return pooledConnection;
-    }
-
-    @Override
-    public void returnConnection(IPooledConnection pooledConnection) {
-        // 验证状态
-        if (testOnReturn) {
-            checkValid(pooledConnection);
-        }
-
-        // 设置为不繁忙
-        pooledConnection.setBusy(false);
-        log.debug("归还连接,状态设置为不繁忙");
-    }
-
-    /**
-     * 获取空闲的连接
-     *
-     * @return 连接
-     * @since 1.3.0
-     */
-    private Optional<IPooledConnection> getFreeConnectionFromPool() {
-        for (IPooledConnection pc : pool) {
-            if (!pc.isBusy()) {
-                pc.setBusy(true);
-                log.debug("从连接池中获取连接");
-
-                // 验证有效性
-                if (testOnBorrow) {
-                    log.debug("Test on borrow start");
-                    checkValid(pc);
-                    log.debug("Test on borrow finish");
-                }
-
-                return Optional.of(pc);
-            }
-        }
-        // 空
-        return Optional.empty();
-    }
-
-
-    /**
-     * https://stackoverflow.com/questions/3668506/efficient-sql-test-query-or-validation-query-that-will-work-across-all-or-most
-     * <p>
-     * 真正支持标准的,直接使用 {@link Connection#isValid(int)} 验证比较合适
-     *
-     * @param pooledConnection 连接池信息
-     * @since 1.5.0
-     */
-    private void checkValid(final IPooledConnection pooledConnection) {
-        if (StringUtils.isNotEmpty(super.validQuery)) {
-            Connection connection = pooledConnection.getConnection();
-            try {
-                // 如果连接无效,重新申请一个新的替代
-                if (!connection.isValid(super.validTimeOutSeconds)) {
-                    log.debug("Old connection is inValid, start create one for it.");
-
-                    Connection newConnection = createConnection();
-                    pooledConnection.setConnection(newConnection);
-                    log.debug("Old connection is inValid, finish create one for it.");
-                }
-            } catch (SQLException throwables) {
-                throw new JdbcPoolException(throwables);
-            }
-        } else {
-            log.debug("valid query is empty, ignore valid.");
-        }
-    }
-
-    /**
-     * 初始化连接池
-     *
-     * @since 1.1.0
-     */
-    private void initJdbcPool() {
-        final int minSize = super.minSize;
-        pool = new ArrayList<>(minSize);
-
-        for (int i = 0; i < minSize; i++) {
-            IPooledConnection pooledConnection = createPooledConnection();
-
-            pool.add(pooledConnection);
-        }
-    }
-
-    /**
-     * 创建一个池化的连接
-     *
-     * @return 连接
-     * @since 1.1.0
-     */
-    private IPooledConnection createPooledConnection() {
-        Connection connection = createConnection();
-
-        IPooledConnection pooledConnection = new PooledConnection();
-        pooledConnection.setBusy(false);
-        pooledConnection.setConnection(connection);
-        pooledConnection.setDataSource(this);
-
-        return pooledConnection;
-    }
-
-    /**
-     * 创建新连接
-     *
-     * @return 连接
-     * @since 1.1.0
-     */
-    private Connection createConnection() {
-        try {
-            if (StringUtils.isBlank(super.getUser()) && StringUtils.isBlank(super.getPassword())) {
-                return DriverManager.getConnection(super.getJdbcUrl());
-            }
-            return DriverManager.getConnection(super.getJdbcUrl(),
-                    super.getUser(), super.getPassword());
-        } catch (SQLException e) {
-            throw new JdbcPoolException(e);
-        }
-    }
-
-
-    /**
-     * 初始化空闲时检验
-     *
-     * @since 1.5.0
-     */
-    private void initTestOnIdle() {
-        if (StringUtils.isNotEmpty(validQuery)) {
-            ScheduledExecutorService idleExecutor = Executors.newSingleThreadScheduledExecutor();
-
-            idleExecutor.scheduleAtFixedRate(this::testOnIdleCheck, super.testOnIdleIntervalSeconds, testOnIdleIntervalSeconds, TimeUnit.SECONDS);
-            log.debug("Test on idle config with interval seonds: " + testOnIdleIntervalSeconds);
-        }
-    }
-
-    /**
-     * 验证所有的空闲连接是否有效
-     *
-     * @since 1.5.0
-     */
-    private void testOnIdleCheck() {
-        log.debug("start check test on idle");
-        for (IPooledConnection pc : this.pool) {
-            if (!pc.isBusy()) {
-                checkValid(pc);
-            }
-        }
-        log.debug("finish check test on idle");
-    }
-
-}

+ 0 - 26
report-core/src/main/java/com/anjiplus/template/gaea/business/modules/dataSource/pool/datasource/UnPooledDataSource.java

@@ -1,26 +0,0 @@
-package com.anjiplus.template.gaea.business.modules.dataSource.pool.datasource;
-
-import com.anjiplus.template.gaea.business.modules.dataSource.pool.util.DriverClassUtil;
-import org.apache.commons.lang3.StringUtils;
-
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.SQLException;
-
-/**
- * @author binbin.hou
- * @since 1.0.0
- */
-public class UnPooledDataSource extends AbstractDataSourceConfig {
-
-    @Override
-    public Connection getConnection() throws SQLException {
-        DriverClassUtil.loadDriverClass(super.driverClass, super.jdbcUrl);
-        if (StringUtils.isBlank(super.getUser()) && StringUtils.isBlank(super.getPassword())) {
-            return DriverManager.getConnection(super.jdbcUrl);
-        }
-        return DriverManager.getConnection(super.getJdbcUrl(),
-                super.getUser(), super.getPassword());
-    }
-
-}