简体   繁体   中英

Is there analog of AsyncContext in spring mvc?(how to write http response in another thread)

I have google about spring support of servlet 3.0/3.1 specification and most of information I have found at this article: Understanding Callable and Spring DeferredResult

Here author say that you can return Callable or DefferedResult from controller and say it is servlet 3.0/3.1 maintain in spring.

But I don't understand how to apply it in my situation:

I have external system and I get result from this system asynchrounously.

In controller I write something like this:

externalSystenm.send(requestId, message);

and I have another thread where I get result:

Message m = externalSystem.get();
m.getRequestId();// According this id I can map message to request

I know that in servlet API I allow to save asyncContext in map and then found it.

How can I acheve it in spring ?

I have found following article: Spring MVC 3.2 Preview: Introducing Servlet 3, Async Support

example:

@RequestMapping("/quotes")
@ResponseBody
public DeferredResult<String> quotes() {
  DeferredResult<String> deferredResult = new DeferredResult<String>();
  // Add deferredResult to a Queue or a Map...
  return deferredResult;
}


// In some other thread...    <-- important phrase
 deferredResult.setResult(data);
// Remove deferredResult from the Queue or Map

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM