Long Polling with Spring's DeferredResult

非 Y 不嫁゛ 提交于 2019-11-27 16:17:22

问题


The client periodically calls an async method (long polling), passing it a value of a stock symbol, which the server uses to query the database and return the object back to the client.

I am using Spring's DeferredResult class, however I'm not familiar with how it works. Notice how I am using the symbol property (sent from client) to query the database for new data (see below).

Perhaps there is a better approach for long polling with Spring?

How do I pass the symbol property from the method deferredResult() to processQueues()?

    private final Queue<DeferredResult<String>> responseBodyQueue = new ConcurrentLinkedQueue<>();

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

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

回答1:


DeferredResult in Spring 4.1.7:

Subclasses can extend this class to easily associate additional data or behavior with the DeferredResult. For example, one might want to associate the user used to create the DeferredResult by extending the class and adding an additional property for the user. In this way, the user could easily be accessed later without the need to use a data structure to do the mapping.

You can extend DeferredResult and save the symbol parameter as a class field.

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


来源:https://stackoverflow.com/questions/31458910/long-polling-with-springs-deferredresult

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!