使用Spring运行长流程的最佳方法是什么



我想问一下,使用Spring运行长流程的最佳方法是什么。我有一个网络应用程序,当客户端发出请求时,它会运行一个Spring控制器。该控制器将从请求中获取一些参数,然后运行查询并从DB中获取记录。

DB中的记录很高,我需要做一个比较逻辑,这可能需要很长时间,所以我需要单独运行它。执行此过程时,应将最终结果写入excel文件并邮寄。

您可以使用注释@Async立即返回。

Fisrt,编写一个@Service类来处理您的DB和Excel作业。

@Service
public class AccountService {
    @Async
    public void executeTask(){
    // DB and Excel job
    }
}

然后,在控制器方法中触发任务

@Controller
public class taskController{
    @RequestMapping(value = "as")
    @ResponseBody
    public ResultInfo async() throws Exception{
        accountService.executeTask();
        return new ResultInfo(0, "success", null);
    }
}

最后,将其添加到applicationcontext.xml(spring-config文件)中

<task:annotation-driven executor="taskExecutor"/>
<task:executor id="taskExecutor" pool-size="10"/>

希望这对你有帮助。

相关内容

最新更新