我想问一下使用Spring运行长进程的最佳方法是什么.我有一个webapp,当客户端发出请求时,它运行一个Spring控制器.该Controller将从请求中获取一些参数,然后运行查询并从数据库中获取记录.
来自DB的记录很高,我需要做一个可能需要很长时间的比较逻辑,所以我需要单独运行它.执行此过程时,应将最终结果写入excel文件并邮寄.
您可以使用注释@Async立即返回。
首先,编写一个@Service类来处理您的 DB 和 Excel 作业。
@Service
public class AccountService {
@Async
public void executeTask(){
// DB and Excel job
}
}
Run Code Online (Sandbox Code Playgroud)
然后,在控制器方法中触发任务
@Controller
public class taskController{
@RequestMapping(value = "as")
@ResponseBody
public ResultInfo async() throws Exception{
accountService.executeTask();
return new ResultInfo(0, "success", null);
}
}
Run Code Online (Sandbox Code Playgroud)
最后,将此添加到 application-context.xml(spring 配置文件)
<task:annotation-driven executor="taskExecutor"/>
<task:executor id="taskExecutor" pool-size="10"/>
Run Code Online (Sandbox Code Playgroud)
希望这会帮助你。
| 归档时间: |
|
| 查看次数: |
3581 次 |
| 最近记录: |