我是否必须致电join()在wheComplete()中的未来?
我将一些代码添加到现有端点以发送电子邮件。我们不需要发送电子邮件来返回给用户的响应的结果,因此我在期货链结束时添加了.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与
emailClient.sendemail(someresponse)
相关联的操作,无论您是否等待其完成,都将安排,因此,除非JVM同时终止,否则它将完成。但是没人会注意到操作完成何时完成或能够等待其完成。
没有人会注意到该操作何时失败,但有例外。
因此,您可能想做的是,
那么,当
someendpoint()
instokesjoin> 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. ButNobody 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
Then, when the caller of
someEndpoint()
invokesjoin()
on it, thejoin()
would wait for the completion of thesendEmail
and also report errors whensendEmail
fails. Likewise, when the caller ofsomeEndpoint()
chains dependent operations, they would start after the completion ofsendEmail
.