code: public <T extends Serializable> List<T> dequeueFromBuffer(String topic, Class<T> type, int timeout, int limit) {
          List<T> result = new ArrayList<T>();
          long cutoff = System.currentTimeMillis() + timeout;
          BlockingQueue<String> queue = _topics.get(topic).getMessages();
  
          while (System.currentTimeMillis() < cutoff && (limit < 0 || result.size() < limit)) {
              if (Thread.currentThread().isInterrupted()) {
                  break;
              }
              try {
                  String message = queue.poll(timeout, TimeUnit.MILLISECONDS);
  
                  if (message != null && !message.isEmpty()) {
                      if (String.class.isAssignableFrom(type)) {
                          result.add(type.cast(message));
                      } else {
                          result.add(_mapper.readValue(message, type));
                      }
                      if (result.size() % 1000 == 0) {
                          _logger.debug("Dequeued {} messages from local buffer.", result.size());
                      }
                  }
              } catch (InterruptedException e) {
                  _logger.warn("Interrupted while waiting for poll() to return a message.");
                  Thread.currentThread().interrupt();
              } catch (IOException e) {
                  _logger.warn("Exception while deserializing message to type: " + type + ". Skipping this message.", e);
              }
          }
          return result;
      }
docs: Dequeues messages from the local buffer as specified by the limit. If no messages are available to dequeue, then waits for at most timeout
  milliseconds before returning.
  
  @param   <T>      The result type.
  @param   topic    The topic to dequeue messages from.
  @param   type     The type that each message should be converted to.
  @param   timeout  The max amount of time in milliseconds that the function can take to dequeue limit number of messages. If number of dequeued
  messages is less than limit, then only those messages are returned.
  @param   limit    The max number of messages to dequeue.
  
  @return  Messages of the given type belonging to the given topic. Empty list if no such topic exists or the method times out.