Skip to content

Commit fc76f4f

Browse files
committed
rxjava-jdbc module fix jooby-project#351 + add website doc for rxjava + some minor doc updates
1 parent 187aa14 commit fc76f4f

20 files changed

Lines changed: 1040 additions & 20 deletions

File tree

coverage-report/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<source>${project.parent.basedir}/jooby-sitemap/src/main/java</source>
7171
<source>${project.parent.basedir}/jooby-rxjava/src/main/java</source>
7272
<source>${project.parent.basedir}/jooby-banner/src/main/java</source>
73+
<source>${project.parent.basedir}/jooby-rxjava-jdbc/src/main/java</source>
7374
</sources>
7475
</configuration>
7576
</execution>
@@ -119,6 +120,7 @@
119120
<source>${project.parent.basedir}/jooby-sitemap/src/test/java</source>
120121
<source>${project.parent.basedir}/jooby-rxjava/src/test/java</source>
121122
<source>${project.parent.basedir}/jooby-banner/src/test/java</source>
123+
<source>${project.parent.basedir}/jooby-rxjava-jdbc/src/test/java</source>
122124
</sources>
123125
</configuration>
124126
</execution>
@@ -351,6 +353,12 @@
351353
<version>${project.version}</version>
352354
</dependency>
353355

356+
<dependency>
357+
<groupId>org.jooby</groupId>
358+
<artifactId>jooby-rxjava-jdbc</artifactId>
359+
<version>${project.version}</version>
360+
</dependency>
361+
354362
<!-- H2 database -->
355363
<dependency>
356364
<groupId>com.h2database</groupId>
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.jooby.issues;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.jooby.rx.Rx;
6+
import org.jooby.rx.RxJdbc;
7+
import org.jooby.test.ServerFeature;
8+
import org.junit.Test;
9+
10+
import com.github.davidmoten.rx.jdbc.Database;
11+
import com.typesafe.config.ConfigFactory;
12+
import com.typesafe.config.ConfigValueFactory;
13+
14+
public class Issue351 extends ServerFeature {
15+
16+
{
17+
use(ConfigFactory.empty()
18+
.withValue("db", ConfigValueFactory.fromAnyRef("mem")));
19+
20+
use(new RxJdbc());
21+
22+
onStart(r -> {
23+
Database db = r.require(Database.class);
24+
db.update("create table something (id int primary key, name varchar(100))")
25+
.execute();
26+
27+
db.update("insert into something (id, name) values (?, ?)")
28+
.parameters(1, "jooby")
29+
.execute();
30+
});
31+
32+
get("/351/reactive", req ->
33+
req.require(Database.class)
34+
.select("select name from something where id = :id")
35+
.parameter("id", 1)
36+
.getAs(String.class)
37+
).map(Rx.rx());
38+
39+
get("/351/blocking", req -> {
40+
return req.require(Database.class)
41+
.select("select name from something where id = :id")
42+
.parameter("id", 1)
43+
.getAs(String.class)
44+
.toBlocking()
45+
.single();
46+
});
47+
48+
get("/db", req -> {
49+
assertEquals(req.require(Database.class), req.require(Database.class));
50+
return "OK";
51+
});
52+
53+
}
54+
55+
@Test
56+
public void rxjdbc() throws Exception {
57+
request().get("/351/reactive")
58+
.expect("jooby");
59+
}
60+
61+
@Test
62+
public void rxjdbcBlocking() throws Exception {
63+
request().get("/351/blocking")
64+
.expect("jooby");
65+
}
66+
67+
@Test
68+
public void singletondb() throws Exception {
69+
request().get("/db")
70+
.expect("OK");
71+
}
72+
73+
}

jooby-banner/README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# banner
2+
3+
Prints out an ASCII art banner on startup using <a href="https://github.com/lalyos/jfiglet">jfiglet</a>.
4+
5+
## dependency
6+
7+
```xml
8+
<dependency>
9+
<groupId>org.jooby</groupId>
10+
<artifactId>jooby-banner</artifactId>
11+
<version>1.0.0.CR2</version>
12+
</dependency>
13+
```
14+
15+
## usage
16+
17+
```java
18+
package com.myapp;
19+
{
20+
use(new Banner());
21+
22+
}
23+
```
24+
25+
Prints out the value of ```application.name``` which here is ```myapp```. Or you can specify the text to prints out:
26+
27+
```java
28+
package com.myapp;
29+
{
30+
use(new Banner("my awesome app"));
31+
32+
}
33+
```
34+
35+
## font
36+
37+
You can pick and use the font of your choice via {@link #font(String)} option:
38+
39+
```java
40+
package com.myapp;
41+
{
42+
use(new Banner("my awesome app").font("slant"));
43+
44+
}
45+
```
46+
47+
Fonts are distributed within the library inside the ```/flf``` classpath folder. A full list of fonts is available <a href="http://patorjk.com/software/taag">here</a>.

jooby-jdbc/src/main/java/org/jooby/jdbc/HikariDataSourceProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ public DataSource get() {
5454
}
5555

5656
public void stop() {
57-
dataSource.get().close();
57+
HikariDataSource ds = dataSource.get();
58+
if (!ds.isClosed()) {
59+
ds.close();
60+
}
5861
}
5962

6063
@Override

jooby-jdbc/src/main/java/org/jooby/jdbc/Jdbc.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,13 @@
4444
import com.zaxxer.hikari.HikariConfig;
4545

4646
/**
47+
* <h1>jdbc</h1>
48+
* <p>
4749
* Production-ready jdbc data source, powered by the
4850
* <a href="https://github.com/brettwooldridge/HikariCP">HikariCP</a> library.
51+
* </p>
4952
*
50-
* <h1>Usage</h1>
53+
* <h2>usage</h2>
5154
*
5255
* <pre>
5356
* import org.jooby.jdbc.Jdbc;
@@ -64,11 +67,13 @@
6467
* }
6568
* </pre>
6669
*
67-
* <h1>db configuration</h1> Database configuration is controlled from your
68-
* <code>application.conf</code> file using the <code>db</code> property and friends:
69-
* <code>db.*</code>.
70+
* <h2>db configuration</h2>
71+
* <p>
72+
* Database configuration is controlled from your <code>application.conf</code> file using the
73+
* <code>db</code> property and friends: <code>db.*</code>.
74+
* </p>
7075
*
71-
* <h2>mem db</h2>
76+
* <h3>mem db</h3>
7277
*
7378
* <pre>
7479
* db = mem
@@ -86,7 +91,7 @@
8691
*
8792
* Mem db is useful for dev environment and/or transient data that can be regenerated.
8893
*
89-
* <h2>fs db</h2>
94+
* <h3>fs db</h3>
9095
*
9196
* <pre>
9297
* db = fs
@@ -99,7 +104,10 @@
99104
* regenerated. Keep in mind this db is saved in a tmp directory and db will be deleted it
100105
* on restarts.
101106
*
102-
* <h2>db.url</h2> Connect to a database using a jdbc url, some examples here:
107+
* <h3>db.url</h3>
108+
* <p>
109+
* Connect to a database using a jdbc url, some examples here:
110+
* </p>
103111
*
104112
* <pre>
105113
* # mysql
@@ -111,7 +119,7 @@
111119
* Previous example, show you how to connect to <strong>mysql</strong>, setting user and password.
112120
* But of course you need the jdbc driver on your <code>pom.xml</code>:
113121
*
114-
* <h2>hikari configuration</h2>
122+
* <h3>hikari configuration</h3>
115123
* <p>
116124
* If you need to configure or tweak the <a
117125
* href="https://github.com/brettwooldridge/HikariCP">hikari pool</a> just add <code>hikari.*</code>
@@ -149,7 +157,7 @@
149157
* </pre>
150158
*
151159
* <p>
152-
* application.conf
160+
* application.conf:
153161
* </p>
154162
*
155163
* <pre>

jooby-jooq/src/test/java/org/jooby/jooq/jOOQTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public class jOOQTest {
9393
expect(binder.bind(Key.get(DSLContext.class, Names.named("db")))).andReturn(abbC);
9494
};
9595

96-
private MockUnit.Block managed = unit -> {
96+
private MockUnit.Block onStop = unit -> {
9797
Env env = unit.get(Env.class);
9898
expect(env.onStop(isA(CheckedRunnable.class))).andReturn(env);
9999
};
@@ -104,7 +104,7 @@ public void defaults() throws Exception {
104104
.expect(jdbc)
105105
.expect(configuration)
106106
.expect(ctx)
107-
.expect(managed)
107+
.expect(onStop)
108108
.run(unit -> {
109109
new jOOQ()
110110
.configure(unit.get(Env.class), config(), unit.get(Binder.class));
@@ -117,7 +117,7 @@ public void doWith() throws Exception {
117117
.expect(jdbc)
118118
.expect(configuration)
119119
.expect(ctx)
120-
.expect(managed)
120+
.expect(onStop)
121121
.run(unit -> {
122122
new jOOQ()
123123
.doWith(c -> assertEquals(unit.get(Configuration.class), c))

jooby-rxjava-jdbc/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# rxjdbc
2+
3+
<a href="https://github.com/davidmoten/rxjava-jdbc">rxjava-jdbc</a> efficient execution, concise code, and functional composition of database calls using JDBC and RxJava Observable.
4+
5+
This module depends on [jdbc module](/doc/jdbc), make sure you read the doc of the [jdbc module](/doc/jdbc) module.
6+
7+
## dependency
8+
9+
```xml
10+
<dependency>
11+
<groupId>org.jooby</groupId>
12+
<artifactId>jooby-rxjdbc</artifactId>
13+
<version>1.0.0.CR2</version>
14+
</dependency>
15+
```
16+
17+
## exports
18+
19+
* A ```Database``` object
20+
* A [Hikari](https://github.com/brettwooldridge/HikariCP) ```DataSource``` object
21+
22+
## usage
23+
24+
```java
25+
import org.jooby.rx.RxJdbc;
26+
import org.jooby.rx.Rx;
27+
{
28+
use(new RxJdbc());
29+
30+
get("/reactive", req ->
31+
req.require(Database.class)
32+
.select("select name from something where id = :id")
33+
.parameter("id", 1)
34+
.getAs(String.class)
35+
).map(Rx.rx());
36+
37+
}
38+
```
39+
40+
The [Rx.rx()](/apidocs/org/jooby/rx/Rx.html#rx--) mapper converts ```Observable``` to [deferred](/apidocs/org/jooby/Deferred.html) instances. More at [rx module](/doc/rxjava).
41+
42+
## multiple db connections
43+
44+
```java
45+
import org.jooby.rx.RxJdbc;
46+
import org.jooby.rx.Rx;
47+
{
48+
use(new RxJdbc("db.main"));
49+
50+
use(new RxJdbc("db.audit"));
51+
52+
get("/", req ->
53+
54+
Databse db = req.require("db.main", Database.class);
55+
Databse audit = req.require("db.audit", Database.class);
56+
// ...
57+
).map(Rx.rx());
58+
59+
}
60+
```
61+
62+
For more details on how to configure the Hikari datasource, please check the [jdbc module](/doc/jdbc).
63+
64+
Happy coding!!!

0 commit comments

Comments
 (0)