简体   繁体   中英

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). 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. 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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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