简体   繁体   English

Spring REST客户端-在方法完成之前强制响应

[英]Spring REST client - force response before method finishes

I'm writing a REST service with Spring Web 4.0.5 and one of called methods is sending e-mail (with javax mail). 我正在用Spring Web 4.0.5编写REST服务,一种被称为方法的方法是发送电子邮件(带有javax邮件)。 Sending mail takes some time, but I would like to be able to send HTTP response (no matter what response, eg 200) BEFORE this method finishes - so before the mail is sent. 发送邮件需要一些时间,但是我希望能够在此方法完成之前发送HTTP响应(无论响应是什么,例如200)-在发送邮件之前。 Is it even possible? 可能吗 Preferably without multithreading? 最好没有多线程?

@RestController
@RequestMapping(value = "/mails", produces = "application/json")
public class RestMailService{

@Autowired
MailService mailService;

@RequestMapping(value="/test", method = RequestMethod.GET)
public void sendMail(){
mailService.sendMail();
}
}

I believe all possible solutions include multithreading. 我相信所有可能的解决方案都包括多线程。 The thread will be either directly started by you or hidden behind messaging or something similar. 该线程将由您直接启动,或者隐藏在消息传递或类似内容的后面。

if you were to go with multi-threading after all please use some Executor instead of below suggested new Thread(...).start() 如果毕竟要使用多线程,请使用一些Executor而不是下面建议的new Thread(...).start()

I would also note that returning HTTP 200 before the operation finishes may somewhat confuse the user as the code suggests the operation was successful where in fact the operation maybe didn't even start yet. 我还要注意,在操作完成之前返回HTTP 200可能会使用户感到困惑,因为代码表明操作成功,而实际上操作甚至还没有开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM