Skip to content

Commit

Permalink
rxjava-jdbc module fix #351 + add website doc for rxjava + some minor…
Browse files Browse the repository at this point in the history
… doc updates
  • Loading branch information
jknack committed Apr 22, 2016
1 parent 187aa14 commit fc76f4f
Show file tree
Hide file tree
Showing 20 changed files with 1,040 additions and 20 deletions.
8 changes: 8 additions & 0 deletions coverage-report/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<source>${project.parent.basedir}/jooby-sitemap/src/main/java</source>
<source>${project.parent.basedir}/jooby-rxjava/src/main/java</source>
<source>${project.parent.basedir}/jooby-banner/src/main/java</source>
<source>${project.parent.basedir}/jooby-rxjava-jdbc/src/main/java</source>
</sources>
</configuration>
</execution>
Expand Down Expand Up @@ -119,6 +120,7 @@
<source>${project.parent.basedir}/jooby-sitemap/src/test/java</source>
<source>${project.parent.basedir}/jooby-rxjava/src/test/java</source>
<source>${project.parent.basedir}/jooby-banner/src/test/java</source>
<source>${project.parent.basedir}/jooby-rxjava-jdbc/src/test/java</source>
</sources>
</configuration>
</execution>
Expand Down Expand Up @@ -351,6 +353,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.jooby</groupId>
<artifactId>jooby-rxjava-jdbc</artifactId>
<version>${project.version}</version>
</dependency>

<!-- H2 database -->
<dependency>
<groupId>com.h2database</groupId>
Expand Down
73 changes: 73 additions & 0 deletions coverage-report/src/test/java/org/jooby/issues/Issue351.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.jooby.issues;

import static org.junit.Assert.assertEquals;

import org.jooby.rx.Rx;
import org.jooby.rx.RxJdbc;
import org.jooby.test.ServerFeature;
import org.junit.Test;

import com.github.davidmoten.rx.jdbc.Database;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigValueFactory;

public class Issue351 extends ServerFeature {

{
use(ConfigFactory.empty()
.withValue("db", ConfigValueFactory.fromAnyRef("mem")));

use(new RxJdbc());

onStart(r -> {
Database db = r.require(Database.class);
db.update("create table something (id int primary key, name varchar(100))")
.execute();

db.update("insert into something (id, name) values (?, ?)")
.parameters(1, "jooby")
.execute();
});

get("/351/reactive", req ->
req.require(Database.class)
.select("select name from something where id = :id")
.parameter("id", 1)
.getAs(String.class)
).map(Rx.rx());

get("/351/blocking", req -> {
return req.require(Database.class)
.select("select name from something where id = :id")
.parameter("id", 1)
.getAs(String.class)
.toBlocking()
.single();
});

get("/db", req -> {
assertEquals(req.require(Database.class), req.require(Database.class));
return "OK";
});

}

@Test
public void rxjdbc() throws Exception {
request().get("/351/reactive")
.expect("jooby");
}

@Test
public void rxjdbcBlocking() throws Exception {
request().get("/351/blocking")
.expect("jooby");
}

@Test
public void singletondb() throws Exception {
request().get("/db")
.expect("OK");
}

}
47 changes: 47 additions & 0 deletions jooby-banner/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# banner

Prints out an ASCII art banner on startup using <a href="https://github.com/lalyos/jfiglet">jfiglet</a>.

## dependency

```xml
<dependency>
<groupId>org.jooby</groupId>
<artifactId>jooby-banner</artifactId>
<version>1.0.0.CR2</version>
</dependency>
```

## usage

```java
package com.myapp;
{
use(new Banner());

}
```

Prints out the value of ```application.name``` which here is ```myapp```. Or you can specify the text to prints out:

```java
package com.myapp;
{
use(new Banner("my awesome app"));

}
```

## font

You can pick and use the font of your choice via {@link #font(String)} option:

```java
package com.myapp;
{
use(new Banner("my awesome app").font("slant"));

}
```

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>.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ public DataSource get() {
}

public void stop() {
dataSource.get().close();
HikariDataSource ds = dataSource.get();
if (!ds.isClosed()) {
ds.close();
}
}

@Override
Expand Down
26 changes: 17 additions & 9 deletions jooby-jdbc/src/main/java/org/jooby/jdbc/Jdbc.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@
import com.zaxxer.hikari.HikariConfig;

/**
* <h1>jdbc</h1>
* <p>
* Production-ready jdbc data source, powered by the
* <a href="https://github.com/brettwooldridge/HikariCP">HikariCP</a> library.
* </p>
*
* <h1>Usage</h1>
* <h2>usage</h2>
*
* <pre>
* import org.jooby.jdbc.Jdbc;
Expand All @@ -64,11 +67,13 @@
* }
* </pre>
*
* <h1>db configuration</h1> Database configuration is controlled from your
* <code>application.conf</code> file using the <code>db</code> property and friends:
* <code>db.*</code>.
* <h2>db configuration</h2>
* <p>
* Database configuration is controlled from your <code>application.conf</code> file using the
* <code>db</code> property and friends: <code>db.*</code>.
* </p>
*
* <h2>mem db</h2>
* <h3>mem db</h3>
*
* <pre>
* db = mem
Expand All @@ -86,7 +91,7 @@
*
* Mem db is useful for dev environment and/or transient data that can be regenerated.
*
* <h2>fs db</h2>
* <h3>fs db</h3>
*
* <pre>
* db = fs
Expand All @@ -99,7 +104,10 @@
* regenerated. Keep in mind this db is saved in a tmp directory and db will be deleted it
* on restarts.
*
* <h2>db.url</h2> Connect to a database using a jdbc url, some examples here:
* <h3>db.url</h3>
* <p>
* Connect to a database using a jdbc url, some examples here:
* </p>
*
* <pre>
* # mysql
Expand All @@ -111,7 +119,7 @@
* Previous example, show you how to connect to <strong>mysql</strong>, setting user and password.
* But of course you need the jdbc driver on your <code>pom.xml</code>:
*
* <h2>hikari configuration</h2>
* <h3>hikari configuration</h3>
* <p>
* If you need to configure or tweak the <a
* href="https://github.com/brettwooldridge/HikariCP">hikari pool</a> just add <code>hikari.*</code>
Expand Down Expand Up @@ -149,7 +157,7 @@
* </pre>
*
* <p>
* application.conf
* application.conf:
* </p>
*
* <pre>
Expand Down
6 changes: 3 additions & 3 deletions jooby-jooq/src/test/java/org/jooby/jooq/jOOQTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class jOOQTest {
expect(binder.bind(Key.get(DSLContext.class, Names.named("db")))).andReturn(abbC);
};

private MockUnit.Block managed = unit -> {
private MockUnit.Block onStop = unit -> {
Env env = unit.get(Env.class);
expect(env.onStop(isA(CheckedRunnable.class))).andReturn(env);
};
Expand All @@ -104,7 +104,7 @@ public void defaults() throws Exception {
.expect(jdbc)
.expect(configuration)
.expect(ctx)
.expect(managed)
.expect(onStop)
.run(unit -> {
new jOOQ()
.configure(unit.get(Env.class), config(), unit.get(Binder.class));
Expand All @@ -117,7 +117,7 @@ public void doWith() throws Exception {
.expect(jdbc)
.expect(configuration)
.expect(ctx)
.expect(managed)
.expect(onStop)
.run(unit -> {
new jOOQ()
.doWith(c -> assertEquals(unit.get(Configuration.class), c))
Expand Down
64 changes: 64 additions & 0 deletions jooby-rxjava-jdbc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# rxjdbc

<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.

This module depends on [jdbc module](/doc/jdbc), make sure you read the doc of the [jdbc module](/doc/jdbc) module.

## dependency

```xml
<dependency>
<groupId>org.jooby</groupId>
<artifactId>jooby-rxjdbc</artifactId>
<version>1.0.0.CR2</version>
</dependency>
```

## exports

* A ```Database``` object
* A [Hikari](https://github.com/brettwooldridge/HikariCP) ```DataSource``` object

## usage

```java
import org.jooby.rx.RxJdbc;
import org.jooby.rx.Rx;
{
use(new RxJdbc());

get("/reactive", req ->
req.require(Database.class)
.select("select name from something where id = :id")
.parameter("id", 1)
.getAs(String.class)
).map(Rx.rx());

}
```

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).

## multiple db connections

```java
import org.jooby.rx.RxJdbc;
import org.jooby.rx.Rx;
{
use(new RxJdbc("db.main"));

use(new RxJdbc("db.audit"));

get("/", req ->

Databse db = req.require("db.main", Database.class);
Databse audit = req.require("db.audit", Database.class);
// ...
).map(Rx.rx());

}
```

For more details on how to configure the Hikari datasource, please check the [jdbc module](/doc/jdbc).

Happy coding!!!
Loading

0 comments on commit fc76f4f

Please sign in to comment.