Skip to content

Commit

Permalink
Replace whenComplete in async.
Browse files Browse the repository at this point in the history
  • Loading branch information
noboomu committed Sep 15, 2020
1 parent 6482aac commit 9653432
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ Proteus Changelog.
## Unreleased
### No issue

**Ensure worker is used for async dispatch.**


[6482aac600edd62](https://github.com/noboomu/proteus/commit/6482aac600edd62) Joshua Bauer *2020-09-15 17:16:40*

**Send**


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla


if (m.getReturnType().equals(ServerResponse.class)) {
methodBuilder.addStatement("$L.send(this,$L)", "response", "exchange");
methodBuilder.addStatement("$L.send($L)", "response", "exchange");

} else if ((m.getGenericReturnType().toString().contains("java.util.concurrent.CompletionStage") && m.getGenericReturnType().toString().contains("ServerResponse"))
|| (m.getGenericReturnType().toString().contains("java.util.concurrent.CompletableFuture") && m.getGenericReturnType().toString().contains("ServerResponse")))
Expand All @@ -687,13 +687,20 @@ protected void addClassMethodHandlers(TypeSpec.Builder typeBuilder, Class<?> cla
methodBuilder.beginControlFlow("", "");

methodBuilder.addCode(
"$L.thenAccept( r -> r.send($L) )\n\t.exceptionally( ex -> ",
"response", "exchange");
methodBuilder.beginControlFlow("", "");
methodBuilder.addCode("\t\tthrow new java.util.concurrent.CompletionException(ex);\n\t");
methodBuilder.endControlFlow(")", "");
"$L.whenComplete( (r,ex) -> ",
"response");
methodBuilder.beginControlFlow("", "");

methodBuilder.beginControlFlow("if(ex != null)");
methodBuilder.addCode("\t\texchange.putAttachment(io.undertow.server.handlers.ExceptionHandler.THROWABLE, ex);");
methodBuilder.addCode("\t\texchange.setResponseCode(500);\n\t");
methodBuilder.addCode("\t\texchange.endExchange();\n\t");
methodBuilder.nextControlFlow("else");
methodBuilder.addCode("\t\tr.send($L);","exchange");
methodBuilder.endControlFlow();
methodBuilder.endControlFlow(")", "");
methodBuilder.endControlFlow(")", "");

methodBuilder.endControlFlow(")", "");
}
else if (m.getReturnType().getTypeName().contains("java.util.concurrent.CompletionStage")
|| m.getReturnType().getTypeName().contains("java.util.concurrent.CompletableFuture"))
Expand All @@ -715,19 +722,26 @@ else if (m.getReturnType().getTypeName().contains("java.util.concurrent.Completi
postProcess = ".";
}
}

methodBuilder.addCode("exchange.dispatch( exchange.getConnection().getWorker(), () -> ");
methodBuilder.beginControlFlow("", "");


methodBuilder.addCode(
"$L.thenAccept( r -> io.sinistral.proteus.server.ServerResponse.response(r)" + postProcess + "send($L) )\n\t.exceptionally( ex -> ",
"response", "exchange");
"$L.whenComplete( (r,ex) -> ",
"response");
methodBuilder.beginControlFlow("", "");
methodBuilder.addCode("\t\tthrow new java.util.concurrent.CompletionException(ex);\n\t");
methodBuilder.endControlFlow(")", "");

methodBuilder.beginControlFlow("if(ex != null)");
methodBuilder.addCode("\texchange.putAttachment(io.undertow.server.handlers.ExceptionHandler.THROWABLE, ex);\n");
methodBuilder.addCode("\texchange.setResponseCode(500);\n");
methodBuilder.addCode("\texchange.endExchange();\n");
methodBuilder.nextControlFlow("else");
methodBuilder.addCode("\t\tio.sinistral.proteus.server.ServerResponse.response(r)" + postProcess + "send($L);","exchange");
methodBuilder.endControlFlow();
methodBuilder.endControlFlow(")", "");

methodBuilder.endControlFlow(")", "");



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,14 @@ public boolean handleDefaultResponse(HttpServerExchange exchange)
errorMap.put("exceptionClass", throwable.getClass().getName());
errorMap.put("message", throwable.getMessage());
errorMap.put("path", path);
errorMap.put("code", Integer.toString(exchange.getStatusCode()));


if(statusCode < 300)
{
statusCode = 500;
}

errorMap.put("code", Integer.toString(statusCode));

if(throwable.getCause() != null)
{
Expand All @@ -106,7 +113,7 @@ public boolean handleDefaultResponse(HttpServerExchange exchange)
}


if (throwable.getStackTrace() != null && exchange.getStatusCode() >= 500 ) {
if (throwable.getStackTrace() != null && statusCode >= 500 ) {

log.error("path: " + path, throwable);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ public CompletableFuture<ServerResponse<Map<String,String>>> responseFutureUser(

try
{

Thread.sleep(2000L);

future.complete(response(Map.of("status","OK")).applicationJson().ok());
Expand Down

0 comments on commit 9653432

Please sign in to comment.