private void doTestBasicGrouping() throws Exception {
    ClientProducer clientProducer = clientSession.createProducer(qName);
    ClientConsumer consumer = clientSession.createConsumer(qName);
    ClientConsumer consumer2 = clientSession.createConsumer(qName);
    clientSession.start();

    SimpleString groupId = new SimpleString("grp1");
    int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
      ClientMessage message = createTextMessage(clientSession, "m" + i);
      message.putStringProperty(Message.HDR_GROUP_ID, groupId);
      clientProducer.send(message);
    }

    CountDownLatch latch = new CountDownLatch(numMessages);
    DummyMessageHandler dummyMessageHandler = new DummyMessageHandler(latch, true);
    consumer.setMessageHandler(dummyMessageHandler);
    DummyMessageHandler dummyMessageHandler2 = new DummyMessageHandler(latch, true);
    consumer2.setMessageHandler(dummyMessageHandler2);
    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    Assert.assertEquals(100, dummyMessageHandler.list.size());
    Assert.assertEquals(0, dummyMessageHandler2.list.size());
    consumer.close();
    consumer2.close();
  }
  @Test
  public void testConsumerMultipleBrowser() throws Exception {

    ClientSessionFactory sf = createSessionFactory(locator);

    ClientSession session = sf.createSession(false, true, true);

    session.createQueue(QUEUE, QUEUE, null, false);

    ClientProducer producer = session.createProducer(QUEUE);

    final int numMessages = 100;

    for (int i = 0; i < numMessages; i++) {
      ClientMessage message = createTextMessage(session, "m" + i);
      producer.send(message);
    }

    ClientConsumer consumer = session.createConsumer(QUEUE, null, true);
    ClientConsumer consumer2 = session.createConsumer(QUEUE, null, true);
    ClientConsumer consumer3 = session.createConsumer(QUEUE, null, true);

    for (int i = 0; i < numMessages; i++) {
      ClientMessage message2 = consumer.receive(1000);
      Assert.assertEquals("m" + i, message2.getBodyBuffer().readString());
      message2 = consumer2.receive(1000);
      Assert.assertEquals("m" + i, message2.getBodyBuffer().readString());
      message2 = consumer3.receive(1000);
      Assert.assertEquals("m" + i, message2.getBodyBuffer().readString());
    }

    session.close();
  }
 @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!");
 }
Esempio n. 4
0
  @Test
  public void testReceiveImmediate() throws Exception {

    // forces perfect round robin
    locator.setConsumerWindowSize(1);
    ClientSessionFactory cf = createSessionFactory(locator);
    ClientSession sendSession = cf.createSession(false, true, true);
    ClientProducer cp = sendSession.createProducer(addressA);
    ClientSession session = cf.createSession(false, true, true);
    session.createQueue(addressA, queueA, false);
    ClientConsumer cc = session.createConsumer(queueA);
    ClientConsumer cc2 = session.createConsumer(queueA);
    session.start();
    cp.send(sendSession.createMessage(false));
    cp.send(sendSession.createMessage(false));
    cp.send(sendSession.createMessage(false));
    sendSession.commit();

    Assert.assertNotNull(cc2.receive(5000));
    Assert.assertNotNull(cc.receive(5000));
    if (cc.receiveImmediate() == null) {
      Assert.assertNotNull(cc2.receiveImmediate());
    }
    session.close();
    sendSession.close();
  }
  @Test
  public void testDeadLetterAddressWithOverridenSublevelAddressSettings() throws Exception {
    int defaultDeliveryAttempt = 3;
    int specificeDeliveryAttempt = defaultDeliveryAttempt + 1;

    SimpleString address = new SimpleString("prefix.address");
    SimpleString queue = RandomUtil.randomSimpleString();
    SimpleString defaultDeadLetterAddress = RandomUtil.randomSimpleString();
    SimpleString defaultDeadLetterQueue = RandomUtil.randomSimpleString();
    SimpleString specificDeadLetterAddress = RandomUtil.randomSimpleString();
    SimpleString specificDeadLetterQueue = RandomUtil.randomSimpleString();

    AddressSettings defaultAddressSettings =
        new AddressSettings()
            .setMaxDeliveryAttempts(defaultDeliveryAttempt)
            .setDeadLetterAddress(defaultDeadLetterAddress);
    server.getAddressSettingsRepository().addMatch("*", defaultAddressSettings);
    AddressSettings specificAddressSettings =
        new AddressSettings()
            .setMaxDeliveryAttempts(specificeDeliveryAttempt)
            .setDeadLetterAddress(specificDeadLetterAddress);
    server.getAddressSettingsRepository().addMatch(address.toString(), specificAddressSettings);

    clientSession.createQueue(address, queue, false);
    clientSession.createQueue(defaultDeadLetterAddress, defaultDeadLetterQueue, false);
    clientSession.createQueue(specificDeadLetterAddress, specificDeadLetterQueue, false);

    ClientProducer producer = clientSession.createProducer(address);
    ClientMessage clientMessage = createTextMessage(clientSession, "heyho!");
    producer.send(clientMessage);

    clientSession.start();
    ClientConsumer clientConsumer = clientSession.createConsumer(queue);
    ClientConsumer defaultDeadLetterConsumer = clientSession.createConsumer(defaultDeadLetterQueue);
    ClientConsumer specificDeadLetterConsumer =
        clientSession.createConsumer(specificDeadLetterQueue);

    for (int i = 0; i < defaultDeliveryAttempt; i++) {
      ClientMessage m = clientConsumer.receive(500);
      Assert.assertNotNull(m);
      Assert.assertEquals(i + 1, m.getDeliveryCount());
      m.acknowledge();
      clientSession.rollback();
    }

    Assert.assertNull(defaultDeadLetterConsumer.receiveImmediate());
    Assert.assertNull(specificDeadLetterConsumer.receiveImmediate());

    // one more redelivery attempt:
    ClientMessage m = clientConsumer.receive(500);
    Assert.assertNotNull(m);
    Assert.assertEquals(specificeDeliveryAttempt, m.getDeliveryCount());
    m.acknowledge();
    clientSession.rollback();

    Assert.assertNull(defaultDeadLetterConsumer.receiveImmediate());
    Assert.assertNotNull(specificDeadLetterConsumer.receive(500));
  }
  private void doTestMultipleGroupingTXCommit() throws Exception {
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory sessionFactory = createSessionFactory(locator);
    ClientSession clientSession = sessionFactory.createSession(false, false, false);
    ClientProducer clientProducer = this.clientSession.createProducer(qName);
    clientSession.start();

    ClientConsumer consumer = clientSession.createConsumer(qName);
    ClientConsumer consumer2 = clientSession.createConsumer(qName);

    // Wait a bit otherwise consumers might be busy
    Thread.sleep(200);

    SimpleString groupId = new SimpleString("grp1");
    SimpleString groupId2 = new SimpleString("grp2");
    int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
      ClientMessage message = createTextMessage(clientSession, "m" + i);
      if (i % 2 == 0 || i == 0) {
        message.putStringProperty(Message.HDR_GROUP_ID, groupId);
      } else {
        message.putStringProperty(Message.HDR_GROUP_ID, groupId2);
      }
      clientProducer.send(message);
    }

    CountDownLatch latch = new CountDownLatch(numMessages);
    DummyMessageHandler dummyMessageHandler = new DummyMessageHandler(latch, true);
    consumer.setMessageHandler(dummyMessageHandler);
    DummyMessageHandler dummyMessageHandler2 = new DummyMessageHandler(latch, true);
    consumer2.setMessageHandler(dummyMessageHandler2);
    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    clientSession.commit();
    Assert.assertEquals(dummyMessageHandler.list.size(), 50);
    int i = 0;
    for (ClientMessage message : dummyMessageHandler.list) {
      Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i);
      i += 2;
    }
    Assert.assertEquals(dummyMessageHandler2.list.size(), 50);
    i = 1;
    for (ClientMessage message : dummyMessageHandler2.list) {
      Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i);
      i += 2;
    }
    consumer.close();
    consumer2.close();
    consumer = this.clientSession.createConsumer(qName);
    Assert.assertNull(consumer.receiveImmediate());
    clientSession.close();
    locator.close();
  }
  @Test
  public void testStopStartMultipleConsumers() throws Exception {
    locator.setConsumerWindowSize(getMessageEncodeSize(QUEUE) * 33);
    ClientSessionFactory sf = createSessionFactory(locator);

    final ClientSession session = sf.createSession(false, true, true);

    session.createQueue(QUEUE, QUEUE, null, false);

    ClientProducer producer = session.createProducer(QUEUE);

    final int numMessages = 100;

    for (int i = 0; i < numMessages; i++) {
      ClientMessage message = createTextMessage(session, "m" + i);
      message.putIntProperty(new SimpleString("i"), i);
      producer.send(message);
    }

    ClientConsumer consumer = session.createConsumer(QUEUE);
    ClientConsumer consumer2 = session.createConsumer(QUEUE);
    ClientConsumer consumer3 = session.createConsumer(QUEUE);

    session.start();

    ClientMessage cm = consumer.receive(5000);
    Assert.assertNotNull(cm);
    cm.acknowledge();
    cm = consumer2.receive(5000);
    Assert.assertNotNull(cm);
    cm.acknowledge();
    cm = consumer3.receive(5000);
    Assert.assertNotNull(cm);
    cm.acknowledge();

    session.stop();
    cm = consumer.receiveImmediate();
    Assert.assertNull(cm);
    cm = consumer2.receiveImmediate();
    Assert.assertNull(cm);
    cm = consumer3.receiveImmediate();
    Assert.assertNull(cm);

    session.start();
    cm = consumer.receive(5000);
    Assert.assertNotNull(cm);
    cm = consumer2.receive(5000);
    Assert.assertNotNull(cm);
    cm = consumer3.receive(5000);
    Assert.assertNotNull(cm);
    session.close();
  }
  private void dotestMultipleGroupingXACommit() throws Exception {
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory sessionFactory = createSessionFactory(locator);
    ClientSession clientSession = sessionFactory.createSession(true, false, false);
    ClientProducer clientProducer = this.clientSession.createProducer(qName);
    ClientConsumer consumer = clientSession.createConsumer(qName);
    ClientConsumer consumer2 = clientSession.createConsumer(qName);
    clientSession.start();

    Xid xid = new XidImpl("bq".getBytes(), 4, "gtid".getBytes());
    clientSession.start(xid, XAResource.TMNOFLAGS);

    SimpleString groupId = new SimpleString("grp1");
    SimpleString groupId2 = new SimpleString("grp2");
    int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
      ClientMessage message = createTextMessage(clientSession, "m" + i);
      if (i % 2 == 0 || i == 0) {
        message.putStringProperty(Message.HDR_GROUP_ID, groupId);
      } else {
        message.putStringProperty(Message.HDR_GROUP_ID, groupId2);
      }
      clientProducer.send(message);
    }
    CountDownLatch latch = new CountDownLatch(numMessages);
    DummyMessageHandler dummyMessageHandler = new DummyMessageHandler(latch, true);
    consumer.setMessageHandler(dummyMessageHandler);
    DummyMessageHandler dummyMessageHandler2 = new DummyMessageHandler(latch, true);
    consumer2.setMessageHandler(dummyMessageHandler2);
    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    clientSession.end(xid, XAResource.TMSUCCESS);
    clientSession.prepare(xid);
    clientSession.commit(xid, false);
    Assert.assertEquals(dummyMessageHandler.list.size(), 50);
    int i = 0;
    for (ClientMessage message : dummyMessageHandler.list) {
      Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i);
      i += 2;
    }
    Assert.assertEquals(dummyMessageHandler2.list.size(), 50);
    i = 1;
    for (ClientMessage message : dummyMessageHandler2.list) {
      Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i);
      i += 2;
    }
    consumer.close();
    consumer2.close();
    consumer = this.clientSession.createConsumer(qName);
    Assert.assertNull(consumer.receiveImmediate());
    clientSession.close();
    locator.close();
  }
  @Test
  public void testPartialQueue() throws Exception {
    ClientSession session = basicSetUp();

    session.createQueue("myAddress", "myQueue1", true);
    session.createQueue("myAddress", "myQueue2", true);

    ClientProducer producer = session.createProducer("myAddress");

    ClientMessage msg = session.createMessage(true);
    producer.send(msg);

    ClientConsumer consumer = session.createConsumer("myQueue1");
    session.start();
    msg = consumer.receive(CONSUMER_TIMEOUT);
    assertNotNull(msg);
    msg.acknowledge();
    consumer.close();

    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);
    consumer = session.createConsumer("myQueue1");
    session.start();
    msg = consumer.receive(CONSUMER_TIMEOUT);
    assertNull(msg);
    consumer.close();

    consumer = session.createConsumer("myQueue2");
    msg = consumer.receive(CONSUMER_TIMEOUT);
    assertNotNull(msg);
  }
  @Test
  public void testLinkedListOrder() throws Exception {
    ServerLocator locator = createInVMNonHALocator();

    ClientSessionFactory sf = createSessionFactory(locator);

    ClientSession session = sf.createSession();

    session.start();

    ClientProducer producer = session.createProducer("foo");

    ClientConsumer redConsumer = session.createConsumer("foo", "color='red'");

    ClientConsumer anyConsumer = session.createConsumer("foo");

    sendMessage(session, producer, "any", "msg1");

    sendMessage(session, producer, "any", "msg2");

    sendMessage(session, producer, "any", "msg3");

    sendMessage(session, producer, "red", "msgRed4");

    sendMessage(session, producer, "red", "msgRed5");

    readConsumer("anyConsumer", anyConsumer);

    readConsumer("anyConsumer", anyConsumer);

    log.info("### closing consumer ###");

    anyConsumer.close();

    readConsumer("redConsumer", redConsumer);

    readConsumer("redConsumer", redConsumer);

    log.info("### recreating consumer ###");

    anyConsumer = session.createConsumer("foo");

    session.start();

    readConsumer("anyConsumer", anyConsumer);

    session.close();

    sf.close();

    locator.close();
  }
  @Test
  public void testRestartJournal() throws Throwable {
    runExternalProcess(getTestDir(), 0, JournalCrashTest.FIRST_RUN);
    runExternalProcess(getTestDir(), JournalCrashTest.FIRST_RUN, JournalCrashTest.SECOND_RUN);
    runExternalProcess(getTestDir(), JournalCrashTest.SECOND_RUN, JournalCrashTest.THIRD_RUN);
    runExternalProcess(getTestDir(), JournalCrashTest.THIRD_RUN, JournalCrashTest.FOURTH_RUN);

    printJournal();

    startServer();

    try (ClientSession session = factory.createSession(true, true)) {

      ClientConsumer consumer = session.createConsumer(QUEUE);
      session.start();

      for (int i = 0; i < JournalCrashTest.FOURTH_RUN; i++) {
        ClientMessage msg = consumer.receive(5000);

        Assert.assertNotNull("Msg at " + i, msg);

        msg.acknowledge();

        Assert.assertEquals(i, msg.getObjectProperty(new SimpleString("key")));
      }
      session.close();
    }
  }
  @Test
  public void testConsumerBrowserMessageAckDoesNothing() throws Exception {
    ClientSessionFactory sf = createSessionFactory(locator);

    ClientSession session = sf.createSession(false, true, true);

    session.createQueue(QUEUE, QUEUE, null, false);

    ClientProducer producer = session.createProducer(QUEUE);

    final int numMessages = 100;

    for (int i = 0; i < numMessages; i++) {
      ClientMessage message = createTextMessage(session, "m" + i);
      producer.send(message);
    }

    ClientConsumer consumer = session.createConsumer(QUEUE, null, true);

    for (int i = 0; i < numMessages; i++) {
      ClientMessage message2 = consumer.receive(1000);

      message2.acknowledge();

      Assert.assertEquals("m" + i, message2.getBodyBuffer().readString());
    }
    // assert that all the messages are there and none have been acked
    Assert.assertEquals(
        0, ((Queue) server.getPostOffice().getBinding(QUEUE).getBindable()).getDeliveringCount());
    Assert.assertEquals(
        100, getMessageCount(((Queue) server.getPostOffice().getBinding(QUEUE).getBindable())));

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

    ClientSessionFactory sf = createSessionFactory(locator);

    ClientSession session = sf.createSession(false, true, true);

    session.createQueue(QUEUE, QUEUE, null, false);

    ClientProducer producer = session.createProducer(QUEUE);

    final int numMessages = 100;

    for (int i = 0; i < numMessages; i++) {
      ClientMessage message = createTextMessage(session, "m" + i);
      if (i % 2 == 0) {
        message.putStringProperty(new SimpleString("color"), new SimpleString("RED"));
      }
      producer.send(message);
    }

    ClientConsumer consumer =
        session.createConsumer(QUEUE, new SimpleString("color = 'RED'"), true);

    for (int i = 0; i < numMessages; i += 2) {
      ClientMessage message2 = consumer.receive(1000);

      Assert.assertEquals("m" + i, message2.getBodyBuffer().readString());
    }

    session.close();
  }
 // HORNETQ- 1084
 @Test
 public void testBasicSendWithDLAButNoBinding() throws Exception {
   SimpleString dla = new SimpleString("DLA");
   SimpleString qName = new SimpleString("q1");
   AddressSettings addressSettings =
       new AddressSettings().setMaxDeliveryAttempts(1).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!"));
   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();
   Queue q = (Queue) server.getPostOffice().getBinding(qName).getBindable();
   Assert.assertEquals(0, q.getDeliveringCount());
 }
 private void doTestMultipleGroupingSingleConsumer(final boolean directDelivery) throws Exception {
   ClientProducer clientProducer = clientSession.createProducer(qName);
   ClientConsumer consumer = clientSession.createConsumer(qName);
   if (directDelivery) {
     clientSession.start();
   }
   SimpleString groupId = new SimpleString("grp1");
   SimpleString groupId2 = new SimpleString("grp2");
   int numMessages = 100;
   for (int i = 0; i < numMessages; i++) {
     ClientMessage message = createTextMessage(clientSession, "m" + i);
     if (i % 2 == 0 || i == 0) {
       message.putStringProperty(Message.HDR_GROUP_ID, groupId);
     } else {
       message.putStringProperty(Message.HDR_GROUP_ID, groupId2);
     }
     clientProducer.send(message);
   }
   if (!directDelivery) {
     clientSession.start();
   }
   CountDownLatch latch = new CountDownLatch(numMessages);
   DummyMessageHandler dummyMessageHandler = new DummyMessageHandler(latch, true);
   consumer.setMessageHandler(dummyMessageHandler);
   Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
   Assert.assertEquals(dummyMessageHandler.list.size(), 100);
   int i = 0;
   for (ClientMessage message : dummyMessageHandler.list) {
     Assert.assertEquals(message.getBodyBuffer().readString(), "m" + i);
     i += 1;
   }
   consumer.close();
 }
  @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 testLoadBalanceGroups() throws Exception {
    Assume.assumeFalse(
        "only makes sense withOUT auto-group",
        clientSessionFactory.getServerLocator().isAutoGroup());

    ClientProducer clientProducer = clientSession.createProducer(qName);
    ClientConsumer consumer1 = clientSession.createConsumer(qName);
    ClientConsumer consumer2 = clientSession.createConsumer(qName);
    ClientConsumer consumer3 = clientSession.createConsumer(qName);
    ClientConsumer[] consumers = new ClientConsumer[] {consumer1, consumer2, consumer3};
    int[] counts = new int[consumers.length];

    clientSession.start();
    try {
      // Add all messages for a particular group before moving onto the next
      for (int group = 0; group < 10; group++) {
        for (int messageId = 0; messageId < 3; messageId++) {
          ClientMessage message = clientSession.createMessage(false);
          message.putStringProperty("_AMQ_GROUP_ID", "" + group);
          clientProducer.send(message);
        }
      }

      for (int c = 0; c < consumers.length; c++) {
        while (true) {
          ClientMessage msg = consumers[c].receiveImmediate();
          if (msg == null) {
            break;
          }
          counts[c]++;
        }
      }

      for (int count : counts) {
        Assert.assertNotEquals(
            "You shouldn't have all messages bound to a single consumer", 30, count);
        Assert.assertNotEquals(
            "But you shouldn't have also a single consumer bound to none", 0, count);
      }
    } finally {
      consumer1.close();
      consumer2.close();
      consumer3.close();
    }
  }
  @Test
  public void testSetMessageHandlerWithMessagesPending() throws Exception {
    ClientSession session = sf.createSession(false, true, true);

    session.createQueue(QUEUE, QUEUE, null, false);

    ClientProducer producer = session.createProducer(QUEUE);

    final int numMessages = 100;

    for (int i = 0; i < numMessages; i++) {
      ClientMessage message = createTextMessage(session, "m" + i);
      producer.send(message);
    }

    ClientConsumer consumer = session.createConsumer(QUEUE, null, true);

    session.start();

    Thread.sleep(100);

    // Message should be in consumer

    class MyHandler implements MessageHandler {

      @Override
      public void onMessage(final ClientMessage message) {
        try {
          Thread.sleep(10);

          message.acknowledge();
        } catch (Exception e) {
        }
      }
    }

    consumer.setMessageHandler(new MyHandler());

    // Let a few messages get processed
    Thread.sleep(100);

    // Now set null

    consumer.setMessageHandler(null);

    // Give a bit of time for some queued executors to run

    Thread.sleep(500);

    // Make sure no exceptions were thrown from onMessage
    Assert.assertNull(consumer.getLastException());

    session.close();
  }
  @Test
  public void testDeadlLetterAddressWithDefaultAddressSettings() throws Exception {
    int deliveryAttempt = 3;

    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    SimpleString deadLetterAddress = RandomUtil.randomSimpleString();
    SimpleString deadLetterQueue = RandomUtil.randomSimpleString();
    AddressSettings addressSettings =
        new AddressSettings()
            .setMaxDeliveryAttempts(deliveryAttempt)
            .setDeadLetterAddress(deadLetterAddress);
    server.getAddressSettingsRepository().setDefault(addressSettings);

    clientSession.createQueue(address, queue, false);
    clientSession.createQueue(deadLetterAddress, deadLetterQueue, false);

    ClientProducer producer = clientSession.createProducer(address);
    ClientMessage clientMessage = createTextMessage(clientSession, "heyho!");
    producer.send(clientMessage);

    clientSession.start();
    ClientConsumer clientConsumer = clientSession.createConsumer(queue);
    for (int i = 0; i < deliveryAttempt; i++) {
      ClientMessage m = clientConsumer.receive(500);
      Assert.assertNotNull(m);
      DeadLetterAddressTest.log.info("i is " + i);
      DeadLetterAddressTest.log.info("delivery cout is " + m.getDeliveryCount());
      Assert.assertEquals(i + 1, m.getDeliveryCount());
      m.acknowledge();
      clientSession.rollback();
    }
    ClientMessage m = clientConsumer.receive(500);
    Assert.assertNull("not expecting a message", m);
    clientConsumer.close();

    clientConsumer = clientSession.createConsumer(deadLetterQueue);
    m = clientConsumer.receive(500);
    Assert.assertNotNull(m);
    Assert.assertEquals(m.getBodyBuffer().readString(), "heyho!");
  }
  @Test
  public void testBrowseWithZeroConsumerWindowSize() throws Exception {
    locator.setConsumerWindowSize(0);

    ClientSessionFactory sf = createSessionFactory(locator);

    ClientSession session = sf.createSession(false, true, true);

    session.createQueue(QUEUE, QUEUE, null, false);

    ClientProducer producer = session.createProducer(QUEUE);

    final int numMessages = 100;

    byte[] bytes = new byte[240];

    for (int i = 0; i < numMessages; i++) {
      ClientMessage message = session.createMessage(false);

      message.getBodyBuffer().writeBytes(bytes);

      message.putIntProperty("foo", i);

      producer.send(message);
    }

    // Create a normal non browsing consumer
    session.createConsumer(QUEUE);

    session.start();

    ClientConsumer browser = session.createConsumer(QUEUE, true);

    for (int i = 0; i < numMessages; i++) {
      ClientMessage message2 = browser.receive(1000);

      assertEquals(i, message2.getIntProperty("foo").intValue());
    }

    session.close();
  }
  @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();
  }
  @Test
  public void testMultipleGroupingConsumeHalf() throws Exception {
    ClientProducer clientProducer = clientSession.createProducer(qName);
    ClientConsumer consumer = clientSession.createConsumer(qName);
    ClientConsumer consumer2 = clientSession.createConsumer(qName);
    clientSession.start();

    // need to wait a bit or consumers might be busy
    Thread.sleep(200);

    SimpleString groupId = new SimpleString("grp1");
    SimpleString groupId2 = new SimpleString("grp2");
    int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
      ClientMessage message = createTextMessage(clientSession, "m" + i);
      if (i % 2 == 0 || i == 0) {
        message.putStringProperty(Message.HDR_GROUP_ID, groupId);
      } else {
        message.putStringProperty(Message.HDR_GROUP_ID, groupId2);
      }
      clientProducer.send(message);
    }

    for (int i = 0; i < numMessages / 2; i++) {
      ClientMessage cm = consumer.receive(500);
      Assert.assertNotNull(cm);
      Assert.assertEquals(cm.getBodyBuffer().readString(), "m" + i);
      i++;
      cm = consumer2.receive(500);
      Assert.assertNotNull(cm);
      Assert.assertEquals(cm.getBodyBuffer().readString(), "m" + i);
    }

    MessageGroupingTest.log.info("closing consumer2");

    consumer2.close();

    consumer.close();
  }
Esempio n. 23
0
  @Test
  public void testReceiveTimesoutCorrectly() throws Exception {

    ClientSessionFactory cf = createSessionFactory(locator);
    ClientSession session = cf.createSession(false, true, true);
    session.createQueue(addressA, queueA, false);
    ClientConsumer cc = session.createConsumer(queueA);
    session.start();
    long time = System.currentTimeMillis();
    cc.receive(1000);
    Assert.assertTrue(System.currentTimeMillis() - time >= 1000);
    session.close();
  }
  @Test
  public void testLargeToken() throws Exception {
    StringBuffer token = new StringBuffer();

    token.append("'");
    for (int i = 0; i < 5000; i++) {
      token.append("a");
    }
    token.append("'");

    // The server would fail to create this consumer if HORNETQ-545 wasn't solved
    consumer = session.createConsumer("foo", "animal=" + token.toString());
  }
Esempio n. 25
0
 @Test
 public void testBasicReceive() throws Exception {
   ClientSessionFactory cf = createSessionFactory(locator);
   ClientSession sendSession = cf.createSession(false, true, true);
   ClientProducer cp = sendSession.createProducer(addressA);
   ClientSession session = cf.createSession(false, true, true);
   session.createQueue(addressA, queueA, false);
   ClientConsumer cc = session.createConsumer(queueA);
   session.start();
   cp.send(sendSession.createMessage(false));
   Assert.assertNotNull(cc.receive());
   session.close();
   sendSession.close();
 }
 @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!");
 }
  @Test
  public void testMessageAttributes() throws Exception {

    ClientSession session = basicSetUp();

    session.createQueue(QUEUE_NAME, QUEUE_NAME, true);

    ClientProducer producer = session.createProducer(QUEUE_NAME);

    ClientMessage msg = session.createMessage(Message.BYTES_TYPE, true);
    msg.setExpiration(Long.MAX_VALUE);
    msg.setPriority((byte) 0);
    msg.setTimestamp(Long.MAX_VALUE - 1);
    msg.setUserID(UUIDGenerator.getInstance().generateUUID());
    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();

    msg = consumer.receive(CONSUMER_TIMEOUT);
    assertEquals(Long.MAX_VALUE, msg.getExpiration());
    assertEquals((byte) 0, msg.getPriority());
    assertEquals(Long.MAX_VALUE - 1, msg.getTimestamp());
    assertNotNull(msg.getUserID());
  }
  @Test
  public void testLoad() throws Throwable {
    ActiveMQServer server2 = createServer(true, false);

    server2.getConfiguration().setJournalFileSize(10 * 1024 * 1024);
    server2.getConfiguration().setJournalMinFiles(10);
    server2.getConfiguration().setJournalCompactMinFiles(3);
    server2.getConfiguration().setJournalCompactPercentage(50);

    for (int i = 0; i < 10; i++) {
      server2.start();

      ServerLocator locator =
          createInVMNonHALocator().setMinLargeMessageSize(1024 * 1024).setBlockOnDurableSend(false);

      ClientSessionFactory sf = createSessionFactory(locator);

      ClientSession session = sf.createSession(true, true);

      try {
        session.createQueue("slow-queue", "slow-queue");
      } catch (Exception ignored) {
      }

      session.start();
      ClientConsumer consumer = session.createConsumer("slow-queue");

      while (true) {
        System.out.println("Received message from previous");
        ClientMessage msg = consumer.receiveImmediate();
        if (msg == null) {
          break;
        }
        msg.acknowledge();
      }

      session.close();

      produceMessages(sf, 30000);

      server2.stop();
    }
  }
  @Test
  public void testStopStartConsumerSyncReceive() throws Exception {
    ClientSessionFactory sf = createSessionFactory(locator);

    final ClientSession session = sf.createSession(false, true, true);

    session.createQueue(QUEUE, QUEUE, null, false);

    ClientProducer producer = session.createProducer(QUEUE);

    final int numMessages = 100;

    for (int i = 0; i < numMessages; i++) {
      ClientMessage message = createTextMessage(session, "m" + i);
      message.putIntProperty(new SimpleString("i"), i);
      producer.send(message);
    }

    final ClientConsumer consumer = session.createConsumer(QUEUE);

    session.start();

    for (int i = 0; i < numMessages / 2; i++) {
      ClientMessage cm = consumer.receive(5000);
      Assert.assertNotNull(cm);
      cm.acknowledge();
    }
    session.stop();
    long time = System.currentTimeMillis();
    ClientMessage cm = consumer.receive(1000);
    long taken = System.currentTimeMillis() - time;
    Assert.assertTrue(taken >= 1000);
    Assert.assertNull(cm);

    session.start();
    for (int i = 0; i < numMessages / 2; i++) {
      cm = consumer.receive(5000);
      Assert.assertNotNull(cm);
      cm.acknowledge();
    }

    session.close();
  }
Esempio n. 30
0
  @Test
  public void testReceiveOnClosedException() throws Exception {

    ClientSessionFactory cf = createSessionFactory(locator);
    ClientSession session = cf.createSession(false, true, true);
    session.createQueue(addressA, queueA, false);
    ClientConsumer cc = session.createConsumer(queueA);
    session.start();
    session.close();
    try {
      cc.receive();
      Assert.fail("should throw exception");
    } catch (ActiveMQObjectClosedException oce) {
      // ok
    } catch (ActiveMQException e) {
      Assert.fail("Invalid Exception type:" + e.getType());
    }
    session.close();
  }