4

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 ?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

1 Answers1

3

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
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • I think you answered your own question. whenever you are getting the message from external system you just have to set it in the deferredResult and DeferredResult will remain shared between the threads. when result thread will right the result on deferred result it will be available as a response. – Vijendra Kumar Kulhade Apr 10 '17 at 21:37