From 54979422dab4bff4e5687e3071c958a2aac23efa Mon Sep 17 00:00:00 2001 From: Joshua Bauer Date: Wed, 2 Sep 2020 11:34:03 -0700 Subject: [PATCH] Cleanup shutdown and expose worker in request. --- CHANGELOG.md | 10 +++++++ .../sinistral/proteus/ProteusApplication.java | 13 ++++++++- .../proteus/server/ServerRequest.java | 11 ++++++++ .../proteus/test/controllers/Tests.java | 27 ++++++++++++++++++- .../test/server/TestControllerEndpoints.java | 7 +++++ 5 files changed, 66 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a9ea1e..ed9911d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,16 @@ Proteus Changelog. ## Unreleased ### No issue +**Set config log level to trace.** + + +[3590b4337ce241c](https://github.com/noboomu/proteus/commit/3590b4337ce241c) Joshua Bauer *2020-08-31 21:15:19* + +**Better generic parameter handling.** + + +[4917d3016811cec](https://github.com/noboomu/proteus/commit/4917d3016811cec) Joshua Bauer *2020-08-31 19:35:07* + **Improve error handling.** diff --git a/proteus-core/src/main/java/io/sinistral/proteus/ProteusApplication.java b/proteus-core/src/main/java/io/sinistral/proteus/ProteusApplication.java index 2149926..a3aaced 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/ProteusApplication.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/ProteusApplication.java @@ -165,7 +165,6 @@ public void start() public void stopped() { undertow.stop(); - running.set(false); } public void healthy() @@ -211,6 +210,12 @@ public void failure(Service service) Runtime.getRuntime().addShutdownHook(new Thread(() -> { + + if (!this.isRunning()) { + log.warn("Server is not running..."); + return; + } + try { shutdown(); mainThread.join(); @@ -228,6 +233,8 @@ public void failure(Service service) undertow.start(); serviceManager.startAsync(); + + } public void shutdown() throws TimeoutException @@ -237,10 +244,14 @@ public void shutdown() throws TimeoutException return; } + + log.info("Shutting down..."); serviceManager.stopAsync().awaitStopped(1, TimeUnit.SECONDS); + this.running.set(false); + log.info("Shutdown complete."); } diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/ServerRequest.java b/proteus-core/src/main/java/io/sinistral/proteus/server/ServerRequest.java index 30ef312..f2ebfa7 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/ServerRequest.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/ServerRequest.java @@ -15,7 +15,9 @@ import io.undertow.server.handlers.form.FormEncodedDataDefinition; import io.undertow.server.handlers.form.MultiPartParserDefinition; import io.undertow.util.*; +import org.xnio.XnioExecutor; import org.xnio.XnioIoThread; +import org.xnio.XnioWorker; import org.xnio.channels.StreamSinkChannel; import org.xnio.channels.StreamSourceChannel; import org.xnio.conduits.StreamSinkConduit; @@ -30,6 +32,7 @@ import java.util.Deque; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.concurrent.Executor; /** @@ -249,6 +252,14 @@ public HttpServerExchange getExchange() return exchange; } + /** + * @return the worker + */ + public XnioWorker getWorker() + { + return Optional.ofNullable(exchange.getConnection()).filter(ServerConnection::isOpen).map(ServerConnection::getWorker).orElse(null); + } + /** * @return the path */ diff --git a/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java b/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java index b522a31..d706289 100644 --- a/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java +++ b/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java @@ -487,7 +487,32 @@ public CompletableFuture responseFutureUser() { return CompletableFuture.completedFuture( new User(123L) ); } - + + @GET + @Path("response/future/worker") + @Produces((MediaType.APPLICATION_JSON)) + public CompletableFuture>> responseFutureUser(ServerRequest request) + { + + CompletableFuture>> future = new CompletableFuture<>(); + + request.getWorker().execute( () -> { + + try + { + Thread.sleep(2000L); + + future.complete(response(Map.of("status","OK")).applicationJson().ok()); + + } catch( Exception e ) + { + future.completeExceptionally(e); + } + }); + + return future; + } + @GET @Path("response/parameters/complex/{pathLong}") @Produces((MediaType.APPLICATION_JSON)) diff --git a/proteus-core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java b/proteus-core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java index cbc6874..873aacc 100644 --- a/proteus-core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java +++ b/proteus-core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java @@ -239,6 +239,13 @@ public void responseUserJson() assertThat(user.getId(), CoreMatchers.is(123L)); } + @Test + public void responseWorkerFuture() + { + Map response = given().accept(ContentType.JSON).when().get("tests/response/future/worker").as(Map.class); + assertThat(response.get("status").toString(), CoreMatchers.is("OK")); + } + @Test public void responseUserXml() {