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; + } }