Reputation: 477
I'm trying to setup a simple success / failure response after a email was sent by the server.
However, even after hours of trying many variants, I still doesn't get the correct response.
The example code that just gives a response accepted is here:
@GET
@Path("/async")
public CompletionStage<Response> sendASimpleEmailAsync() {
return reactiveMailer.send(
Mail.withText("[email protected]", "A reactive email from quarkus", "This is my body"))
.subscribeAsCompletionStage()
.thenApply(x -> Response.accepted().build());
}
However, when the mail was not successfully sent, I want to provide another response here. What I have tried is this (but this is a cast to Uni that doesn't succeed):
@GET
@Path("/async")
public Uni<Void> sendASimpleEmailAsync() {
final Mail mailToBeSent = Mail.withText("[email protected]", "A reactive email from quarkus", "This is my body");
return (Uni<Void>) reactiveMailer.send(mailToBeSent)
.then( response -> {
if (response == null) {
return Response.accepted();
}
});
}
Console output (when no mail was sent because of wrong password):
[ERROR] Failed to execute goal io.quarkus:quarkus-maven-plugin:1.5.1.Final:dev (default-cli) on project h21-microservices: Unable to execute mojo: Compilation failure:
[ERROR] /FeedbackResource.java:[36,32] lambda body is neither value nor void compatible
[ERROR] /FeedbackResource.java:[36,13] method then in interface io.smallrye.mutiny.Uni<T> cannot be applied to given types;
[ERROR] required: java.util.function.Function<io.smallrye.mutiny.Uni<java.lang.Void>,O>
[ERROR] found: (response)[...]; } }
[ERROR] reason: cannot infer type-variable(s) O
[ERROR] (argument mismatch; bad return type in lambda expression
[ERROR] missing return value)
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
And another option:
@GET
@Path("/async")
public Cancellable sendASimpleEmailAsync() {
final Mail mailToBeSent = Mail.withText("[email protected]", "A reactive email from quarkus", "This is my body");
Uni<Void> stage = reactiveMailer.send(mailToBeSent);
return stage.subscribe().with(
result -> { System.out.println("Result with " + result); Response.accepted(); },
failure -> { System.out.println("Failure with " + failure); Response.status(Status.BAD_GATEWAY); }
);
}
Console log (with println). It is executed after I receive the client output accepted.
Failure with io.vertx.core.impl.NoStackTraceThrowable: AUTH CRAM-MD5 failed 530 Invalid username or password
Client output (when no mail was sent because of wrong password):
HTTP/1.1 200 OK
Content-Length: 57
Content-Type: text/plain;charset=UTF-8
io.smallrye.mutiny.helpers.UniCallbackSubscriber@3fa06fdb
But bot with no success. I just want to receive if the mail was sent or if there was any error by sending it. Does anybody has any ideas tips on how to proceed?
Upvotes: 6
Views: 5052
Reputation: 72294
You can use mutiny's onFailure().recoverWithItem()
functionality to specify a separate response to use on failure:
@GET
@Path("/async")
public Uni<Response> sendASimpleEmailAsync() {
return reactiveMailer.send(
Mail.withText("[email protected]", "A reactive email from quarkus", "This is my body"))
.map(a -> Response.accepted().build())
.onFailure().recoverWithItem(Response.serverError().build());
}
Note that you'll need quarkus-resteasy-mutiny
to return a Uni
directly and avoid converting to a CompletionStage
, but it makes much more sense if you're doing this regularly.
Upvotes: 5