diff --git a/stocks/src/main/java/org/springframework/amqp/rabbit/stocks/web/QuoteController.java b/stocks/src/main/java/org/springframework/amqp/rabbit/stocks/web/QuoteController.java index 14d6d0c..33249ff 100644 --- a/stocks/src/main/java/org/springframework/amqp/rabbit/stocks/web/QuoteController.java +++ b/stocks/src/main/java/org/springframework/amqp/rabbit/stocks/web/QuoteController.java @@ -51,49 +51,48 @@ public class QuoteController { private ConcurrentMap responses = new ConcurrentHashMap(); private Queue quotes = new PriorityBlockingQueue(100, new QuoteComparator()); - + private long timeout = 30000; // 30 seconds of data public void setStockServiceGateway(StockServiceGateway stockServiceGateway) { this.stockServiceGateway = stockServiceGateway; } - public void handleMessage(Object message) { - logger.info("Client received: " + message); - if (message instanceof TradeResponse) { - TradeResponse response = (TradeResponse) message; - String key = response.getRequestId(); - responses.putIfAbsent(key, response); - Collection queue = new ArrayList(responses.values()); - long timestamp = System.currentTimeMillis() - timeout; - for (Iterator iterator = queue.iterator(); iterator.hasNext();) { - TradeResponse tradeResponse = iterator.next(); - if (tradeResponse.getTimestamp() queue = new ArrayList(responses.values()); + long timestamp = System.currentTimeMillis() - timeout; + for (Iterator iterator = queue.iterator(); iterator.hasNext();) { + TradeResponse tradeResponse = iterator.next(); + if (tradeResponse.getTimestamp() < timestamp) { + responses.remove(tradeResponse.getRequestId()); } - } else if (message instanceof Quote) { - long timestamp = System.currentTimeMillis() - timeout; - for (Iterator iterator = quotes.iterator(); iterator.hasNext();) { - Quote quote = iterator.next(); - if (quote.getTimestamp() iterator = quotes.iterator(); iterator.hasNext();) { + Quote quote = iterator.next(); + if (quote.getTimestamp() < timestamp) { + iterator.remove(); + } + } + quotes.add(message); + } + @RequestMapping("/quotes") @ResponseBody public List quotes(@RequestParam(required = false) Long timestamp) { if (timestamp == null) { timestamp = 0L; } - // TODO: remove older quotes ArrayList list = new ArrayList(); for (Quote quote : quotes) { - if (quote.getTimestamp()>timestamp) { + if (quote.getTimestamp() > timestamp) { list.add(quote); } } @@ -106,7 +105,7 @@ public class QuoteController { public TradeRequest trade(@ModelAttribute TradeRequest tradeRequest) { String ticker = tradeRequest.getTicker(); Long quantity = tradeRequest.getQuantity(); - if (quantity == null || quantity<=0 || !StringUtils.hasText(ticker)) { + if (quantity == null || quantity <= 0 || !StringUtils.hasText(ticker)) { // error return tradeRequest; } else { @@ -132,7 +131,7 @@ public class QuoteController { private static class QuoteComparator implements Comparator { public int compare(Quote o1, Quote o2) { - return new Long(o1.getTimestamp()-o2.getTimestamp()).intValue(); + return new Long(o1.getTimestamp() - o2.getTimestamp()).intValue(); } } diff --git a/stocks/src/main/resources/servlet-config.xml b/stocks/src/main/resources/servlet-config.xml index 741ba97..056804a 100644 --- a/stocks/src/main/resources/servlet-config.xml +++ b/stocks/src/main/resources/servlet-config.xml @@ -37,8 +37,8 @@ - + +