Skip to content

Commit 9e65c4b

Browse files
committed
server/module: vertx support #3778
- add future handler - WIP documentation of vertx
1 parent 82967d6 commit 9e65c4b

File tree

7 files changed

+114
-82
lines changed

7 files changed

+114
-82
lines changed

modules/jooby-vertx-mysql-client/src/main/java/io/jooby/vertx/mysqlclient/VertxMySQLModule.java

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,23 @@
1010
import edu.umd.cs.findbugs.annotations.NonNull;
1111
import io.jooby.vertx.sqlclient.VertxSqlClientModule;
1212
import io.vertx.core.json.JsonObject;
13-
import io.vertx.mysqlclient.MySQLBuilder;
1413
import io.vertx.mysqlclient.MySQLConnectOptions;
1514
import io.vertx.sqlclient.*;
1615

17-
public class VertxMySQLModule<C extends SqlClient> extends VertxSqlClientModule<C> {
16+
public class VertxMySQLModule extends VertxSqlClientModule {
1817

19-
private final Supplier<ClientBuilder<C>> builder;
18+
private final Supplier<ClientBuilder<? extends SqlClient>> builder;
2019

21-
protected VertxMySQLModule(@NonNull String name, @NonNull Supplier<ClientBuilder<C>> builder) {
20+
public VertxMySQLModule(
21+
@NonNull String name, @NonNull Supplier<ClientBuilder<? extends SqlClient>> builder) {
2222
super(name);
2323
this.builder = builder;
2424
}
2525

26+
public VertxMySQLModule(@NonNull Supplier<ClientBuilder<? extends SqlClient>> builder) {
27+
this("db", builder);
28+
}
29+
2630
@Override
2731
protected SqlConnectOptions fromMap(JsonObject config) {
2832
return new MySQLConnectOptions(config);
@@ -34,31 +38,7 @@ protected SqlConnectOptions fromUri(String uri) {
3438
}
3539

3640
@Override
37-
protected ClientBuilder<C> newBuilder() {
41+
protected ClientBuilder<? extends SqlClient> newBuilder() {
3842
return builder.get();
3943
}
40-
41-
public static VertxMySQLModule<Pool> pool() {
42-
return pool(new PoolOptions());
43-
}
44-
45-
public static VertxMySQLModule<Pool> pool(PoolOptions options) {
46-
return pool("db", options);
47-
}
48-
49-
public static VertxMySQLModule<Pool> pool(String name) {
50-
return pool(name, new PoolOptions());
51-
}
52-
53-
public static VertxMySQLModule<Pool> pool(String name, PoolOptions options) {
54-
return new VertxMySQLModule<>(name, () -> MySQLBuilder.pool().with(options));
55-
}
56-
57-
public static VertxMySQLModule<SqlClient> client() {
58-
return client("db");
59-
}
60-
61-
public static VertxMySQLModule<SqlClient> client(String name) {
62-
return new VertxMySQLModule<>(name, MySQLBuilder::client);
63-
}
6444
}

modules/jooby-vertx-pg-client/src/main/java/io/jooby/vertx/pgclient/VertxPgModule.java

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,23 @@
1010
import edu.umd.cs.findbugs.annotations.NonNull;
1111
import io.jooby.vertx.sqlclient.VertxSqlClientModule;
1212
import io.vertx.core.json.JsonObject;
13-
import io.vertx.pgclient.PgBuilder;
1413
import io.vertx.pgclient.PgConnectOptions;
1514
import io.vertx.sqlclient.*;
1615

17-
public class VertxPgModule<C extends SqlClient> extends VertxSqlClientModule<C> {
16+
public class VertxPgModule extends VertxSqlClientModule {
1817

19-
private final Supplier<ClientBuilder<C>> builder;
18+
private final Supplier<ClientBuilder<? extends SqlClient>> builder;
2019

21-
protected VertxPgModule(@NonNull String name, @NonNull Supplier<ClientBuilder<C>> builder) {
20+
public VertxPgModule(
21+
@NonNull String name, @NonNull Supplier<ClientBuilder<? extends SqlClient>> builder) {
2222
super(name);
2323
this.builder = builder;
2424
}
2525

26+
public VertxPgModule(@NonNull Supplier<ClientBuilder<? extends SqlClient>> builder) {
27+
this("db", builder);
28+
}
29+
2630
@Override
2731
protected SqlConnectOptions fromMap(JsonObject config) {
2832
return new PgConnectOptions(config);
@@ -34,31 +38,7 @@ protected SqlConnectOptions fromUri(String uri) {
3438
}
3539

3640
@Override
37-
protected ClientBuilder<C> newBuilder() {
41+
protected ClientBuilder<? extends SqlClient> newBuilder() {
3842
return builder.get();
3943
}
40-
41-
public static VertxPgModule<Pool> pool() {
42-
return pool(new PoolOptions());
43-
}
44-
45-
public static VertxPgModule<Pool> pool(PoolOptions options) {
46-
return pool("db", options);
47-
}
48-
49-
public static VertxPgModule<Pool> pool(String name) {
50-
return pool(name, new PoolOptions());
51-
}
52-
53-
public static VertxPgModule<Pool> pool(String name, PoolOptions options) {
54-
return new VertxPgModule<>(name, () -> PgBuilder.pool().with(options));
55-
}
56-
57-
public static VertxPgModule<SqlClient> client() {
58-
return client("db");
59-
}
60-
61-
public static VertxPgModule<SqlClient> client(String name) {
62-
return new VertxPgModule<>(name, PgBuilder::client);
63-
}
6444
}

modules/jooby-vertx-sql-client/src/main/java/io/jooby/vertx/sqlclient/VertxSqlClientModule.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@
1313
import io.vertx.core.Vertx;
1414
import io.vertx.core.json.JsonObject;
1515
import io.vertx.sqlclient.*;
16-
import io.vertx.sqlclient.impl.SqlClientInternal;
1716

18-
public abstract class VertxSqlClientModule<C extends SqlClient> implements Extension {
17+
public abstract class VertxSqlClientModule implements Extension {
1918
private final String name;
2019

2120
public VertxSqlClientModule(@NonNull String name) {
@@ -42,10 +41,6 @@ public void install(@NonNull Jooby application) throws Exception {
4241
registry.put(ServiceKey.key(Pool.class, name), pool);
4342
registry.putIfAbsent(Pool.class, pool);
4443
}
45-
if (client instanceof SqlClientInternal sci) {
46-
registry.put(ServiceKey.key(SqlClientInternal.class, name), sci);
47-
registry.putIfAbsent(SqlClientInternal.class, sci);
48-
}
4944
// Shutdown
5045
application.onStop(() -> client.close().await());
5146
}
@@ -54,5 +49,5 @@ public void install(@NonNull Jooby application) throws Exception {
5449

5550
protected abstract SqlConnectOptions fromUri(String uri);
5651

57-
protected abstract ClientBuilder<C> newBuilder();
52+
protected abstract ClientBuilder<? extends SqlClient> newBuilder();
5853
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 io.jooby.vertx;
7+
8+
import edu.umd.cs.findbugs.annotations.NonNull;
9+
import io.jooby.Context;
10+
import io.jooby.Route;
11+
import io.vertx.core.Future;
12+
import io.vertx.core.Promise;
13+
14+
public class VertxFutureHandler implements Route.Filter {
15+
@Override
16+
public @NonNull Route.Handler apply(Route.Handler next) {
17+
return ctx -> {
18+
var result = next.apply(ctx);
19+
if (ctx.isResponseStarted()) {
20+
// Return context to mark as handled
21+
return ctx;
22+
} else if (result instanceof Promise<?> promise) {
23+
return futureResult(ctx, promise.future());
24+
} else if (result instanceof Future<?> future) {
25+
return futureResult(ctx, future);
26+
}
27+
return result;
28+
};
29+
}
30+
31+
private static Context futureResult(Context ctx, Future<?> future) {
32+
future.onComplete(
33+
ar -> {
34+
if (ar.succeeded()) {
35+
ctx.render(ar.result());
36+
} else {
37+
ctx.sendError(ar.cause());
38+
}
39+
});
40+
return ctx;
41+
}
42+
}

modules/jooby-vertx/src/main/java/io/jooby/vertx/VertxModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.jooby.Jooby;
1111
import io.vertx.core.Vertx;
1212
import io.vertx.core.VertxOptions;
13+
import io.vertx.core.eventbus.EventBus;
1314
import io.vertx.core.json.JsonObject;
1415

1516
public class VertxModule implements Extension {
@@ -39,6 +40,7 @@ public void install(@NonNull Jooby application) throws Exception {
3940
}
4041
var registry = application.getServices();
4142
registry.put(Vertx.class, vertx);
43+
registry.put(EventBus.class, vertx.eventBus());
4244
application.onStop(() -> vertx.close().await());
4345
}
4446
}

modules/jooby-vertx/src/main/java/io/jooby/vertx/VertxServer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import io.jooby.netty.NettyServer;
1515
import io.vertx.core.Vertx;
1616
import io.vertx.core.VertxOptions;
17+
import io.vertx.core.eventbus.EventBus;
1718

1819
public class VertxServer extends NettyServer {
1920
private Vertx vertx;
@@ -36,7 +37,9 @@ public VertxServer() {}
3637
new VertxOptions().setPreferNativeTransport(true).setEventLoopPoolSize(nThreads);
3738
this.vertx = Vertx.vertx(options);
3839
}
39-
application.getServices().put(Vertx.class, vertx);
40+
var registry = application.getServices();
41+
registry.put(Vertx.class, vertx);
42+
registry.put(EventBus.class, vertx.eventBus());
4043
return super.init(application);
4144
}
4245

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

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,19 @@
55
*/
66
package examples.vertx;
77

8-
import static io.jooby.MediaType.JSON;
8+
import java.util.ArrayList;
9+
import java.util.List;
910

1011
import com.typesafe.config.ConfigFactory;
1112
import com.typesafe.config.ConfigValueFactory;
1213
import io.jooby.Jooby;
14+
import io.jooby.jackson.JacksonModule;
1315
import io.jooby.netty.NettyServer;
16+
import io.jooby.vertx.VertxFutureHandler;
1417
import io.jooby.vertx.VertxModule;
1518
import io.jooby.vertx.pgclient.VertxPgModule;
19+
import io.vertx.core.Promise;
20+
import io.vertx.pgclient.PgBuilder;
1621
import io.vertx.sqlclient.SqlClient;
1722
import io.vertx.sqlclient.Tuple;
1823

@@ -31,31 +36,56 @@ public class VertxPoolApp extends Jooby {
3136
.withValue("db.user", ConfigValueFactory.fromAnyRef("benchmarkdbuser"))
3237
.withValue(
3338
"db.password", ConfigValueFactory.fromAnyRef("benchmarkdbpass"))));
39+
40+
install(new JacksonModule());
3441
install(new VertxModule());
35-
install(VertxPgModule.client());
42+
install(new VertxPgModule(PgBuilder::pool));
43+
44+
use(new VertxFutureHandler());
3645

3746
get(
3847
"/db",
3948
ctx -> {
40-
require(SqlClient.class)
41-
.preparedQuery("SELECT id, randomnumber from WORLD where id=$1")
49+
var db = require(SqlClient.class);
50+
return db.preparedQuery("SELECT id, randomnumber from WORLD where id=$1")
4251
.execute(Tuple.of(Util.boxedRandomWorld()))
43-
.onComplete(
44-
rsp -> {
45-
if (rsp.succeeded()) {
46-
var rs = rsp.result().iterator();
47-
var row = rs.next();
48-
ctx.setResponseType(JSON)
49-
.send(Json.encode(new World(row.getInteger(0), row.getInteger(1))));
50-
} else {
51-
ctx.sendError(rsp.cause());
52-
}
52+
.map(
53+
result -> {
54+
var row = result.iterator().next();
55+
return new World(row.getInteger(0), row.getInteger(1));
5356
});
54-
return ctx;
57+
});
58+
59+
get(
60+
"/queries",
61+
ctx -> {
62+
int queries = Util.queries(ctx);
63+
var db = require(SqlClient.class);
64+
Promise<List<World>> promise = Promise.promise();
65+
var statement = db.preparedQuery("SELECT id, randomnumber from WORLD where id=$1");
66+
var worlds = new ArrayList<World>(queries);
67+
for (int i = 0; i < queries; i++) {
68+
statement
69+
.execute(Tuple.of(Util.boxedRandomWorld()))
70+
.onComplete(
71+
ar -> {
72+
if (ar.succeeded()) {
73+
var rs = ar.result();
74+
worlds.add(
75+
new World(rs.iterator().next().getInteger(0), Util.boxedRandomWorld()));
76+
if (worlds.size() == queries) {
77+
promise.complete(worlds);
78+
}
79+
} else {
80+
promise.fail(ar.cause());
81+
}
82+
});
83+
}
84+
return promise;
5585
});
5686
}
5787

58-
public static void main(String[] args) {
88+
public static void main(String[] args) throws InterruptedException {
5989
runApp(args, new NettyServer(), VertxPoolApp::new);
6090
}
6191
}

0 commit comments

Comments
 (0)