Have you looked at the @Async annotation in the Spring reference document ?
First create a bean for your asynchronous task:
@Service public class AsyncServiceBean implements ServiceBean { private AtomicInteger cn; @Async public void doSomething() {
Then call it from the controller:
@Controller public 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()); } }
Remember to configure the artist, for example,
<task:annotation-driven executor="myExecutor"/> <task:executor id="myExecutor" pool-size="5"/>
matsev
source share