Spring DeferredResult超时

时间:2016-10-25 06:57:53

标签: java spring spring-mvc rx-java deferred-result

我有一个可观察的,因为我可能需要很长时间才能返回春天的DeferredResult

这是我正在使用的控制器逻辑:

public DeferredResult<ResponseEntity<InputStreamResource>> getSomeFile() {
    DeferredResult<ResponseEntity<InputStreamResource>> deferredResult = new DeferredResult<>(TIMEOUT);
    Observable<File> observableFile = fileService.getSomeFile();
    observableFile
            .map(this::fileToInputStreamResource)
            .map(resource -> ResponseEntity.ok().cacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic()).body(resource))
            .subscribe(deferredResult::setResult, ex -> {
                deferredResult.setErrorResult(ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null));
            });
    return deferredResult;
}

这是我的测试用例:

@Test
public void getSomeFile() throws Exception {
    this.mockMvc.perform(get("/somefile").accept(MediaType.parseMediaType("application/xml;charset=UTF-8")))
            .andExpect(request().asyncStarted())
            .andExpect(status().isOk())
            .andDo(result -> {
                ResponseEntity<InputStreamResource> fileResource = (ResponseEntity<InputStreamResource>) result.getAsyncResult();
                InputStream fileResourceInputStream = fileResource.getBody().getInputStream();
                FooBar foobar = (FooBar) jaxb2Marshaller.unmarshal(new StreamSource(fileResourceInputStream));
                assertThat(foobar.getFoos(), is(not(empty())));
            });
}

当我在没有设置超时的情况下实例化DeferredResult时,此测试失败,因为它使用为休息模板设置的通用超时(10秒)。如果我显式设置超时new DefferredResult(Long.MAX_VALUE),它将失败并出现相同的异常:

java.lang.IllegalStateException: Async result for handler [public org.springframework.web.context.request.async.DeferredResult<org.springframework.http.ResponseEntity<org.springframework.core.io.InputStreamResource>> com.example.controller.FileController.getSomeFile()] was not set during the specified timeToWait=9223372036854775807
    at org.springframework.test.web.servlet.DefaultMvcResult.getAsyncResult(DefaultMvcResult.java:145)
    at org.springframework.test.web.servlet.DefaultMvcResult.getAsyncResult(DefaultMvcResult.java:121)
    at org.springframework.test.web.servlet.result.RequestResultMatchers$4.match(RequestResultMatchers.java:114)
    at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:171)

那么应该如何配置超时或哪里出现问题?

0 个答案:

没有答案