Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Spring'进行长轮询;推迟的结果_Spring_Spring Mvc_Long Polling_Server Sent Events_Http Streaming - Fatal编程技术网

使用Spring'进行长轮询;推迟的结果

使用Spring'进行长轮询;推迟的结果,spring,spring-mvc,long-polling,server-sent-events,http-streaming,Spring,Spring Mvc,Long Polling,Server Sent Events,Http Streaming,客户机定期调用异步方法(长轮询),向其传递股票符号的值,服务器使用该值查询数据库并将对象返回给客户机 我正在使用Spring的类,但是我不熟悉它的工作原理。请注意,我是如何使用symbol属性(从客户端发送)来查询数据库中的新数据的(请参见下文) 也许有更好的方法来使用Spring进行长时间轮询 如何将symbol属性从方法deferredResult()传递到processQueues() private final Queue responseByQueue=new ConcurrentLi

客户机定期调用异步方法(长轮询),向其传递股票符号的值,服务器使用该值查询数据库并将对象返回给客户机

我正在使用Spring的类,但是我不熟悉它的工作原理。请注意,我是如何使用symbol属性(从客户端发送)来查询数据库中的新数据的(请参见下文)

也许有更好的方法来使用Spring进行长时间轮询

如何将symbol属性从方法
deferredResult()
传递到
processQueues()

private final Queue responseByQueue=new ConcurrentLinkedQueue();
@请求映射(“/poll/{symbol}”)
public@ResponseBody DeferredResult DeferredResult(@PathVariable(“symbol”)字符串符号){
DeferredResult=新的DeferredResult();
this.responseBodyQueue.add(结果);
返回结果;
}
@计划(固定日期=2000)
public void processQueues(){
for(延迟结果:this.responseByQueue){
Quote Quote=jpaStockQuoteRepository.findStock(符号);
result.setResult(引号);
this.responseBodyQueue.remove(结果);
}
}
在Spring 4.1.7中:

子类可以扩展该类,以便将附加数据或行为与DeferredResult轻松关联。例如,您可能希望通过扩展类并为用户添加其他属性来关联用于创建DeferredResult的用户。通过这种方式,以后可以轻松访问用户,而无需使用数据结构进行映射

您可以扩展DeferredResult并将符号参数另存为类字段

static class DeferredQuote extends DeferredResult<Quote> {
    private final String symbol;
    public DeferredQuote(String symbol) {
        this.symbol = symbol;
    }
}

@RequestMapping("/poll/{symbol}")
public @ResponseBody DeferredQuote deferredResult(@PathVariable("symbol") String symbol) {
    DeferredQuote result = new DeferredQuote(symbol);
    responseBodyQueue.add(result);
    return result;
}

@Scheduled(fixedRate = 2000)
public void processQueues() {
    for (DeferredQuote result : responseBodyQueue) {
        Quote quote = jpaStockQuoteRepository.findStock(result.symbol);
        result.setResult(quote);
        responseBodyQueue.remove(result);
    }
}
静态类DeferredQuote扩展了DeferredResult{
私有最终字符串符号;
公共延迟引号(字符串符号){
这个符号=符号;
}
}
@请求映射(“/poll/{symbol}”)
public@responseBy DeferredQuote deferredResult(@PathVariable(“符号”)字符串符号){
递延报价结果=新递延报价(符号);
添加(结果);
返回结果;
}
@计划(固定日期=2000)
public void processQueues(){
for(延迟报价结果:ResponseByQueue){
Quote Quote=jpaStockQuoteRepository.findStock(result.symbol);
result.setResult(引号);
responseBodyQueue.remove(结果);
}
}

是否要等待结果并将其返回给客户端?如果不是,只返回null。如果是,您可能最好使用Future,因为您可以在以后从提交线程中提取结果。
static class DeferredQuote extends DeferredResult<Quote> {
    private final String symbol;
    public DeferredQuote(String symbol) {
        this.symbol = symbol;
    }
}

@RequestMapping("/poll/{symbol}")
public @ResponseBody DeferredQuote deferredResult(@PathVariable("symbol") String symbol) {
    DeferredQuote result = new DeferredQuote(symbol);
    responseBodyQueue.add(result);
    return result;
}

@Scheduled(fixedRate = 2000)
public void processQueues() {
    for (DeferredQuote result : responseBodyQueue) {
        Quote quote = jpaStockQuoteRepository.findStock(result.symbol);
        result.setResult(quote);
        responseBodyQueue.remove(result);
    }
}