@Test
 public void testBasicSend() throws Exception {
   SimpleString dla = new SimpleString("DLA");
   SimpleString qName = new SimpleString("q1");
   SimpleString adName = new SimpleString("ad1");
   AddressSettings addressSettings =
       new AddressSettings().setMaxDeliveryAttempts(1).setDeadLetterAddress(dla);
   server.getAddressSettingsRepository().addMatch(adName.toString(), addressSettings);
   SimpleString dlq = new SimpleString("DLQ1");
   clientSession.createQueue(dla, dlq, null, false);
   clientSession.createQueue(adName, qName, null, false);
   ClientProducer producer = clientSession.createProducer(adName);
   producer.send(createTextMessage(clientSession, "heyho!"));
   clientSession.start();
   ClientConsumer clientConsumer = clientSession.createConsumer(qName);
   ClientMessage m = clientConsumer.receive(500);
   m.acknowledge();
   Assert.assertNotNull(m);
   Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!");
   // force a cancel
   clientSession.rollback();
   m = clientConsumer.receiveImmediate();
   Assert.assertNull(m);
   clientConsumer.close();
   clientConsumer = clientSession.createConsumer(dlq);
   m = clientConsumer.receive(500);
   Assert.assertNotNull(m);
   assertEquals("q1", m.getStringProperty(Message.HDR_ORIGINAL_QUEUE));
   assertEquals("ad1", m.getStringProperty(Message.HDR_ORIGINAL_ADDRESS));
   Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!");
 }
 /**
  * @param consumer
  * @throws Exception
  */
 private void readConsumer(String consumerName, ClientConsumer consumer) throws Exception {
   ClientMessage message = consumer.receive(5000);
   assertNotNull(message);
   System.out.println(
       "consumer = "
           + consumerName
           + " message, color="
           + message.getStringProperty("color")
           + ", msg = "
           + message.getStringProperty("value"));
   message.acknowledge();
 }
  @Test
  public void testNonMatchingMessagesFollowedByMatchingMessages() throws Exception {

    ClientMessage message = session.createMessage(false);

    message.putStringProperty("animal", "hippo");

    producer.send(message);

    assertNull(consumer.receiveImmediate());

    message = session.createMessage(false);

    message.putStringProperty("animal", "giraffe");

    log.info("sending second msg");

    producer.send(message);

    ClientMessage received = consumer.receiveImmediate();

    assertNotNull(received);

    assertEquals("giraffe", received.getStringProperty("animal"));

    assertNull(consumer.receiveImmediate());

    session.close();
  }
  @Test
  public void testNonMatchingMessagesFollowedByMatchingMessagesMany() throws Exception {

    for (int i = 0; i < QueueImpl.MAX_DELIVERIES_IN_LOOP * 2; i++) {
      ClientMessage message = session.createMessage(false);

      message.putStringProperty("animal", "hippo");

      producer.send(message);
    }

    assertNull(consumer.receiveImmediate());

    for (int i = 0; i < QueueImpl.MAX_DELIVERIES_IN_LOOP * 2; i++) {
      ClientMessage message = session.createMessage(false);

      message.putStringProperty("animal", "giraffe");

      producer.send(message);
    }

    for (int i = 0; i < QueueImpl.MAX_DELIVERIES_IN_LOOP * 2; i++) {
      ClientMessage received = consumer.receiveImmediate();

      assertNotNull(received);

      assertEquals("giraffe", received.getStringProperty("animal"));
    }

    assertNull(consumer.receiveImmediate());

    session.close();
  }
    public void receiveMessages(String queueName, int num, boolean checkDup)
        throws ActiveMQException {
      ClientSession session = sessionFactory.createSession();
      session.start();
      ClientConsumer consumer = session.createConsumer("jms.queue." + queueName);
      for (int i = 0; i < num; i++) {
        ClientMessage m = consumer.receive(30000);
        assertNotNull("i=" + i, m);
        assertNotNull(m.getStringProperty("bridge-message"));
        m.acknowledge();
      }

      ClientMessage m = consumer.receive(500);
      if (checkDup) {
        assertNull(m);
      } else {
        // drain messages
        while (m != null) {
          m = consumer.receive(200);
        }
      }

      session.close();
    }
  @Test
  public void testMessageProperties() throws Exception {
    ClientSession session = basicSetUp();

    session.createQueue(QUEUE_NAME, QUEUE_NAME, true);

    ClientProducer producer = session.createProducer(QUEUE_NAME);

    StringBuilder international = new StringBuilder();
    for (char x = 800; x < 1200; x++) {
      international.append(x);
    }

    String special = "\"<>'&";

    for (int i = 0; i < 5; i++) {
      ClientMessage msg = session.createMessage(true);
      msg.getBodyBuffer().writeString("Bob the giant pig " + i);
      msg.putBooleanProperty("myBooleanProperty", Boolean.TRUE);
      msg.putByteProperty("myByteProperty", new Byte("0"));
      msg.putBytesProperty("myBytesProperty", new byte[] {0, 1, 2, 3, 4});
      msg.putDoubleProperty("myDoubleProperty", i * 1.6);
      msg.putFloatProperty("myFloatProperty", i * 2.5F);
      msg.putIntProperty("myIntProperty", i);
      msg.putLongProperty("myLongProperty", Long.MAX_VALUE - i);
      msg.putObjectProperty("myObjectProperty", i);
      msg.putObjectProperty("myNullObjectProperty", null);
      msg.putShortProperty("myShortProperty", new Integer(i).shortValue());
      msg.putStringProperty("myStringProperty", "myStringPropertyValue_" + i);
      msg.putStringProperty("myNullStringProperty", null);
      msg.putStringProperty("myNonAsciiStringProperty", international.toString());
      msg.putStringProperty("mySpecialCharacters", special);
      msg.putStringProperty(
          new SimpleString("mySimpleStringProperty"),
          new SimpleString("mySimpleStringPropertyValue_" + i));
      msg.putStringProperty(new SimpleString("myNullSimpleStringProperty"), null);
      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 = createSessionFactory(locator);
    session = factory.createSession(false, true, true);

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

    for (int i = 0; i < 5; i++) {
      ClientMessage msg = consumer.receive(CONSUMER_TIMEOUT);
      byte[] body = new byte[msg.getBodySize()];
      msg.getBodyBuffer().readBytes(body);
      assertTrue(new String(body).contains("Bob the giant pig " + i));
      assertEquals(msg.getBooleanProperty("myBooleanProperty"), Boolean.TRUE);
      assertEquals(msg.getByteProperty("myByteProperty"), new Byte("0"));
      byte[] bytes = msg.getBytesProperty("myBytesProperty");
      for (int j = 0; j < 5; j++) {
        assertEquals(j, bytes[j]);
      }
      assertEquals(i * 1.6, msg.getDoubleProperty("myDoubleProperty"), 0.000001);
      assertEquals(i * 2.5F, msg.getFloatProperty("myFloatProperty"), 0.000001);
      assertEquals(i, msg.getIntProperty("myIntProperty").intValue());
      assertEquals(Long.MAX_VALUE - i, msg.getLongProperty("myLongProperty").longValue());
      assertEquals(i, msg.getObjectProperty("myObjectProperty"));
      assertEquals(null, msg.getObjectProperty("myNullObjectProperty"));
      assertEquals(
          new Integer(i).shortValue(), msg.getShortProperty("myShortProperty").shortValue());
      assertEquals("myStringPropertyValue_" + i, msg.getStringProperty("myStringProperty"));
      assertEquals(null, msg.getStringProperty("myNullStringProperty"));
      assertEquals(international.toString(), msg.getStringProperty("myNonAsciiStringProperty"));
      assertEquals(special, msg.getStringProperty("mySpecialCharacters"));
      assertEquals(
          new SimpleString("mySimpleStringPropertyValue_" + i),
          msg.getSimpleStringProperty(new SimpleString("mySimpleStringProperty")));
      assertEquals(
          null, msg.getSimpleStringProperty(new SimpleString("myNullSimpleStringProperty")));
    }
  }
  @Test
  public void testTwoConsumers() throws Exception {
    ClientConsumer consumer2 = session.createConsumer("foo", "animal='elephant'");

    // Create and consume message that matches the first consumer's filter

    ClientMessage message = session.createMessage(false);

    message.putStringProperty("animal", "giraffe");

    producer.send(message);

    ClientMessage received = consumer.receive(10000);

    assertNotNull(received);

    assertEquals("giraffe", received.getStringProperty("animal"));

    assertNull(consumer.receiveImmediate());
    assertNull(consumer2.receiveImmediate());

    // Create and consume another message that matches the first consumer's filter
    message = session.createMessage(false);

    message.putStringProperty("animal", "giraffe");

    producer.send(message);

    received = consumer.receive(10000);

    assertNotNull(received);

    assertEquals("giraffe", received.getStringProperty("animal"));

    assertNull(consumer.receiveImmediate());
    assertNull(consumer2.receiveImmediate());

    // Create and consume a message that matches the second consumer's filter

    message = session.createMessage(false);

    message.putStringProperty("animal", "elephant");

    producer.send(message);

    received = consumer2.receive(10000);

    assertNotNull(received);

    assertEquals("elephant", received.getStringProperty("animal"));

    assertNull(consumer.receiveImmediate());
    assertNull(consumer2.receiveImmediate());

    // Create and consume another message that matches the second consumer's filter

    message = session.createMessage(false);

    message.putStringProperty("animal", "elephant");

    producer.send(message);

    received = consumer2.receive(1000);

    assertNotNull(received);

    assertEquals("elephant", received.getStringProperty("animal"));

    assertNull(consumer.receiveImmediate());
    assertNull(consumer2.receiveImmediate());

    session.close();
  }