您是否愿意分享任何最新的手册或在此解释如何使用最新的Spring(Spring Boot)实现REST Long Polling endpoints ?
我这次发现的所有东西都已经过时了,并且是在几年前发布的 .
那么,我提出一个问题是Long Polling仍然是一个好方法吗?我知道它在chess.com中使用过
对于长拉请求,您可以使用 DeferredResult . 当您返回DeferredResult响应时,请求线程将是空闲的,并且此请求由工作线程处理 . 这是一个例子:
DeferredResult
@GetMapping("/test") DeferredResult<String> test(){ Long timeOutInMilliSec = 100000L; String timeOutResp = "Time Out."; DeferredResult<String> deferredResult = new DeferredResult<>(timeOutInMilliSec,timeOutResp); CompletableFuture.runAsync(()->{ try { //Long pooling task;If task is not completed within 100 sec timeout response retrun for this request TimeUnit.SECONDS.sleep(10); //set result after completing task to return response to client deferredResult.setResult("Task Finished"); }catch (Exception ex){ } }); return deferredResult; }
在此请求中,等待10秒后给出响应 . 如果等待超过100秒,您将获得超时响应 .
看this .
1 回答
对于长拉请求,您可以使用
DeferredResult
. 当您返回DeferredResult响应时,请求线程将是空闲的,并且此请求由工作线程处理 . 这是一个例子:在此请求中,等待10秒后给出响应 . 如果等待超过100秒,您将获得超时响应 .
看this .