我是否必须致电join()在wheComplete()中的未来?

发布于 2025-02-08 13:47:30 字数 681 浏览 0 评论 0原文

我将一些代码添加到现有端点以发送电子邮件。我们不需要发送电子邮件来返回给用户的响应的结果,因此我在期货链结束时添加了.whencomplete(),从之内。对电子邮件服务的呼叫也是异步,返回completionstage< void>

CompletionStage<SomeResponse> someEndpoint() {
  return doThings()
      .thenApply(things -> {
        return someResponseFormat(things);
      })
      .whenComplete((someResponse, ex) -> {
        if (ex == null) {
          emailClient.sendEmail(someResponse); // CompletionStage<Void>
        }
      });
}

据我了解,该任务将被安排和执行。我需要在sendemail(...)上调用join()吗?这样做会有与不打电话不同的行为吗?最好的做法是什么?

编辑:最初,我问我是否需要致电join()或get(),这被误解为“我需要打电话给什么”时,当我的意思是“我需要打电话”。 /em>

I'm adding some code to an existing endpoint to send an email. We don't need the result of sending an email to return a response to the user, so I'm adding a .whenComplete() at the end of the chain of futures, calling our email service from within. The call to the email service is also async, returning a CompletionStage<Void>.

CompletionStage<SomeResponse> someEndpoint() {
  return doThings()
      .thenApply(things -> {
        return someResponseFormat(things);
      })
      .whenComplete((someResponse, ex) -> {
        if (ex == null) {
          emailClient.sendEmail(someResponse); // CompletionStage<Void>
        }
      });
}

As I understand, that task will be scheduled and executed. Do I need to call join() on sendEmail(...)? Would doing so have a different behavior than not calling them? What is the best practice?

Edit: Originally I asked if I need to call join() or get(), which was misunderstood as "which do I need to call," when I meant, "do I need to call either at all."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很酷又爱笑 2025-02-15 13:47:30

emailClient.sendemail(someresponse)相关联的操作,无论您是否等待其完成,都将安排,因此,除非JVM同时终止,否则它将完成。但是

  • 没人会注意到操作完成何时完成或能够等待其完成。

  • 没有人会注意到该操作何时失败,但有例外。

因此,您可能想做的是,

CompletionStage<SomeResponse> someEndpoint() {
  return doThings()
      .thenApply(things -> someResponseFormat(things))
      .thenCompose(someResponse -> emailClient.sendEmail(someResponse)
          .thenApply(_void -> someResponse));
}

那么,当someendpoint() instokes join> join()的呼叫者时,join> join> join()将等待完成sendemail的完成,并在sendemail失败时报告错误。同样,当someendpoint()链条依赖操作的呼叫者时,它们将在完成sendemail完成后开始。

The operation associated with emailClient.sendEmail(someResponse) will be scheduled regardless of whether you wait for its completion, so unless the JVM terminates in the meanwhile, it will complete. But

  • Nobody will notice when the operation completed or be able to wait for its completion.

  • Nobody will notice when the operation fails with an exception.

So what you probably want to do, is

CompletionStage<SomeResponse> someEndpoint() {
  return doThings()
      .thenApply(things -> someResponseFormat(things))
      .thenCompose(someResponse -> emailClient.sendEmail(someResponse)
          .thenApply(_void -> someResponse));
}

Then, when the caller of someEndpoint() invokes join() on it, the join() would wait for the completion of the sendEmail and also report errors when sendEmail fails. Likewise, when the caller of someEndpoint() chains dependent operations, they would start after the completion of sendEmail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文