我找到了以下文章:Spring MVC 3.2 预览版:介绍 Servlet 3,异步支持
例:
@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
但是我需要以防万一,如果结果未在 1 分钟内设置 - 应该返回错误 resul。
如何根据我的要求更改此示例?
DeferredResult 具有可用于您的要求的构造函数。
/**
* Create a DeferredResult with a timeout value and a default result to use
* in case of timeout.
* @param timeout timeout value in milliseconds (ignored if {@code null})
* @param timeoutResult the result to use
*/
public DeferredResult(Long timeout, Object timeoutResult) {
this.timeoutResult = timeoutResult;
this.timeout = timeout;
}