Spring MVC Async task runs synchronously
Hello,
I'd like to implement an asynchronous task, and a page which returns immediately and starts the task in the background. However, the page instead waits for the background task to complete and returns only afterwards. When I visit /start it takes 15s to load the page. I'm using Spring 3.2.0. I have a line containing <task:annotation-driven/> in my test-servlet.xml.
The odd thing is that even if I replace @Async with @Async("this_bean_does_not_exist"), the application does the same(though I would be expecting an exception for referencing a not existing bean).
Code:
public interface AsyncTestService {
void startSlowProcess();
}
Code:
@Service
public class AsyncTestServiceImpl implements AsyncTestService {
@Override
@Async
public void startSlowProcess() {
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
Code:
@Controller
public class TestController {
@Autowired
AsyncTestService asyncTestService;
@RequestMapping("/start")
@ResponseBody
public String startSlowProcess() {
asyncTestService.startSlowProcess(); // It takes 15s to complete
return "STARTED"; // should return immediately
}
}