Skip to content

Commit 3164993

Browse files
committed
server/module: vertx support #3778
- WIP documentation
1 parent b0e8ad9 commit 3164993

File tree

4 files changed

+184
-17
lines changed

4 files changed

+184
-17
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
== Sql Client
2+
3+
A https://github.com/eclipse-vertx/vertx-sql-client[reactive SQL Client]. Supported databases:
4+
5+
- MySQL
6+
- PostgreSQL
7+
8+
These modules require a Vertx instance available in the service registry.
9+
10+
=== MySQL client
11+
12+
1) Add the dependency:
13+
14+
[dependency, artifactId="jooby-vertx-mysql-client"]
15+
.
16+
17+
2) Install
18+
19+
.Java
20+
[source, java, role="primary"]
21+
----
22+
import io.jooby.vertx.VertxModule;
23+
import io.jooby.vertx.VertxMySQLModule;
24+
import static io.jooby.vertx.VertxHandler.vertx;
25+
26+
{
27+
install(new VertxModule()); <1>
28+
29+
use(vertx()); <2>
30+
31+
get("/{id}", ctx -> {
32+
var db = require(SqlClient.class); <3>
33+
return db.preparedQuery("SELECT id, randomnumber from WORLD where id=$1") <4>
34+
.execute(Tuple.of(ctx.path("id").longValue()))
35+
.map(result -> {
36+
var row = result.iterator().next();
37+
return new World(row.getInteger(0), row.getInteger(1));
38+
});
39+
});
40+
}
41+
----
42+
43+
.Kotlin
44+
[source, kt, role="secondary"]
45+
----
46+
import io.jooby.vertx.VertxModule
47+
import io.jooby.vertx.VertxMySQLModule
48+
import static io.jooby.vertx.VertxHandler.vertx
49+
50+
{
51+
install(VertxModule()) <1>
52+
53+
use(vertx()) <2>
54+
55+
get("/{id}") {
56+
val db = require(SqlClient::class) <3>
57+
db.preparedQuery("SELECT id, randomnumber from WORLD where id=$1") <4>
58+
.execute(Tuple.of(ctx.path("id").longValue()))
59+
.map({ result ->
60+
val row = result.iterator().next()
61+
World(row.getInteger(0), row.getInteger(1))
62+
})
63+
}
64+
}
65+
----
66+
67+
<1> Install Vertx
68+
<2> Install vertx handler. Render future, promise and buffer.
69+
<3> Get SQL client instance
70+
<4> Run a database query
71+

docs/asciidoc/modules/vertx.adoc

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,56 @@ https://vertx.io/[Vertx] Reactive applications on the JVM.
1515
[source, java, role="primary"]
1616
----
1717
import io.jooby.vertx.VertxModule;
18-
18+
import static io.jooby.vertx.VertxHandler.vertx;
1919
{
20-
install(new VertxModule()); <1>
20+
install(new VertxModule()); <1>
21+
22+
use(vertx()); <2>
2123
22-
get("/{msg}", ctx -> {
23-
var eb = require(EventBus.class); <2>
24+
get("/bus}", ctx -> {
25+
var eb = require(EventBus.class); <3>
2426
25-
eventBus.publish("msg", ctx.path("msg").value()); <3>
27+
eventBus.publish("msg", ctx.query("msg").value("Event Bus!")); <4>
2628
...
2729
});
30+
31+
get("/*filepath", ctx -> {
32+
var fs = require(FileSytem.class); <5>
33+
return fs.open(ctx.path("filepath").value(), new OpenOptions()); <6>
34+
});
2835
}
2936
----
3037

3138
.Kotlin
3239
[source, kt, role="secondary"]
33-
----
40+
----
3441
import io.jooby.vertx.VertxModule
3542
3643
{
37-
install(VertxModule()) <1>
44+
install(VertxModule()) <1>
45+
46+
use(vertx()) <2>
3847
39-
get("/{msg}") {
40-
val eb = require(EventBus.class); <2>
48+
get("/bus") {
49+
val eb = require(EventBus::class) <3>
4150
42-
eventBus.publish("msg", ctx.path("msg").value()); <3>
51+
eventBus.publish("msg", ctx.query("msg").value("Event Bus!")) <4>
4352
...
4453
}
54+
55+
get("/*filepath") {
56+
varl fs = require(FileSytem::class) <5>
57+
fs.open(ctx.path("filepath").value(), OpenOptions()) <6>
58+
}
4559
}
4660
----
4761

4862
<1> Install Vertx
49-
<2> Get EventBus or Vertx instance
50-
<3> Send message to `msg`
63+
<2> Install vertx handler. Know how to render future, promise and buffer.
64+
<3> Get EventBus instance
65+
<4> Send message to `msg`
66+
<5> Get FileSystem
67+
<6> Open a possible large file and send it to client.
5168

5269
=== Options
5370

@@ -59,12 +76,15 @@ Options can be provided manually or from application.conf properties:
5976
import io.jooby.vertx.VertxModule;
6077
6178
{
62-
install(new VertxModule(new VertxOptions()));
79+
install(new VertxModule(new VertxOptions()
80+
.setEventLoopSize(5)
81+
.setWorkerPoolSize(20)
82+
.setBlockedThreadCheckInterval(500)
83+
.setMaxEventLoopExecuteTime(2000)
84+
));
6385
}
6486
----
6587

66-
.Properties
67-
6888
.application.conf
6989
[source, properties]
7090
----
@@ -83,3 +103,21 @@ import io.jooby.vertx.VertxModule;
83103
install(new VertxModule());
84104
}
85105
----
106+
107+
A clustered vertx instance can be provided at creation time:
108+
109+
.Clustered
110+
[source, java]
111+
----
112+
import io.jooby.vertx.VertxModule;
113+
114+
{
115+
install(new VertxModule(options -> Vertx.builder()
116+
.with(options)
117+
.withClusteredManager(cm)
118+
.buildClustered()
119+
));
120+
}
121+
----
122+
123+
include::vertx-sql-client.adoc[]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package examples.vertx;
7+
8+
import static io.jooby.ExecutionMode.EVENT_LOOP;
9+
import static io.jooby.vertx.VertxHandler.vertx;
10+
11+
import java.nio.file.Paths;
12+
import java.util.Map;
13+
import java.util.UUID;
14+
15+
import io.jooby.Jooby;
16+
import io.jooby.netty.NettyServer;
17+
import io.jooby.vertx.VertxModule;
18+
import io.vertx.core.eventbus.EventBus;
19+
import io.vertx.core.file.FileSystem;
20+
import io.vertx.core.file.OpenOptions;
21+
22+
public class VertxApp extends Jooby {
23+
24+
{
25+
install(new VertxModule());
26+
var eb = require(EventBus.class);
27+
eb.consumer(
28+
"news.uk.sport",
29+
message -> {
30+
getLog().info("I have received a message: {}", message.body());
31+
});
32+
33+
use(vertx());
34+
35+
get(
36+
"/publish",
37+
ctx -> {
38+
ctx.require(EventBus.class)
39+
.publish(
40+
"news.uk.sport",
41+
Map.of("msg", ctx.query("msg").value(UUID.randomUUID().toString())));
42+
return "Sent";
43+
});
44+
45+
get(
46+
"/readme",
47+
ctx -> {
48+
var fs = ctx.require(FileSystem.class);
49+
return fs.open(
50+
Paths.get(System.getProperty("user.dir"), "pom.xml").toAbsolutePath().toString(),
51+
new OpenOptions());
52+
});
53+
}
54+
55+
public static void main(String[] args) {
56+
runApp(args, new NettyServer(), EVENT_LOOP, VertxApp::new);
57+
}
58+
}

tests/src/test/java/examples/vertx/VertxPoolApp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import io.jooby.Jooby;
1414
import io.jooby.jackson.JacksonModule;
1515
import io.jooby.netty.NettyServer;
16-
import io.jooby.vertx.VertxFutureHandler;
16+
import io.jooby.vertx.VertxHandler;
1717
import io.jooby.vertx.VertxModule;
1818
import io.jooby.vertx.pgclient.VertxPgModule;
1919
import io.vertx.core.Promise;
@@ -41,7 +41,7 @@ public class VertxPoolApp extends Jooby {
4141
install(new VertxModule());
4242
install(new VertxPgModule(PgBuilder::pool));
4343

44-
use(new VertxFutureHandler());
44+
use(new VertxHandler());
4545

4646
get(
4747
"/db",

0 commit comments

Comments
 (0)