如何在 spring mvc 中使用具有延迟结果的超时?

How to use timeout with deffered result in spring mvc?

我找到了以下文章: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 分钟内设置 - 应返回错误结果。

如何根据我的要求更改此示例?

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;
        }