HTTP POST API响应,无需等待电子邮件通知完成Spring引导

问题描述 投票:0回答:1

在我的Spring引导控制器中,我有一个方法可以在后端插入一些记录,在此操作结束时,我会根据从先前操作收到的响应,通过Javax电子邮件通知用户。目前,电子邮件方法完成后,我从API得到响应。在后台进行电子邮件通知的同时完成我的第一个操作后,有什么办法可以将响应返回给客户端?

我已经尝试在邮件服务的sendemail方法中实现异步注释。但是我找不到响应时间的任何差异,并且仅在发送电子邮件后仍然可以得到响应。我的伪代码控制器:

@Autowired
private EmailService emailService;

@PostMapping(value = "create", produces = "text/plain")
private insertRecord()
{
 response = <Insert into DB>;
 sendEmail(response);
}

private sendEmail(response)
{
  //check if email should be sent and if yes
  emailservice.send(response);  
}

电子邮件服务:

@Service
public class EmailService {

@Async
public static void sendEmail(MailEvent mailEvent) throws IOException {//send 
email}
}

入门@SpringBootApplication@EnableAsync公共课程入门{...}

[在我的Spring启动控制器中,我有一个方法可以在后端插入一些记录,在此操作结束时,我会根据从...]接收到的响应通过Javax电子邮件通知用户。 [

我认为Spring Asynch是解决您的问题的方法。您是否还通过@EnableAsync启用了异步功能并创建了Executor bean?有关完整的教程,请参见本指南:https://spring.io/guides/gs/async-method/
java spring api javamail
1个回答
0
投票
我认为Spring Asynch是解决您的问题的方法。您是否还通过@EnableAsync启用了异步功能并创建了Executor bean?有关完整的教程,请参见本指南:https://spring.io/guides/gs/async-method/
© www.soinside.com 2019 - 2024. All rights reserved.