Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Long Polling with Spring's DeferredResult

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);
        }
    }
like image 448
Jamie White Avatar asked Jul 16 '15 15:07

Jamie White


People also ask

How is long polling implemented?

Long polling is a strategy in which the server chooses to keep a client's connection open for as long as feasible, only responding when data becomes available or a timeout threshold is reached, rather than having to repeat the procedure for each client until new data becomes available.

What is long polling in Java?

Long polling is a method that server applications use to hold a client connection until information becomes available. This is often used when a server must call a downstream service to get information and await a result.

What is long polling in Websocket?

Long polling takes HTTP request/response polling and makes it more efficient, since repeated requests to a server wastes resources. For example, establishing a new connection, parsing the HTTP headers, a query for new data, response generation and delivery, and finally connection closure and clean up.

What is long polling API?

Applications of Long Polling HTTP Long polling is a mechanism where the server can send data independently or push data to the client without the web client making a request. The information is then pushed as it becomes available, which makes it real-time.


1 Answers

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);
    }
}
like image 77
approxiblue Avatar answered Oct 05 '22 18:10

approxiblue