Last active
December 23, 2015 19:29
-
-
Save st-kurilin/6682757 to your computer and use it in GitHub Desktop.
Data Source Config with Spring Configurations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<beans> | |
<bean class="com.DataSourceConfig"> | |
<property name="dataSourceId" value="mySource"/> | |
<property name="maxIdle" value="2"/> <!--override default value--> | |
</bean> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<beans> | |
<bean id="myAppDataSource" class="com.copyright.rup.common.config.db.RupBasicDataSource" destroy-method="close" | |
p:driverClassName="org.postgresql.Driver" | |
p:url="$RUP{myapp.db.url}" | |
p:username="$RUP{myapp.db.username}" | |
p:password="$RUP{myapp.db.password}" | |
p:connectionProperties="ApplicationName=#{T(com.copyright.rup.common.app.RupAppUtils).getApplicationIdentifier()};" | |
p:initialSize="$RUP{myapp.database.pool.initial.size:1}" | |
p:maxActive="$RUP{myapp.database.pool.max.active.size:10}" | |
p:maxIdle="$RUP{myapp.database.pool.max.idle.size:5}" | |
p:minIdle="$RUP{myapp.database.pool.min.idle.size:0}" | |
p:maxWait="$RUP{myapp.database.timeout:300000}" | |
p:validationQuery="$RUP{myapp.database.pool.validation.query.sql:SELECT 1}" | |
p:validationQueryTimeout="$RUP{myapp.database.pool.validation.query.timeout:-1}" | |
p:testOnBorrow="$RUP{myapp.database.pool.validation.query.run:true}" | |
p:testOnReturn="$RUP{myapp.database.pool.validation.query.run.onreturn:false}" | |
p:testWhileIdle="$RUP{myapp.database.pool.validation.query.run.whileidle:false}" | |
p:timeBetweenEvictionRunsMillis="$RUP{myapp.database.pool.eviction.run.time:-1}" | |
p:numTestsPerEvictionRun="$RUP{myapp.database.pool.eviction.connection.number:3}" | |
p:minEvictableIdleTimeMillis="$RUP{myapp.database.pool.eviction.min.idle.time:1800000}" | |
p:poolPreparedStatements="$RUP{myapp.database.pool.pool.prepared.statements:false}" | |
p:maxOpenPreparedStatements="$RUP{myapp.database.pool.max.open.prepared.statements:0}" | |
p:removeAbandoned="$RUP{myapp.database.pool.remove.abandoned:false}" | |
p:removeAbandonedTimeout="$RUP{myapp.database.pool.remove.abandoned.timeout:300}" | |
p:logAbandoned="$RUP{myapp.database.pool.log.abandoned:false}" | |
/> | |
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false"> | |
<property name="beans"> | |
<map> | |
<entry key="com.copyright.rup.myapp:name=myAppDataSource" value-ref="myAppDataSource"/> | |
</map> | |
</property> | |
</bean> | |
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com; | |
import com.google.common.base.Optional; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.config.ConfigurableBeanFactory; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.jmx.export.MBeanExporter; | |
import javax.annotation.PostConstruct; | |
import javax.management.MalformedObjectNameException; | |
import javax.management.ObjectName; | |
import static com.google.common.base.Optional.fromNullable; | |
/** | |
* Extracted template configuration for Data SourceConfig. Part of RUP common distribution. | |
*/ | |
@Configuration | |
public class DataSourceConfig { | |
@Autowired | |
private MBeanExporter beanExporter; | |
@Autowired | |
private ConfigurableBeanFactory beanFactory; | |
private String maxIdle; | |
private String maxWait; | |
private String dataSourceId; | |
@PostConstruct | |
public void init() throws MalformedObjectNameException { | |
final RupBasicDataSource dataSource = createDataSource(); | |
beanExporter.registerManagedResource(dataSource, new ObjectName("com.copyright.rup.myapp:name=myAppDataSource")); | |
beanFactory.registerSingleton(dataSourceId, dataSource); | |
} | |
private RupBasicDataSource createDataSource() { | |
final RupBasicDataSource dataSource = new RupBasicDataSource(); | |
dataSource.setDriverClassName("org.postgresql.Driver"); | |
dataSource.setInitialSize(readFromProperties("database.pool.initial.size").or("1")); //from properties or default | |
dataSource.setMaxIdle(fromNullable(maxIdle).or("5")); //from setters or default | |
dataSource.setMaxWait(fromNullable(maxWait).or(readFromProperties("database.timeout")).or("5")); //from setters or properties or default | |
return dataSource; | |
} | |
private Optional<String> readFromProperties(String prop) { | |
final String key = String.format("$RUP{%s.%s}", getAppId(), prop); | |
final String resolved = beanFactory.resolveEmbeddedValue(key); | |
if (key.equals(resolved)) { | |
return Optional.absent(); | |
} | |
return Optional.of(resolved); | |
} | |
public void setMaxIdle(String maxIdle) { | |
this.maxIdle = maxIdle; | |
} | |
public void setMaxWait(String maxWait) { | |
this.maxWait = maxWait; | |
} | |
public void setDataSourceId(String dataSourceId) { | |
this.dataSourceId = dataSourceId; | |
} | |
private String getAppId() { | |
//could be readed using rup common utils or setted as a property (see dataSourceId) | |
return "myApp"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dmitro pointed that app prefix for properties is not app id. So it will be needed to set it as we do for dataSourceId.