Skip to content

Commit cccf1a0

Browse files
author
Ke Wang
committed
micro-dbcp plugin
1 parent 16bfa57 commit cccf1a0

File tree

12 files changed

+489
-1
lines changed

12 files changed

+489
-1
lines changed

micro-dbcp/build.gradle

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
description = 'micro-dbcp'
2+
dependencies {
3+
4+
compile group: 'org.apache.commons', name: 'commons-dbcp2', version:'2.1'
5+
compile project(':micro-core')
6+
compile project(':micro-jdbc')
7+
8+
testCompile project(':micro-client')
9+
testCompile project(':micro-grizzly')
10+
testCompile project(':micro-jersey')
11+
testCompile group: 'org.hsqldb', name:'hsqldb', version:'2.0.0'
12+
}
13+
14+
modifyPom {
15+
project {
16+
name 'Microserver dbcp'
17+
description 'Opinionated rest microservices'
18+
url 'https://github.com/aol/micro-server'
19+
inceptionYear '2015'
20+
21+
groupId 'com.aol.microservices'
22+
artifactId 'micro-dbcp'
23+
version "$version"
24+
25+
26+
scm {
27+
url 'scm:git@github.com:aol/micro-server.git'
28+
connection 'scm:git@github.com:aol/micro-server.git'
29+
developerConnection 'scm:git@github.com:aol/micro-server.git'
30+
}
31+
32+
licenses {
33+
license {
34+
name 'The Apache Software License, Version 2.0'
35+
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
36+
distribution 'repo'
37+
}
38+
}
39+
40+
developers {
41+
developer {
42+
id 'johnmcclean-aol'
43+
name 'John McClean'
44+
email 'john.mcclean@teamaol.com'
45+
}
46+
developer {
47+
id 'kewangie'
48+
name 'Ke Wang'
49+
email 'ke.wang@teamaol.com'
50+
}
51+
developer {
52+
id 'earlzero'
53+
name 'Nikita Sapozhnikov'
54+
email 'nikita.sapozhnikov@teamaol.com'
55+
}
56+
}
57+
58+
}
59+
}
60+
61+
extraArchive {
62+
sources = true
63+
tests = true
64+
javadoc = true
65+
}
66+
67+
nexus {
68+
sign = true
69+
repositoryUrl = 'https://oss.sonatype.org/service/local/staging/deploy/maven2'
70+
snapshotRepositoryUrl = 'https://oss.sonatype.org/content/repositories/snapshots'
71+
}
72+

micro-dbcp/readme.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# DBCP plugin
2+
3+
[micro-hikaricp example apps](https://github.com/aol/micro-server/tree/master/micro-jdbc/src/test/java/app)
4+
5+
Creates a DataSource Spring Bean with name "mainDataSource". This will be based on [DBCP2](https://commons.apache.org/proper/commons-dbcp/).
6+
7+
## To use
8+
9+
10+
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.aol.microservices/micro-data/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.aol.microservices/micro-jdbc)
11+
12+
Simply add to the classpath
13+
14+
Maven
15+
16+
<dependency>
17+
<groupId>com.aol.microservices</groupId>
18+
<artifactId>micro-dbcp</artifactId>
19+
<version>x.yz</version>
20+
</dependency>
21+
22+
Gradle
23+
24+
compile 'com.aol.microservices:micro-dbcp:x.yz'
25+
26+
# Configuring a data source
27+
28+
A datasource can be configured by setting the following properties in application.properties, instance.properties or via the Microserver annotation
29+
30+
db.connection.driver: (e.g. (org.hsqldb.jdbcDriver)
31+
db.connection.url: (e.g. jdbc:hsqldb:mem:aname)
32+
db.connection.username: (e.g. admin)
33+
db.connection.password: (e.g. password)
34+
db.connection.dialect: (e.g. org.hibernate.dialect.HSQLDialect)
35+
db.connection.hibernate.showsql: (e.g. true | false)
36+
db.connection.ddl.auto: (e.g. create-drop)
37+
dbcp.db.test.on.borrow: (e.g. true | false)
38+
dbcp.db.validation.query: (e.g. SELECT 1)
39+
dbcp.db.max.total: (e.g. -1)
40+
dbcp.db.min.evictable.idle.time: (e.g. 1800000)
41+
dbcp.db.time.between.eviction.runs: (e.g. 1800000)
42+
dbcp.db.num.tests.per.eviction.run: (e.g. 3)
43+
dbcp.db.test.while.idle: (e.g. true | false)
44+
dbcp.db.test.on.return: (e.g. true | false)
45+
46+
47+
The Microserver annotation can also be used to set some default properties, or they can be set in an application.properties or instance.properties file ([see wiki for more details](https://github.com/aol/micro-server/wiki/Defining-Properties)).
48+
49+
50+
The important properties for us to set are the datasource properties
51+
52+
@Microserver(properties={"db.connection.driver","org.hsqldb.jdbcDriver",
53+
"db.connection.url","jdbc:hsqldb:mem:aname",
54+
"db.connection.username", "sa"})
55+
56+
public class MyMainClass {
57+
58+
59+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.aol.micro.server.spring;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
import java.util.function.Function;
7+
8+
import javax.servlet.ServletContextListener;
9+
10+
import com.aol.micro.server.Plugin;
11+
import com.aol.micro.server.servers.model.ServerData;
12+
import com.aol.micro.server.spring.datasource.DBCPConfig;
13+
import com.aol.micro.server.spring.datasource.DBCPDataSourceBuilder;
14+
15+
/**
16+
*
17+
* @author kewang
18+
*
19+
*/
20+
public class DBCPPlugin implements Plugin {
21+
22+
@Override
23+
public Set<Class> springClasses() {
24+
return new HashSet<>(Arrays.asList(DBCPConfig.class, DBCPDataSourceBuilder.class));
25+
}
26+
27+
@Override
28+
public Set<Function<ServerData, ServletContextListener>> servletContextListeners() {
29+
return null;
30+
}
31+
32+
@Override
33+
public Set<Class> jaxRsResources() {
34+
return null;
35+
}
36+
37+
@Override
38+
public Set<String> jaxRsPackages() {
39+
return null;
40+
}
41+
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.aol.micro.server.spring.datasource;
2+
3+
import lombok.Getter;
4+
import lombok.experimental.Builder;
5+
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.beans.factory.annotation.Value;
8+
import org.springframework.stereotype.Component;
9+
10+
@Getter
11+
@Builder
12+
@Component("dbcpEnv")
13+
public class DBCPConfig {
14+
15+
private final boolean testOnBorrow;
16+
private final String validationQuery;
17+
private final int maxTotal;
18+
private final long minEvictableIdleTime;
19+
private final long timeBetweenEvictionRuns;
20+
private final int numTestsPerEvictionRun;
21+
private final boolean testWhileIdle;
22+
private final boolean testOnReturn;
23+
24+
@Autowired
25+
public DBCPConfig(@Value("${dbcp.db.test.on.borrow:true}") boolean testOnBorrow,
26+
@Value("${dbcp.db.validation.query:SELECT 1}") String validationQuery, @Value("${dbcp.db.max.total:-1}") int maxTotal,
27+
@Value("${dbcp.db.min.evictable.idle.time:1800000}") long minEvictableIdleTime,
28+
@Value("${dbcp.db.time.between.eviction.runs:1800000}") long timeBetweenEvictionRuns,
29+
@Value("${dbcp.db.num.tests.per.eviction.run:3}") int numTestsPerEvictionRun,
30+
@Value("${dbcp.db.test.while.idle:true}") boolean testWhileIdle, @Value("${dbcp.db.test.on.return:true}") boolean testOnReturn) {
31+
this.testOnBorrow = testOnBorrow;
32+
this.validationQuery = validationQuery;
33+
this.maxTotal = maxTotal;
34+
this.minEvictableIdleTime = minEvictableIdleTime;
35+
this.timeBetweenEvictionRuns = timeBetweenEvictionRuns;
36+
this.numTestsPerEvictionRun = numTestsPerEvictionRun;
37+
this.testWhileIdle = testWhileIdle;
38+
this.testOnReturn = testOnReturn;
39+
40+
}
41+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.aol.micro.server.spring.datasource;
2+
3+
import javax.annotation.Resource;
4+
import javax.sql.DataSource;
5+
6+
import lombok.AllArgsConstructor;
7+
import lombok.NoArgsConstructor;
8+
import lombok.experimental.Builder;
9+
10+
import org.apache.commons.dbcp2.BasicDataSource;
11+
import org.springframework.context.annotation.Bean;
12+
import org.springframework.context.annotation.Configuration;
13+
14+
@Builder
15+
@Configuration
16+
@NoArgsConstructor
17+
@AllArgsConstructor
18+
public class DBCPDataSourceBuilder {
19+
20+
@Resource(name = "mainEnv")
21+
private JdbcConfig mainEnv;
22+
23+
@Resource(name = "dbcpEnv")
24+
private DBCPConfig dbcpEnv;
25+
26+
@Bean(destroyMethod = "close", name = "mainDataSource")
27+
public DataSource mainDataSource() {
28+
return getDataSource();
29+
}
30+
31+
private DataSource getDataSource() {
32+
BasicDataSource ds = new BasicDataSource();
33+
34+
ds.setDriverClassName(mainEnv.getDriverClassName());
35+
ds.setUrl(mainEnv.getUrl());
36+
ds.setUsername(mainEnv.getUsername());
37+
ds.setPassword(mainEnv.getPassword());
38+
39+
retrySetup(ds);
40+
41+
return ds;
42+
}
43+
44+
private void retrySetup(BasicDataSource ds) {
45+
if (!"org.hibernate.dialect.HSQLDialect".equals(mainEnv.getDialect())) {
46+
ds.setTestOnBorrow(dbcpEnv.isTestOnBorrow());
47+
ds.setValidationQuery(dbcpEnv.getValidationQuery());
48+
ds.setMaxTotal(dbcpEnv.getMaxTotal());
49+
ds.setMinEvictableIdleTimeMillis(dbcpEnv.getMinEvictableIdleTime());
50+
ds.setTimeBetweenEvictionRunsMillis(dbcpEnv.getTimeBetweenEvictionRuns());
51+
ds.setNumTestsPerEvictionRun(dbcpEnv.getNumTestsPerEvictionRun());
52+
ds.setTestWhileIdle(dbcpEnv.isTestWhileIdle());
53+
ds.setTestOnReturn(dbcpEnv.isTestOnReturn());
54+
}
55+
}
56+
57+
58+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.aol.micro.server.spring.DBCPPlugin
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package app.pure.jdbc.com.aol.micro.server;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
import lombok.experimental.Builder;
8+
9+
10+
11+
12+
@Setter
13+
@Getter
14+
@Builder
15+
@NoArgsConstructor
16+
@AllArgsConstructor
17+
public class JdbcEntity implements java.io.Serializable {
18+
19+
private static final long serialVersionUID = 1L;
20+
21+
private Long id;
22+
private String name;
23+
private String value;
24+
private int version;
25+
26+
27+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package app.pure.jdbc.com.aol.micro.server;
2+
3+
import static org.hamcrest.CoreMatchers.is;
4+
import static org.junit.Assert.assertThat;
5+
6+
import java.util.concurrent.ExecutionException;
7+
8+
import org.junit.After;
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
12+
import com.aol.micro.server.MicroserverApp;
13+
import com.aol.micro.server.config.Microserver;
14+
import com.aol.micro.server.rest.client.nio.AsyncRestClient;
15+
import com.aol.micro.server.testing.RestAgent;
16+
17+
@Microserver(properties = { "db.connection.driver", "org.hsqldb.jdbcDriver", "db.connection.url", "jdbc:hsqldb:mem:aname", "db.connection.username",
18+
"sa", "db.connection.dialect", "org.hibernate.dialect.HSQLDialect" })
19+
public class JdbcRunnerTest {
20+
21+
private final AsyncRestClient<JdbcEntity> listClient = new AsyncRestClient(1000, 1000).withResponse(JdbcEntity.class);
22+
23+
RestAgent rest = new RestAgent();
24+
25+
MicroserverApp server;
26+
27+
@Before
28+
public void startServer() {
29+
30+
server = new MicroserverApp(() -> "jdbc-app");
31+
server.start();
32+
rest.get("http://localhost:8080/jdbc-app/persistence/gen");
33+
34+
}
35+
36+
@After
37+
public void stopServer() {
38+
server.stop();
39+
}
40+
41+
@Test
42+
public void runAppAndBasicTest() throws InterruptedException, ExecutionException {
43+
44+
assertThat(rest.get("http://localhost:8080/jdbc-app/persistence/create"), is("ok"));
45+
assertThat(listClient.get("http://localhost:8080/jdbc-app/persistence/get").get(), is(JdbcEntity.class));
46+
47+
}
48+
49+
}

0 commit comments

Comments
 (0)