Spring MVC 处理程序返回后调用另一个方法

Pra*_*A.K 5 spring spring-mvc spring-aop

我有一个要求,我需要从休息控制器返回一些状态/长值,然后执行代码以发送推送通知。

@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!!");    
}
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我如何实现这一目标吗?是否可以为此使用 Spring AOP?我认为线程不会保证只有在返回后才会执行 sendPushNotifiction 方法。那么有哪些方法可以有效地实现这一目标呢?提前致谢

San*_*jay 3

我认为这可能是异步处理的一个很好的用例。Spring对其有很好的支持。准确地说,你需要

  1. 注释sendPushNotifiction@Async.
  2. 用 注释一些配置类@EnableAsync
  3. sendPushNotifiction()在 return 语句之前调用。执行流程不会等待sendPushNotifiction完成。

如果不起作用,请尝试sendPushNotifiction在单独的服务中进行编码。