@Test
  public void testBody2() throws Exception {
    final String QUEUE_NAME = "A1";
    ActiveMQServer server = createServer(true);
    server.start();
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory factory = locator.createSessionFactory();
    ClientSession session = factory.createSession(false, true, true);

    session.createQueue(QUEUE_NAME, QUEUE_NAME, true);

    ClientProducer producer = session.createProducer(QUEUE_NAME);

    ClientMessage msg = session.createMessage(true);
    byte[] bodyTst = new byte[10];
    for (int i = 0; i < 10; i++) {
      bodyTst[i] = (byte) (i + 1);
    }
    msg.getBodyBuffer().writeBytes(bodyTst);
    assertEquals(bodyTst.length, msg.getBodySize());
    producer.send(msg);

    session.close();
    locator.close();
    server.stop();

    ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream();
    XmlDataExporter xmlDataExporter = new XmlDataExporter();
    xmlDataExporter.process(
        xmlOutputStream,
        server.getConfiguration().getBindingsDirectory(),
        server.getConfiguration().getJournalDirectory(),
        server.getConfiguration().getPagingDirectory(),
        server.getConfiguration().getLargeMessagesDirectory());
    System.out.print(new String(xmlOutputStream.toByteArray()));

    clearDataRecreateServerDirs();
    server.start();
    locator = createInVMNonHALocator();
    factory = locator.createSessionFactory();
    session = factory.createSession(false, false, true);
    ClientSession managementSession = factory.createSession(false, true, true);

    ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(xmlOutputStream.toByteArray());
    XmlDataImporter xmlDataImporter = new XmlDataImporter();
    xmlDataImporter.process(xmlInputStream, session, managementSession);
    ClientConsumer consumer = session.createConsumer(QUEUE_NAME);
    session.start();

    msg = consumer.receive(CONSUMER_TIMEOUT);
    assertNotNull(msg);
    assertEquals(msg.getBodySize(), bodyTst.length);
    byte[] bodyRead = new byte[bodyTst.length];
    msg.getBodyBuffer().readBytes(bodyRead);
    assertEqualsByteArrays(bodyTst, bodyRead);

    session.close();
    locator.close();
    server.stop();
  }
  @Test
  public void testBody() throws Exception {
    final String QUEUE_NAME = "A1";
    ActiveMQServer server = createServer(true);
    server.start();
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory factory = locator.createSessionFactory();
    ClientSession session = factory.createSession(false, true, true);

    session.createQueue(QUEUE_NAME, QUEUE_NAME, true);

    ClientProducer producer = session.createProducer(QUEUE_NAME);

    ClientMessage msg = session.createMessage(Message.TEXT_TYPE, true);
    msg.getBodyBuffer().writeString("bob123");
    producer.send(msg);

    session.close();
    locator.close();
    server.stop();

    ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream();
    XmlDataExporter xmlDataExporter = new XmlDataExporter();
    xmlDataExporter.process(
        xmlOutputStream,
        server.getConfiguration().getBindingsDirectory(),
        server.getConfiguration().getJournalDirectory(),
        server.getConfiguration().getPagingDirectory(),
        server.getConfiguration().getLargeMessagesDirectory());
    System.out.print(new String(xmlOutputStream.toByteArray()));

    clearDataRecreateServerDirs();
    server.start();
    locator = createInVMNonHALocator();
    factory = locator.createSessionFactory();
    session = factory.createSession(false, false, true);
    ClientSession managementSession = factory.createSession(false, true, true);

    ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(xmlOutputStream.toByteArray());
    XmlDataImporter xmlDataImporter = new XmlDataImporter();
    xmlDataImporter.process(xmlInputStream, session, managementSession);
    ClientConsumer consumer = session.createConsumer(QUEUE_NAME);
    session.start();

    msg = consumer.receive(CONSUMER_TIMEOUT);
    assertNotNull(msg);
    assertEquals("bob123", msg.getBodyBuffer().readString());

    session.close();
    locator.close();
    server.stop();
  }
    public void start() throws Exception {
      liveNode.start();
      waitForServerToStart(liveNode.getActiveMQServer());
      backupNode.start();
      waitForRemoteBackupSynchronization(backupNode.getActiveMQServer());

      locator = ActiveMQClient.createServerLocatorWithHA(liveConnector).setReconnectAttempts(-1);
      sessionFactory = locator.createSessionFactory();
    }
  protected ClientSessionFactoryInternal createSessionFactoryAndWaitForTopology(
      ServerLocator locator, int topologyMembers) throws Exception {
    CountDownLatch countDownLatch = new CountDownLatch(topologyMembers);

    locator.addClusterTopologyListener(new LatchClusterTopologyListener(countDownLatch));

    ClientSessionFactoryInternal sf = (ClientSessionFactoryInternal) locator.createSessionFactory();
    addSessionFactory(sf);

    Assert.assertTrue(
        "topology members expected " + topologyMembers, countDownLatch.await(5, TimeUnit.SECONDS));
    return sf;
  }
  public void testDuplicate() throws Exception {
    ActiveMQDestination queue = (ActiveMQDestination) session.createQueue("TEST,TEST");
    for (int i = 0; i < data.length; i++) {
      Message message = createMessage(i);
      configureMessage(message);
      if (verbose) {
        LOG.info("About to send a message: " + message + " with text: " + data[i]);
      }
      producer.send(queue, message);
    }

    Thread.sleep(200); // wait for messages to be queue;

    try (ServerLocator locator = ServerLocatorImpl.newLocator("tcp://localhost:61616");
        ClientSessionFactory factory = locator.createSessionFactory();
        ClientSession session = factory.createSession()) {
      ClientSession.QueueQuery query = session.queueQuery(new SimpleString("jms.queue.TEST"));
      assertNotNull(query);
      assertEquals(data.length, query.getMessageCount());
    }
  }
  @Test
  public void testPagedLargeMessage() throws Exception {
    final String MY_ADDRESS = "myAddress";
    final String MY_QUEUE = "myQueue";

    ActiveMQServer server = createServer(true);

    AddressSettings defaultSetting =
        new AddressSettings().setPageSizeBytes(10 * 1024).setMaxSizeBytes(20 * 1024);
    server.getAddressSettingsRepository().addMatch("#", defaultSetting);
    server.start();

    ServerLocator locator =
        createInVMNonHALocator()
            // Making it synchronous, just because we want to stop sending messages as soon as the
            // page-store becomes in
            // page mode and we could only guarantee that by setting it to synchronous
            .setBlockOnNonDurableSend(true)
            .setBlockOnDurableSend(true)
            .setBlockOnAcknowledge(true);

    ClientSessionFactory factory = locator.createSessionFactory();
    ClientSession session = factory.createSession(false, true, true);

    session.createQueue(MY_ADDRESS, MY_QUEUE, true);

    ClientProducer producer = session.createProducer(MY_ADDRESS);

    ClientMessage message = session.createMessage(true);
    message.getBodyBuffer().writeBytes(new byte[1024]);

    for (int i = 0; i < 200; i++) {
      producer.send(message);
    }

    LargeServerMessageImpl fileMessage =
        new LargeServerMessageImpl((JournalStorageManager) server.getStorageManager());

    fileMessage.setMessageID(1005);
    fileMessage.setDurable(true);

    for (int i = 0; i < 2 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE; i++) {
      fileMessage.addBytes(new byte[] {getSamplebyte(i)});
    }

    fileMessage.putLongProperty(
        Message.HDR_LARGE_BODY_SIZE, 2 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE);

    fileMessage.releaseResources();

    producer.send(fileMessage);

    fileMessage.deleteFile();

    session.close();
    locator.close();
    server.stop();

    ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream();
    XmlDataExporter xmlDataExporter = new XmlDataExporter();
    xmlDataExporter.process(
        xmlOutputStream,
        server.getConfiguration().getBindingsDirectory(),
        server.getConfiguration().getJournalDirectory(),
        server.getConfiguration().getPagingDirectory(),
        server.getConfiguration().getLargeMessagesDirectory());
    // System.out.print(new String(xmlOutputStream.toByteArray()));

    clearDataRecreateServerDirs();
    server.start();
    locator = createInVMNonHALocator();
    factory = locator.createSessionFactory();
    session = factory.createSession(false, true, true);

    ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(xmlOutputStream.toByteArray());
    XmlDataImporter xmlDataImporter = new XmlDataImporter();
    xmlDataImporter.process(xmlInputStream, session);

    ClientConsumer consumer = session.createConsumer(MY_QUEUE);

    session.start();

    for (int i = 0; i < 200; i++) {
      message = consumer.receive(CONSUMER_TIMEOUT);

      assertNotNull(message);
    }

    ClientMessage msg = consumer.receive(CONSUMER_TIMEOUT);

    assertNotNull(msg);

    assertEquals(2 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE, msg.getBodySize());

    for (int i = 0; i < 2 * ActiveMQClient.DEFAULT_MIN_LARGE_MESSAGE_SIZE; i++) {
      assertEquals(getSamplebyte(i), msg.getBodyBuffer().readByte());
    }

    session.close();
    locator.close();
    server.stop();
  }
  @Test
  public void testPagedMessageWithMissingBinding() throws Exception {
    final String MY_ADDRESS = "myAddress";
    final String MY_QUEUE = "myQueue";
    final String MY_QUEUE2 = "myQueue2";

    ActiveMQServer server = createServer(true);

    AddressSettings defaultSetting =
        new AddressSettings().setPageSizeBytes(10 * 1024).setMaxSizeBytes(20 * 1024);
    server.getAddressSettingsRepository().addMatch("#", defaultSetting);
    server.start();

    ServerLocator locator =
        createInVMNonHALocator()
            // Making it synchronous, just because we want to stop sending messages as soon as the
            // page-store becomes in
            // page mode and we could only guarantee that by setting it to synchronous
            .setBlockOnNonDurableSend(true)
            .setBlockOnDurableSend(true)
            .setBlockOnAcknowledge(true);

    ClientSessionFactory factory = locator.createSessionFactory();
    ClientSession session = factory.createSession(false, true, true);

    session.createQueue(MY_ADDRESS, MY_QUEUE, true);
    session.createQueue(MY_ADDRESS, MY_QUEUE2, true);

    ClientProducer producer = session.createProducer(MY_ADDRESS);

    ClientMessage message = session.createMessage(true);
    message.getBodyBuffer().writeBytes(new byte[1024]);

    for (int i = 0; i < 200; i++) {
      producer.send(message);
    }

    session.deleteQueue(MY_QUEUE2);

    session.close();
    locator.close();
    server.stop();

    ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream();
    XmlDataExporter xmlDataExporter = new XmlDataExporter();
    xmlDataExporter.process(
        xmlOutputStream,
        server.getConfiguration().getBindingsDirectory(),
        server.getConfiguration().getJournalDirectory(),
        server.getConfiguration().getPagingDirectory(),
        server.getConfiguration().getLargeMessagesDirectory());
    System.out.print(new String(xmlOutputStream.toByteArray()));

    clearDataRecreateServerDirs();
    server.start();
    locator = createInVMNonHALocator();
    factory = locator.createSessionFactory();
    session = factory.createSession(false, true, true);

    ByteArrayInputStream xmlInputStream = new ByteArrayInputStream(xmlOutputStream.toByteArray());
    XmlDataImporter xmlDataImporter = new XmlDataImporter();
    xmlDataImporter.process(xmlInputStream, session);

    ClientConsumer consumer = session.createConsumer(MY_QUEUE);

    session.start();

    for (int i = 0; i < 200; i++) {
      message = consumer.receive(CONSUMER_TIMEOUT);

      assertNotNull(message);
    }

    session.close();
    locator.close();
    server.stop();
  }