5

在服务器发送电子邮件后,我正在尝试设置简单的成功/失败响应。

但是,即使经过数小时的尝试多种变体,我仍然没有得到正确的响应。

只是给出一个接受的响应示例代码在这里:

@GET
@Path("/async")
public CompletionStage<Response> sendASimpleEmailAsync() {
    return reactiveMailer.send(
            Mail.withText("to@acme.org", "A reactive email from quarkus", "This is my body"))
            .subscribeAsCompletionStage()
            .thenApply(x -> Response.accepted().build());
}

但是,当邮件没有成功发送时,我想在这里提供另一个回复。我试过的是这个(但这是一个没有成功的 Uni 演员):

@GET
@Path("/async")
public Uni<Void> sendASimpleEmailAsync() {
    final Mail mailToBeSent =  Mail.withText("to@acme.org", "A reactive email from quarkus", "This is my body");

    return (Uni<Void>) reactiveMailer.send(mailToBeSent)
            .then( response -> {
                if (response == null) {
                    return Response.accepted();
                }
            });
}

控制台输出(由于密码错误没有发送邮件时):

[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

还有另一个选择:

@GET
@Path("/async")
public Cancellable sendASimpleEmailAsync() {
    final Mail mailToBeSent =  Mail.withText("to@acme.org", "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); }
    );
}

控制台日志(带有 println)。它在我收到接受的客户端输出后执行。

Failure with io.vertx.core.impl.NoStackTraceThrowable: AUTH CRAM-MD5 failed 530 Invalid username or password

客户端输出(由于密码错误没有发送邮件时):

HTTP/1.1 200 OK
Content-Length: 57
Content-Type: text/plain;charset=UTF-8

io.smallrye.mutiny.helpers.UniCallbackSubscriber@3fa06fdb

但是机器人没有成功。我只想接收邮件是否已发送或发送时是否有任何错误。有人对如何进行有任何想法提示吗?

4

1 回答 1

4

您可以使用 mutiny 的onFailure().recoverWithItem()功能来指定在失败时使用的单独响应:

@GET
@Path("/async")
public Uni<Response> sendASimpleEmailAsync() {
    return reactiveMailer.send(
            Mail.withText("to@acme.org", "A reactive email from quarkus", "This is my body"))
            .map(a -> Response.accepted().build())
            .onFailure().recoverWithItem(Response.serverError().build());
}

请注意,您需要直接quarkus-resteasy-mutiny返回 aUni并避免转换为 a CompletionStage,但如果您经常这样做会更有意义。

于 2020-07-01T13:27:43.737 回答