From cd23a594caa8aebb6fe0db663a5f1f4ffc4d7905 Mon Sep 17 00:00:00 2001 From: Francesco Guardiani Date: Mon, 10 Jun 2024 13:06:24 +0200 Subject: [PATCH] Fix #351, buildAndListen now fails (#353) --- .../vertx/RestateHttpEndpointBuilder.java | 45 ++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/sdk-http-vertx/src/main/java/dev/restate/sdk/http/vertx/RestateHttpEndpointBuilder.java b/sdk-http-vertx/src/main/java/dev/restate/sdk/http/vertx/RestateHttpEndpointBuilder.java index 30165003..fc3ec967 100644 --- a/sdk-http-vertx/src/main/java/dev/restate/sdk/http/vertx/RestateHttpEndpointBuilder.java +++ b/sdk-http-vertx/src/main/java/dev/restate/sdk/http/vertx/RestateHttpEndpointBuilder.java @@ -13,12 +13,13 @@ import dev.restate.sdk.core.RestateEndpoint; import dev.restate.sdk.core.manifest.EndpointManifestSchema; import io.opentelemetry.api.OpenTelemetry; -import io.vertx.core.AsyncResult; +import io.vertx.core.Future; import io.vertx.core.Vertx; import io.vertx.core.http.Http2Settings; import io.vertx.core.http.HttpServer; import io.vertx.core.http.HttpServerOptions; import java.util.*; +import java.util.concurrent.CompletionException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -120,17 +121,29 @@ public RestateHttpEndpointBuilder withRequestIdentityVerifier( return this; } - /** Build and listen on the specified port. */ - public void buildAndListen(int port) { - build().listen(port).onComplete(RestateHttpEndpointBuilder::handleStart); + /** + * Build and listen on the specified port. + * + *

NOTE: this method will block for opening the socket and reserving the port. If you need a + * non-blocking variant, manually {@link #build()} the server and start listening it. + * + * @return The listening port + */ + public int buildAndListen(int port) { + return handleStart(build().listen(port)); } /** * Build and listen on the port specified by the environment variable {@code PORT}, or * alternatively on the default {@code 9080} port. + * + *

NOTE: this method will block for opening the socket and reserving the port. If you need a + * non-blocking variant, manually {@link #build()} the server and start listening it. + * + * @return The listening port */ - public void buildAndListen() { - build().listen().onComplete(RestateHttpEndpointBuilder::handleStart); + public int buildAndListen() { + return handleStart(build().listen()); } /** Build the {@link HttpServer} serving the Restate service endpoint. */ @@ -145,11 +158,21 @@ public HttpServer build() { return server; } - private static void handleStart(AsyncResult ar) { - if (ar.succeeded()) { - LOG.info("Restate HTTP Endpoint server started on port {}", ar.result().actualPort()); - } else { - LOG.error("Restate HTTP Endpoint server start failed", ar.cause()); + private static int handleStart(Future fut) { + try { + HttpServer server = fut.toCompletionStage().toCompletableFuture().join(); + LOG.info("Restate HTTP Endpoint server started on port {}", server.actualPort()); + return server.actualPort(); + } catch (CompletionException e) { + LOG.error("Restate HTTP Endpoint server start failed", e.getCause()); + sneakyThrow(e.getCause()); + // This is never reached + return -1; } } + + @SuppressWarnings("unchecked") + private static void sneakyThrow(Throwable e) throws E { + throw (E) e; + } }