private ProducerThreadTester createProducerTester(
      String brokerName, javax.jms.Destination destination) throws Exception {
    BrokerItem brokerItem = brokers.get(brokerName);

    Connection conn = brokerItem.createConnection();
    conn.start();
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ProducerThreadTester rc = new ProducerThreadTester(sess, destination);
    rc.setPersistent(persistentDelivery);
    return rc;
  }
  public void testMessageLeaks() throws Exception {
    clearSelectorCacheFiles();
    startAllBrokers();

    final BrokerService brokerA = brokers.get("BrokerA").broker;

    // Create the remote virtual topic consumer with selector
    ActiveMQDestination consumerQueue =
        createDestination("Consumer.B.VirtualTopic.tempTopic", false);
    // create it so that the queue is there and messages don't get lost
    MessageConsumer consumer1 = createConsumer("BrokerA", consumerQueue, "SYMBOL = 'AAPL'");
    MessageConsumer consumer2 = createConsumer("BrokerA", consumerQueue, "SYMBOL = 'AAPL'");

    ActiveMQTopic virtualTopic = new ActiveMQTopic("VirtualTopic.tempTopic");
    ProducerThreadTester producerTester = createProducerTester("BrokerA", virtualTopic);
    producerTester.setRunIndefinitely(true);
    producerTester.setSleep(5);
    producerTester.addMessageProperty("AAPL");
    producerTester.addMessageProperty("VIX");
    producerTester.start();

    int currentCount = producerTester.getSentCount();
    LOG.info(
        ">>>> currently sent: total="
            + currentCount
            + ", AAPL="
            + producerTester.getCountForProperty("AAPL")
            + ", VIX="
            + producerTester.getCountForProperty("VIX"));

    // let some messages get sent
    Thread.sleep(2000);

    MessageIdList consumer1Messages = getConsumerMessages("BrokerA", consumer1);
    consumer1Messages.waitForMessagesToArrive(50, 1000);

    // switch one of the consumers to SYMBOL = 'VIX'
    consumer1.close();
    consumer1 = createConsumer("BrokerA", consumerQueue, "SYMBOL = 'VIX'");

    // wait till new consumer is on board
    Wait.waitFor(
        new Wait.Condition() {
          @Override
          public boolean isSatisified() throws Exception {
            return brokerA
                    .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
                    .getConsumers()
                    .size()
                == 2;
          }
        });

    currentCount = producerTester.getSentCount();
    LOG.info(
        ">>>> currently sent: total="
            + currentCount
            + ", AAPL="
            + producerTester.getCountForProperty("AAPL")
            + ", VIX="
            + producerTester.getCountForProperty("VIX"));

    // let some messages get sent
    Thread.sleep(2000);

    // switch the other consumer to SYMBOL = 'VIX'
    consumer2.close();
    consumer2 = createConsumer("BrokerA", consumerQueue, "SYMBOL = 'VIX'");

    // wait till new consumer is on board
    Wait.waitFor(
        new Wait.Condition() {
          @Override
          public boolean isSatisified() throws Exception {
            return brokerA
                    .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
                    .getConsumers()
                    .size()
                == 2;
          }
        });

    currentCount = producerTester.getSentCount();
    LOG.info(
        ">>>> currently sent: total="
            + currentCount
            + ", AAPL="
            + producerTester.getCountForProperty("AAPL")
            + ", VIX="
            + producerTester.getCountForProperty("VIX"));

    // let some messages get sent
    Thread.sleep(2000);

    currentCount = producerTester.getSentCount();
    LOG.info(
        ">>>> currently sent: total="
            + currentCount
            + ", AAPL="
            + producerTester.getCountForProperty("AAPL")
            + ", VIX="
            + producerTester.getCountForProperty("VIX"));

    // make sure if there are messages that are orphaned in the queue that this number doesn't
    // grow...
    final long currentDepth =
        brokerA
            .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
            .getDestinationStatistics()
            .getMessages()
            .getCount();

    LOG.info(">>>>> Orphaned messages? " + currentDepth);

    // wait 5s to see if we can get a growth in the depth of the queue
    Wait.waitFor(
        new Wait.Condition() {
          @Override
          public boolean isSatisified() throws Exception {
            return brokerA
                    .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
                    .getDestinationStatistics()
                    .getMessages()
                    .getCount()
                > currentDepth;
          }
        },
        5000);

    // stop producers
    producerTester.setRunning(false);
    producerTester.join();

    // pause to let consumers catch up
    Thread.sleep(1000);

    assertTrue(
        brokerA
                .getDestination(new ActiveMQQueue("Consumer.B.VirtualTopic.tempTopic"))
                .getDestinationStatistics()
                .getMessages()
                .getCount()
            <= currentDepth);
  }