Skip to content

Commit b0e8ad9

Browse files
committed
server/module: vertx support #3778
- Add vertx encoder - Rename some classes
1 parent 6e69ad6 commit b0e8ad9

File tree

7 files changed

+141
-12
lines changed

7 files changed

+141
-12
lines changed

modules/jooby-apt/src/main/java/io/jooby/internal/apt/ReactiveType.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,22 @@ public static List<ReactiveType> supportedTypes() {
3838
"io.jooby.ReactiveSupport",
3939
"concurrent",
4040
Set.of("java.util.concurrent.Flow", "java.util.concurrent.CompletionStage")),
41+
// Vertx
42+
new ReactiveType(
43+
"io.jooby.vertx.VertxHandler",
44+
"vertx",
45+
Set.of("io.vertx.core.Future", "io.vertx.core.Promise", "io.vertx.core.buffer.Buffer")),
46+
// Mutiny
4147
new ReactiveType(
4248
"io.jooby.mutiny.Mutiny",
4349
"mutiny",
4450
Set.of("io.smallrye.mutiny.Uni", "io.smallrye.mutiny.Multi")),
51+
// Reactor
4552
new ReactiveType(
4653
"io.jooby.reactor.Reactor",
4754
"reactor",
4855
Set.of("reactor.core.publisher.Flux", "reactor.core.publisher.Mono")),
56+
// Rxjava
4957
new ReactiveType(
5058
"io.jooby.rxjava3.Reactivex",
5159
"rx",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void install(@NonNull Jooby application) throws Exception {
3333
options = fromMap(new JsonObject(config.getObject(name).unwrapped()));
3434
}
3535

36-
var client = newBuilder().connectingTo(options).using(registry.getOrNull(Vertx.class)).build();
36+
var client = newBuilder().connectingTo(options).using(registry.require(Vertx.class)).build();
3737

3838
registry.put(ServiceKey.key(SqlClient.class, name), client);
3939
registry.putIfAbsent(SqlClient.class, client);
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.internal.vertx;
7+
8+
import java.io.IOException;
9+
import java.io.OutputStream;
10+
11+
import edu.umd.cs.findbugs.annotations.NonNull;
12+
import edu.umd.cs.findbugs.annotations.Nullable;
13+
import io.jooby.Context;
14+
import io.jooby.MessageEncoder;
15+
import io.jooby.output.Output;
16+
import io.vertx.core.Handler;
17+
import io.vertx.core.buffer.Buffer;
18+
import io.vertx.core.file.AsyncFile;
19+
20+
public class VertxEncoder implements MessageEncoder {
21+
private static class AsyncFileHandler {
22+
private Context ctx;
23+
private OutputStream out;
24+
private boolean errored;
25+
private boolean closed;
26+
27+
public AsyncFileHandler(Context ctx) {
28+
this.ctx = ctx;
29+
this.out = ctx.responseStream();
30+
}
31+
32+
public Handler<Buffer> toHandler() {
33+
return this::handle;
34+
}
35+
36+
public Handler<Void> toEndHandler() {
37+
return this::handleEnd;
38+
}
39+
40+
public Handler<Throwable> toErrorHandler() {
41+
return this::handleError;
42+
}
43+
44+
private void handleEnd(Void unused) {
45+
if (!closed) {
46+
closed = true;
47+
try {
48+
out.close();
49+
} catch (IOException ex) {
50+
handleError(ex);
51+
}
52+
}
53+
}
54+
55+
private void handleError(Throwable ex) {
56+
if (!errored) {
57+
errored = true;
58+
ctx.sendError(ex);
59+
} else {
60+
ctx.getRouter().getLog().error("Async file write resulted in exception", ex);
61+
}
62+
}
63+
64+
private void handle(Buffer buffer) {
65+
if (!closed) {
66+
try {
67+
out.write(buffer.getBytes());
68+
} catch (IOException ex) {
69+
handleError(ex);
70+
}
71+
}
72+
}
73+
}
74+
75+
@Nullable @Override
76+
public Output encode(@NonNull Context ctx, @NonNull Object value) {
77+
if (value instanceof Buffer buffer) {
78+
ctx.send(buffer.getBytes());
79+
} else if (value instanceof AsyncFile file) {
80+
var handler = new AsyncFileHandler(ctx);
81+
file.handler(handler.toHandler());
82+
file.endHandler(handler.toEndHandler());
83+
file.exceptionHandler(handler.toErrorHandler());
84+
}
85+
return null;
86+
}
87+
}

modules/jooby-vertx/src/main/java/io/jooby/vertx/VertxFutureHandler.java renamed to modules/jooby-vertx/src/main/java/io/jooby/vertx/VertxHandler.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
import io.jooby.Route;
1111
import io.vertx.core.Future;
1212
import io.vertx.core.Promise;
13+
import io.vertx.core.buffer.Buffer;
1314

14-
public class VertxFutureHandler implements Route.Filter {
15+
public class VertxHandler implements Route.Filter {
1516
@Override
1617
public @NonNull Route.Handler apply(Route.Handler next) {
1718
return ctx -> {
@@ -23,11 +24,21 @@ public class VertxFutureHandler implements Route.Filter {
2324
return futureResult(ctx, promise.future());
2425
} else if (result instanceof Future<?> future) {
2526
return futureResult(ctx, future);
27+
} else if (result instanceof Buffer buffer) {
28+
return bufferResult(ctx, buffer);
2629
}
2730
return result;
2831
};
2932
}
3033

34+
public static Route.Filter vertx() {
35+
return new VertxHandler();
36+
}
37+
38+
private Context bufferResult(Context ctx, Buffer buffer) {
39+
return ctx.render(buffer);
40+
}
41+
3142
private static Context futureResult(Context ctx, Future<?> future) {
3243
future.onComplete(
3344
ar -> {

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

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,64 @@
55
*/
66
package io.jooby.vertx;
77

8+
import java.util.function.Function;
9+
import java.util.function.Supplier;
10+
811
import edu.umd.cs.findbugs.annotations.NonNull;
912
import io.jooby.Extension;
1013
import io.jooby.Jooby;
14+
import io.jooby.internal.vertx.VertxEncoder;
15+
import io.vertx.core.Future;
1116
import io.vertx.core.Vertx;
1217
import io.vertx.core.VertxOptions;
1318
import io.vertx.core.eventbus.EventBus;
19+
import io.vertx.core.file.FileSystem;
1420
import io.vertx.core.json.JsonObject;
1521

1622
public class VertxModule implements Extension {
17-
private Vertx vertx;
23+
private VertxOptions options;
24+
private final Function<VertxOptions, Future<Vertx>> vertxFactory;
25+
26+
public VertxModule() {
27+
this(options -> Future.succeededFuture(Vertx.vertx(options)));
28+
}
1829

1930
public VertxModule(Vertx vertx) {
20-
this.vertx = vertx;
31+
this(options -> Future.succeededFuture(vertx));
2132
}
2233

2334
public VertxModule(VertxOptions options) {
24-
this.vertx = Vertx.vertx(options);
35+
this(ops -> Future.succeededFuture(Vertx.vertx(ops)));
36+
this.options = options;
37+
}
38+
39+
public VertxModule(Function<VertxOptions, Future<Vertx>> vertx) {
40+
this.vertxFactory = vertx;
2541
}
2642

27-
public VertxModule() {}
43+
public VertxModule(Supplier<Future<Vertx>> vertx) {
44+
this(options -> vertx.get());
45+
}
2846

2947
@Override
3048
public void install(@NonNull Jooby application) throws Exception {
31-
if (vertx == null) {
32-
var config = application.getConfig();
33-
VertxOptions options;
49+
var config = application.getConfig();
50+
if (options == null) {
3451
if (config.hasPath("vertx")) {
3552
options = new VertxOptions(new JsonObject(config.getObject("vertx").unwrapped()));
3653
} else {
3754
options = new VertxOptions();
3855
}
39-
vertx = Vertx.vertx(options);
4056
}
57+
var vertxFuture = vertxFactory.apply(options);
58+
var vertx = vertxFuture.await();
4159
var registry = application.getServices();
4260
registry.put(Vertx.class, vertx);
4361
registry.put(EventBus.class, vertx.eventBus());
62+
registry.put(FileSystem.class, vertx.fileSystem());
63+
64+
application.encoder(new VertxEncoder());
65+
4466
application.onStop(() -> vertx.close().await());
4567
}
4668
}

modules/jooby-vertx/src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88
requires io.netty.transport;
99
requires org.slf4j;
1010
requires static com.github.spotbugs.annotations;
11+
requires jakarta.inject;
1112
}

tests/src/test/java/examples/vertx/VertxApp.java renamed to tests/src/test/java/examples/vertx/VertxSqlConnectionApp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import io.vertx.sqlclient.*;
2525
import io.vertx.sqlclient.impl.SqlClientInternal;
2626

27-
public class VertxApp extends Jooby {
27+
public class VertxSqlConnectionApp extends Jooby {
2828
private static final String SELECT_WORLD = "SELECT id, randomnumber from WORLD where id=$1";
2929
private static final String SELECT_FORTUNE = "SELECT id, message from FORTUNE";
3030

@@ -182,6 +182,6 @@ public static void main(String[] args) {
182182
args,
183183
new VertxServer().setOptions(new ServerOptions().setIoThreads(2)),
184184
EVENT_LOOP,
185-
VertxApp::new);
185+
VertxSqlConnectionApp::new);
186186
}
187187
}

0 commit comments

Comments
 (0)