由于接口中调用的函数耗费时间很长,导致前端等待请求时间超时。
尝试改为接口调用立即返回请求,之后在后台静默执行函数。
方法
首先在config目录中配置异步执行配置文件ExecutorConfig.
然后在Service目录中新建异步接口类AsyncService,并定义函数executeAsync.
然后新建实现类AsyncServiceImpl继承AsyncService,并在其中实现executeAsync.
最后在Controller中调用executeAsync即可。
同时在后台函数执行期间,禁止该接口被调用。
参考
SpringBoot异步及线程池配置
springboot 中开启线程
使用SpringBoot的@Async实现异步调用方法,以及自己开启新线程异步调用