@Override
  public Map<String, Object>[] listMessages(final String filterStr) throws Exception {
    checkStarted();

    clearIO();
    try {
      Filter filter = FilterImpl.createFilter(filterStr);
      List<Map<String, Object>> messages = new ArrayList<>();
      queue.flushExecutor();
      try (LinkedListIterator<MessageReference> iterator = queue.totalIterator()) {
        while (iterator.hasNext()) {
          MessageReference ref = iterator.next();
          if (filter == null || filter.match(ref.getMessage())) {
            Message message = ref.getMessage();
            messages.add(message.toMap());
          }
        }
        return messages.toArray(new Map[messages.size()]);
      }
    } catch (ActiveMQException e) {
      throw new IllegalStateException(e.getMessage());
    } finally {
      blockOnIO();
    }
  }
 /**
  * @param refs
  * @return
  */
 private Map<String, Object>[] convertMessagesToMaps(List<MessageReference> refs)
     throws ActiveMQException {
   Map<String, Object>[] messages = new Map[refs.size()];
   int i = 0;
   for (MessageReference ref : refs) {
     Message message = ref.getMessage();
     messages[i++] = message.toMap();
   }
   return messages;
 }
  protected Map<String, Object>[] getFirstMessage() throws Exception {
    checkStarted();

    clearIO();
    try {
      List<Map<String, Object>> messages = new ArrayList<>();
      queue.flushExecutor();
      try (LinkedListIterator<MessageReference> iterator = queue.totalIterator()) {
        // returns just the first, as it's the first only
        if (iterator.hasNext()) {
          MessageReference ref = iterator.next();
          Message message = ref.getMessage();
          messages.add(message.toMap());
        }
        return messages.toArray(new Map[1]);
      }
    } finally {
      blockOnIO();
    }
  }
 @Test
 public void testReceiveWithListeners() throws Exception {
   SimpleString dla = new SimpleString("DLA");
   SimpleString qName = new SimpleString("q1");
   AddressSettings addressSettings =
       new AddressSettings().setMaxDeliveryAttempts(2).setDeadLetterAddress(dla);
   server.getAddressSettingsRepository().addMatch(qName.toString(), addressSettings);
   SimpleString dlq = new SimpleString("DLQ1");
   clientSession.createQueue(dla, dlq, null, false);
   clientSession.createQueue(qName, qName, null, false);
   ClientProducer producer = clientSession.createProducer(qName);
   producer.send(createTextMessage(clientSession, "heyho!"));
   ClientConsumer clientConsumer = clientSession.createConsumer(qName);
   final CountDownLatch latch = new CountDownLatch(2);
   TestHandler handler = new TestHandler(latch, clientSession);
   clientConsumer.setMessageHandler(handler);
   clientSession.start();
   assertTrue(latch.await(5, TimeUnit.SECONDS));
   assertEquals(handler.count, 2);
   clientConsumer = clientSession.createConsumer(dlq);
   Message m = clientConsumer.receive(5000);
   Assert.assertNotNull(m);
   Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!");
 }
  @Override
  public void sendAcknowledged(final Message message) {
    if (logger.isTraceEnabled()) {
      logger.trace("BridgeImpl::sendAcknowledged received confirmation for message " + message);
    }
    if (active) {
      try {

        final MessageReference ref;

        synchronized (refs) {
          ref = refs.remove(message.getMessageID());
        }

        if (ref != null) {
          if (logger.isTraceEnabled()) {
            logger.trace(
                "BridgeImpl::sendAcknowledged bridge "
                    + this
                    + " Acking "
                    + ref
                    + " on queue "
                    + ref.getQueue());
          }
          ref.getQueue().acknowledge(ref);
          pendingAcks.countDown();
        } else {
          if (logger.isTraceEnabled()) {
            logger.trace(
                "BridgeImpl::sendAcknowledged bridge "
                    + this
                    + " could not find reference for message "
                    + message);
          }
        }
      } catch (Exception e) {
        ActiveMQServerLogger.LOGGER.bridgeFailedToAck(e);
      }
    }
  }