您是否已查看Spring参考文档中的
@Async注释?
首先,为您的异步任务创建一个bean:
@Servicepublic class AsyncServiceBean implements ServiceBean { private AtomicInteger cn; @Async public void doSomething() { // triggers the async task, which updates the cn status accordingly } public Integer getCn() { return cn.get(); }}
@Controllerpublic class YourController { private final ServiceBean bean; @Autowired YourController(ServiceBean bean) { this.bean = bean; } @RequestMapping(value = "/trigger") void triggerAsyncJob() { bean.doSomething(); } @RequestMapping(value = "/status") @ResponseBody Map<String, Integer> fetchStatus() { return Collections.singletonMap("cn", bean.getCn()); } }
记住要相应地配置一个执行器,例如
<task:annotation-driven executor="myExecutor"/><task:executor id="myExecutor" pool-size="5"/>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)