Skip to content

Commit

Permalink
Cleanup shutdown and expose worker in request.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Sep 2, 2020
1 parent 3590b43 commit 5497942
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.**


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ public void start()
public void stopped()
{
undertow.stop();
running.set(false);
}

public void healthy()
Expand Down Expand Up @@ -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();
Expand All @@ -228,6 +233,8 @@ public void failure(Service service)
undertow.start();

serviceManager.startAsync();


}

public void shutdown() throws TimeoutException
Expand All @@ -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.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,32 @@ public CompletableFuture<User> responseFutureUser()
{
return CompletableFuture.completedFuture( new User(123L) );
}


@GET
@Path("response/future/worker")
@Produces((MediaType.APPLICATION_JSON))
public CompletableFuture<ServerResponse<Map<String,String>>> responseFutureUser(ServerRequest request)
{

CompletableFuture<ServerResponse<Map<String,String>>> 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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down

0 comments on commit 5497942

Please sign in to comment.