@Test public void externalDatasourceToAssertMinIdle() { MockEnvironment environment = new MockEnvironment(); environment.setProperty("db.num", "1"); environment.setProperty("db.user", USERNAME); environment.setProperty("db.password", PASSWORD); environment.setProperty("db.url.0", JDBC_URL); List<HikariDataSource> dataSources = new ExternalDataSourceProperties().build(environment, (dataSource -> { dataSource.validate(); Assert.assertEquals(dataSource.getMinimumIdle(), ExternalDataSourceProperties.DEFAULT_MINIMUM_IDLE); })); Assert.assertEquals(dataSources.size(), 1); }
List<HikariDataSource> build(Environment environment, Callback<HikariDataSource> callback) { List<HikariDataSource> dataSources = new ArrayList<>(); Binder.get(environment).bind("db", Bindable.ofInstance(this)); Preconditions.checkArgument(Objects.nonNull(num), "db.num is null"); Preconditions.checkArgument(CollectionUtils.isNotEmpty(user), "db.user or db.user.[index] is null"); Preconditions.checkArgument(CollectionUtils.isNotEmpty(password), "db.password or db.password.[index] is null"); for (int index = 0; index < num; index++) { int currentSize = index + 1; Preconditions.checkArgument(url.size() >= currentSize, "db.url.%s is null", index); HikariDataSource ds = new HikariDataSource(); ds.setDriverClassName(JDBC_DRIVER_NAME); ds.setJdbcUrl(url.get(index).trim()); ds.setUsername(getOrDefault(user, index, user.get(0)).trim()); ds.setPassword(getOrDefault(password, index, password.get(0)).trim()); ds.setConnectionTimeout(CONNECTION_TIMEOUT_MS); ds.setMaximumPoolSize(getOrDefault(maxPoolSize, index, DEFAULT_MAX_POOL_SIZE)); ds.setMinimumIdle(getOrDefault(minIdle, index, DEFAULT_MINIMUM_IDLE)); ds.setValidationTimeout(TimeUnit.MINUTES.toMillis(VALIDATION_TIMEOUT)); ds.setConnectionTestQuery(TEST_QUERY); dataSources.add(ds); callback.accept(ds); } Preconditions.checkArgument(CollectionUtils.isNotEmpty(dataSources), "no datasource available"); return dataSources; }
ExternalDataSourceProperties { List<HikariDataSource> build(Environment environment, Callback<HikariDataSource> callback) { List<HikariDataSource> dataSources = new ArrayList<>(); Binder.get(environment).bind("db", Bindable.ofInstance(this)); Preconditions.checkArgument(Objects.nonNull(num), "db.num is null"); Preconditions.checkArgument(CollectionUtils.isNotEmpty(user), "db.user or db.user.[index] is null"); Preconditions.checkArgument(CollectionUtils.isNotEmpty(password), "db.password or db.password.[index] is null"); for (int index = 0; index < num; index++) { int currentSize = index + 1; Preconditions.checkArgument(url.size() >= currentSize, "db.url.%s is null", index); HikariDataSource ds = new HikariDataSource(); ds.setDriverClassName(JDBC_DRIVER_NAME); ds.setJdbcUrl(url.get(index).trim()); ds.setUsername(getOrDefault(user, index, user.get(0)).trim()); ds.setPassword(getOrDefault(password, index, password.get(0)).trim()); ds.setConnectionTimeout(CONNECTION_TIMEOUT_MS); ds.setMaximumPoolSize(getOrDefault(maxPoolSize, index, DEFAULT_MAX_POOL_SIZE)); ds.setMinimumIdle(getOrDefault(minIdle, index, DEFAULT_MINIMUM_IDLE)); ds.setValidationTimeout(TimeUnit.MINUTES.toMillis(VALIDATION_TIMEOUT)); ds.setConnectionTestQuery(TEST_QUERY); dataSources.add(ds); callback.accept(ds); } Preconditions.checkArgument(CollectionUtils.isNotEmpty(dataSources), "no datasource available"); return dataSources; } }
ExternalDataSourceProperties { List<HikariDataSource> build(Environment environment, Callback<HikariDataSource> callback) { List<HikariDataSource> dataSources = new ArrayList<>(); Binder.get(environment).bind("db", Bindable.ofInstance(this)); Preconditions.checkArgument(Objects.nonNull(num), "db.num is null"); Preconditions.checkArgument(CollectionUtils.isNotEmpty(user), "db.user or db.user.[index] is null"); Preconditions.checkArgument(CollectionUtils.isNotEmpty(password), "db.password or db.password.[index] is null"); for (int index = 0; index < num; index++) { int currentSize = index + 1; Preconditions.checkArgument(url.size() >= currentSize, "db.url.%s is null", index); HikariDataSource ds = new HikariDataSource(); ds.setDriverClassName(JDBC_DRIVER_NAME); ds.setJdbcUrl(url.get(index).trim()); ds.setUsername(getOrDefault(user, index, user.get(0)).trim()); ds.setPassword(getOrDefault(password, index, password.get(0)).trim()); ds.setConnectionTimeout(CONNECTION_TIMEOUT_MS); ds.setMaximumPoolSize(getOrDefault(maxPoolSize, index, DEFAULT_MAX_POOL_SIZE)); ds.setMinimumIdle(getOrDefault(minIdle, index, DEFAULT_MINIMUM_IDLE)); ds.setValidationTimeout(TimeUnit.MINUTES.toMillis(VALIDATION_TIMEOUT)); ds.setConnectionTestQuery(TEST_QUERY); dataSources.add(ds); callback.accept(ds); } Preconditions.checkArgument(CollectionUtils.isNotEmpty(dataSources), "no datasource available"); return dataSources; }  }
ExternalDataSourceProperties { List<HikariDataSource> build(Environment environment, Callback<HikariDataSource> callback) { List<HikariDataSource> dataSources = new ArrayList<>(); Binder.get(environment).bind("db", Bindable.ofInstance(this)); Preconditions.checkArgument(Objects.nonNull(num), "db.num is null"); Preconditions.checkArgument(CollectionUtils.isNotEmpty(user), "db.user or db.user.[index] is null"); Preconditions.checkArgument(CollectionUtils.isNotEmpty(password), "db.password or db.password.[index] is null"); for (int index = 0; index < num; index++) { int currentSize = index + 1; Preconditions.checkArgument(url.size() >= currentSize, "db.url.%s is null", index); HikariDataSource ds = new HikariDataSource(); ds.setDriverClassName(JDBC_DRIVER_NAME); ds.setJdbcUrl(url.get(index).trim()); ds.setUsername(getOrDefault(user, index, user.get(0)).trim()); ds.setPassword(getOrDefault(password, index, password.get(0)).trim()); ds.setConnectionTimeout(CONNECTION_TIMEOUT_MS); ds.setMaximumPoolSize(getOrDefault(maxPoolSize, index, DEFAULT_MAX_POOL_SIZE)); ds.setMinimumIdle(getOrDefault(minIdle, index, DEFAULT_MINIMUM_IDLE)); ds.setValidationTimeout(TimeUnit.MINUTES.toMillis(VALIDATION_TIMEOUT)); ds.setConnectionTestQuery(TEST_QUERY); dataSources.add(ds); callback.accept(ds); } Preconditions.checkArgument(CollectionUtils.isNotEmpty(dataSources), "no datasource available"); return dataSources; }  void setNum(Integer num); void setUrl(List<String> url); void setUser(List<String> user); void setPassword(List<String> password); void setMaxPoolSize(List<Integer> maxPoolSize); void setMinIdle(List<Integer> minIdle); }
ExternalDataSourceProperties { List<HikariDataSource> build(Environment environment, Callback<HikariDataSource> callback) { List<HikariDataSource> dataSources = new ArrayList<>(); Binder.get(environment).bind("db", Bindable.ofInstance(this)); Preconditions.checkArgument(Objects.nonNull(num), "db.num is null"); Preconditions.checkArgument(CollectionUtils.isNotEmpty(user), "db.user or db.user.[index] is null"); Preconditions.checkArgument(CollectionUtils.isNotEmpty(password), "db.password or db.password.[index] is null"); for (int index = 0; index < num; index++) { int currentSize = index + 1; Preconditions.checkArgument(url.size() >= currentSize, "db.url.%s is null", index); HikariDataSource ds = new HikariDataSource(); ds.setDriverClassName(JDBC_DRIVER_NAME); ds.setJdbcUrl(url.get(index).trim()); ds.setUsername(getOrDefault(user, index, user.get(0)).trim()); ds.setPassword(getOrDefault(password, index, password.get(0)).trim()); ds.setConnectionTimeout(CONNECTION_TIMEOUT_MS); ds.setMaximumPoolSize(getOrDefault(maxPoolSize, index, DEFAULT_MAX_POOL_SIZE)); ds.setMinimumIdle(getOrDefault(minIdle, index, DEFAULT_MINIMUM_IDLE)); ds.setValidationTimeout(TimeUnit.MINUTES.toMillis(VALIDATION_TIMEOUT)); ds.setConnectionTestQuery(TEST_QUERY); dataSources.add(ds); callback.accept(ds); } Preconditions.checkArgument(CollectionUtils.isNotEmpty(dataSources), "no datasource available"); return dataSources; }  void setNum(Integer num); void setUrl(List<String> url); void setUser(List<String> user); void setPassword(List<String> password); void setMaxPoolSize(List<Integer> maxPoolSize); void setMinIdle(List<Integer> minIdle); static final long CONNECTION_TIMEOUT_MS; static final long VALIDATION_TIMEOUT; static final String TEST_QUERY; static final int DEFAULT_MAX_POOL_SIZE; static final int DEFAULT_MINIMUM_IDLE; }