Skip to content

Commit

Permalink
add bean conditionals and additional logging
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkBurov committed Nov 20, 2018
1 parent 13e17cb commit 57d66c9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ public void afterPropertiesSet() {
DataSource dataSource = dataSourceFactory.create(dataSourceProps);

return new NamedFailAwareDataSource(dataSourceName, dataSource);
}).collect(collectingAndThen(toList(), Collections::unmodifiableList));
})
.peek(dataSource -> log.info("Datasource with id {} of {} type is created",
dataSource.getName(), dataSource.getDataSource().getClass().getCanonicalName()))
.collect(collectingAndThen(toList(), Collections::unmodifiableList));
if (properties.isValidateDataSourceAtStart()) {
validateConnectionAtStart();
}
Expand All @@ -75,7 +78,7 @@ public Connection getConnection(String s, String s1) throws SQLException {
return getBalancedConnection(datasource -> datasource.getConnection(s, s1));
}

private Connection getBalancedConnection(SqlFunction<DataSource, Connection> connectionProducer) throws SQLException { //todo add check on start
private Connection getBalancedConnection(SqlFunction<DataSource, Connection> connectionProducer) throws SQLException {
val filteredDataSources = filteringStrategy.filter(dataSources);
val balancingDataSources = balancingStrategy.apply(filteredDataSources);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.ankburov.spring.balancing.datasource.log.FailedDataSourceLogStrategy;
import io.ankburov.spring.balancing.datasource.log.TimedFailedDataSourceLogStrategy;
import io.ankburov.spring.balancing.datasource.property.BalancingDataSourceProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
Expand All @@ -26,40 +27,47 @@
public class BalancingDataSourceConfiguration {

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.balancing-dataSources-config.balancing", value = "type", havingValue = "FAILOVER", matchIfMissing = true)
public BalancingStrategy failoverBalancingStrategy() {
return new FailOverBalancingStrategy();
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.balancing-dataSources-config.balancing", value = "type", havingValue = "RANDOM")
public BalancingStrategy randomBalancingStrategy() {
return new RandomBalancingStrategy();
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.balancing-dataSources-config.filtering", value = "type", havingValue = "ALL")
public FilteringStrategy useAllFilteringStrategy() {
return new UseAllFilteringStrategy();
}

@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = "spring.balancing-dataSources-config.filtering", value = "type", havingValue = "ONLY_WORKING")
public FilteringStrategy onlyWorkingFilteringStrategy(BalancingDataSourceProperties properties) {
return new OnlyWorkingFilteringStrategy(properties.getFiltering().getTimeThreshold());
}

@Bean
@ConditionalOnMissingBean
public FailedDataSourceLogStrategy timedFailedDataSourceLogStrategy(BalancingDataSourceProperties properties) {
return new TimedFailedDataSourceLogStrategy(properties.getLogging().getTimeThreshold());
}

@Bean
@ConditionalOnMissingBean
public UpdateFailedDataSourceStrategy alwaysUpdateFailedDataSourceStrategy() {
return new AlwaysUpdateFailedDataSourceStrategy();
}

@Bean
@ConditionalOnMissingBean
public DataSourceFactory hikariDataSourceFactory() {
return new HikariDataSourceFactory();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import io.ankburov.spring.balancing.datasource.property.ExtendedDataSourceProperties;
import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.util.Properties;

@Slf4j
public class HikariDataSourceFactory implements DataSourceFactory {

@Override
Expand All @@ -28,6 +30,7 @@ public DataSource create(ExtendedDataSourceProperties dataSourceProperties) {
}

dataSourceProperties.getAdditionalProperties().forEach(properties::put);
log.info("Datasource properties are {}", properties.toString());

HikariConfig hikariConfig = new HikariConfig(properties);
return new HikariDataSource(hikariConfig);
Expand Down

0 comments on commit 57d66c9

Please sign in to comment.