- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在实现 long polling as per the Spring blog from some time ago .
这里我转换的方法与以前具有相同的响应签名,但不是立即响应,它现在使用长轮询:
private Map<String, DeferredResult<ResponseEntity<?>>> requests = new ConcurrentHashMap<>();
@RequestMapping(value = "/{uuid}", method = RequestMethod.GET)
public DeferredResult<ResponseEntity<?>> poll(@PathVariable("uuid") final String uuid) {
// Create & store a new instance
ResponseEntity<?> pendingOnTimeout = ResponseEntity.accepted().build();
DeferredResult<ResponseEntity<?>> deferredResult = new DeferredResult<>(TWENTYFIVE_SECONDS, pendingOnTimeout);
requests.put(uuid, deferredResult);
// Clean up poll requests when done
deferredResult.onCompletion(() -> {
requests.remove(deferredResult);
});
// Set result if already available
Task task = taskHolder.retrieve(uuid);
if (task == null)
deferredResult.setResult(ResponseEntity.status(HttpStatus.GONE).build());
else
// Done (or canceled): Redirect to retrieve file contents
if (task.getFutureFile().isDone())
deferredResult.setResult(ResponseEntity.created(RetrieveController.uri(uuid)).build());
// Return result
return deferredResult;
}
pendingOnTimeout
当请求时间过长时响应(我之前立即返回),以防止代理切断请求。
accepted
的方法。标题。例如,当我尝试以下操作时:
@Test
public void pollPending() throws Exception {
MvcResult result = mockMvc.perform(get("/poll/{uuid}", uuidPending)).andReturn();
mockMvc.perform(asyncDispatch(result))
.andExpect(status().isAccepted());
}
java.lang.IllegalStateException: Async result for handler [public org.springframework.web.context.request.async.DeferredResult> nl.bioprodict.blast.api.PollController.poll(java.lang.String)] was not set during the specified timeToWait=25000 at org.springframework.util.Assert.state(Assert.java:392) at org.springframework.test.web.servlet.DefaultMvcResult.getAsyncResult(DefaultMvcResult.java:143) at org.springframework.test.web.servlet.DefaultMvcResult.getAsyncResult(DefaultMvcResult.java:120) at org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch(MockMvcRequestBuilders.java:235) at nl.bioprodict.blast.docs.PollControllerDocumentation.pollPending(PollControllerDocumentation.java:53) ...
最佳答案
在我的情况下,经过 spring 源代码并设置超时(10000 毫秒)并获得异步结果后,为我解决了这个问题,因为;
mvcResult.getRequest().getAsyncContext().setTimeout(10000);
mvcResult.getAsyncResult();
MvcResult mvcResult = this.mockMvc.perform(
post("<SOME_RELATIVE_URL>")
.contentType(MediaType.APPLICATION_JSON)
.content(<JSON_DATA>))
***.andExpect(request().asyncStarted())***
.andReturn();
***mvcResult.getRequest().getAsyncContext().setTimeout(10000);***
***mvcResult.getAsyncResult();***
this.mockMvc
.perform(asyncDispatch(mvcResult))
.andDo(print())
.andExpect(status().isOk());
关于spring-mvc - 如何测试 DeferredResult timeoutResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34343231/
我有这个 REST Controller @RequestMapping(path = "/hello", method = RequestMethod.POST) @ResponseStatus(H
我有一个 Spring Boot 应用程序和遗留代码, Controller 中有两个端点,这两个端点都返回不同对象的 DeferredResult<> ,现在我需要创建一个新端点,它基本上调用这两个
我正在使用DeferredResult在我的 Spring MVC 应用程序中处理一些可能长时间运行的操作的服务器端处理。它可能非常快,或者可能需要一两秒。 但无论哪种情况,传入的 HTTP 请求都会
我有一个旧的应用程序,客户端通过非常慢的连接(GPRS)上传大文件。目前我们使用Spring MVC和旧的servlet 2.0标准并直接获取请求inputStream,这显然会导致长时间运行的线程阻
我的应用程序应该有 2 个核心端点:push、pull 用于发送和获取数据。 拉取操作应该异步进行并产生 DeferredResult。当用户调用 pull service over rest 时,会
我正在使用 SpringBoot 1.4.4 创建异步 RESTful api。一切正常,除非请求被成功处理,DeferredResult> 总是有空主体和 200 OK 状态代码,即使我明确地将字符
我正在实现 long polling as per the Spring blog from some time ago . 这里我转换的方法与以前具有相同的响应签名,但不是立即响应,它现在使用长轮询
我正在尝试构建一个 REST 网络服务器,其中 GET 请求是非阻塞的,即使它需要进行稍微耗时的调用也是如此。 @RestController public class Endpoint {
为了实现长轮询,我尝试了不同的解决方案,但没有获得任何好的结果。 因此,我决定研究异步方法和 DeferredResult。这里我实现了 REST Controller 。 @Controller("
我有一个 comet(长轮询)Controller 调用,它接受一些 id 并 puts 然后进入阻塞队列,如果没有运行该 id 的计算,对于一个 Consumer 从 queue 中 take 并对
我在 Spring MVC 上使用 deferredResult,但是使用这段代码,超时仍然将 HTTP 代码 503 发送回客户端。 future.onCompletion(new Runnable
我有一个 Java 7 Spring Boot (1.5.4) RESTfull 服务,其端点返回 DeferredResult: @RequestMapping(value = "/download
我有一个 Guava ListenableFuture 实例和 Spring DeferredResult 的列表。我想为列表中第一个成功的 future 设置结果,或者如果超时尚未到期以从所有 fu
对于其余接口(interface),使用从 Controller 返回的 Spring MVC + RxJava + DeferredResult。 我正在考虑将 Hateoas 支持添加到端点。自然
以此为基础Jira 我有这个方法: val innerResult: DeferredResult[Object] = new DeferredResult[Object]() override de
我正在使用新的 spring 3.2 异步 servlet 请求。除了这一种情况之外,一切都运行良好... @RequestMapping("/test") @ResponseBody public
我在 Java 7 上使用 Spring 4.3.18 和 Spring Boot 1.5.14。 我正在实现一个 RestController 端点,它返回一个带有超时回调的 DeferredRes
Spring Web 3.2 带有一个用于异步请求处理的DeferredResult 类。它有一个 setErrorResult 用于在出现问题时提供替代响应,但没有提供 http 错误代码的选项。
Spring MVC 让 Controller 返回DeferredResult 和ListenableFuture(由ListenableFutureTask 实现)来做异步响应。有什么不同?我应该
客户端定期调用异步方法(长轮询),向其传递股票代码的值,服务器使用该值查询数据库并将对象返回给客户端。 我正在使用 Spring 的 DeferredResult类,但是我不熟悉它是如何工作的。请注意
我是一名优秀的程序员,十分优秀!