我有一个要求,我需要从休息 Controller 返回一些状态/长值,然后执行代码以发送推送通知。
@RequestMapping(value="/create")
public String createTicket() throws InterruptedException {
// code to create ticket
return "ticket created";
// need to call sendPushNotifiction() after I return status
}
public void sendPushNotifiction() throws InterruptedException {
// code to send push notification
System.out.println("Sent push notification successfully!!");
}
有人可以告诉我如何实现这一目标吗?是否可以为此使用 Spring AOP?我认为线程不会保证只有在返回后才会执行 sendPushNotifiction 方法。那么有哪些方法可以有效地实现这一目标呢?
提前致谢
最佳答案
我认为这可能是异步处理的一个很好的用例。春有good support为了它。准确地说,你需要
sendPushNotifiction
与 @Async
. @EnableAsync
注释一些配置类. sendPushNotifiction()
在 return 语句之前。执行流程不会等待 sendPushNotifiction
完成。 如果它不起作用,请尝试编码
sendPushNotifiction
在单独的服务中。
关于spring - Spring MVC 处理程序返回后调用另一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33184063/