I need to process request asynchronously in time - after receiving request I must return a response with status 200 to confirm that the request have reached it's goal, and then proceed with some magic to happen in service. I tried few ways to reach it, but every time response was sent after logic part ended in some other thread.
Is there a way to reach it using Spring? Or should I rather think about other approach to this problem?
The Spring Framework provides abstractions for asynchronous execution and scheduling of tasks
You can look at this => http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html
You need to use deferredResult http://docs.spring.io/spring-framework/docs/3.2.0.BUILD-SNAPSHOT/api/org/springframework/web/context/request/async/DeferredResult.html
You will create a deferredResult object and you will return to the client. then asynchronously you will execute the logic and as soon as you finish you will inform the client that the request it´s done. This technique is also know as "http long polling"
@RequestMapping("/")
@ResponseBody
public DeferredResult<String> square() throws JMSException {
final DeferredResult<String> deferredResult = new DeferredResult<>();
runInOtherThread(deferredResult);
return deferredResult;
}
private void runInOtherThread(DeferredResult<String> deferredResult) {
//seconds later in other thread...
deferredResult.setResult("HTTP response is: 42");
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.