@Override
  @Before
  public void setUp() throws Exception {
    super.setUp();
    server = internalCreateServer();

    server.createAddressInfo(new AddressInfo(ADDRESS, RoutingType.ANYCAST));
    Queue queue = server.createQueue(ADDRESS, RoutingType.ANYCAST, ADDRESS, null, true, false);
    queue.getPageSubscription().getPagingStore().startPaging();

    for (int i = 0; i < 10; i++) {
      queue.getPageSubscription().getPagingStore().forceAnotherPage();
    }

    final ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(null, null, false, true, true, false, 0);
    ClientProducer prod = session.createProducer(ADDRESS);

    for (int i = 0; i < 500; i++) {
      ClientMessage msg = session.createMessage(true);
      msg.putIntProperty("key", i);
      prod.send(msg);
      if (i > 0 && i % 10 == 0) {
        session.commit();
      }
    }

    session.close();
    locator.close();

    server.stop();

    internalCreateServer();
  }
Esempio n. 2
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();
  }
 /**
  * @param session
  * @param producer
  * @throws Exception
  */
 private void sendMessage(ClientSession session, ClientProducer producer, String color, String msg)
     throws Exception {
   ClientMessage anyMessage = session.createMessage(true);
   anyMessage.putStringProperty("color", color);
   anyMessage.putStringProperty("value", msg);
   producer.send(anyMessage);
   session.commit();
 }
  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();
  }
  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();
  }
    @Override
    public void run() {
      ClientSession session = null;
      latchReady.countDown();
      try {
        ActiveMQTestBase.waitForLatch(latchStart);
        session = sf.createSession(false, false);
        session.start();
        ClientConsumer cons = session.createConsumer(QUEUE);
        for (int i = 0; i < numberOfMessages; i++) {
          ClientMessage msg = cons.receive(60 * 1000);
          msg.acknowledge();
          if (i % commitInterval == 0) {
            session.commit();
          }
          if (i % 1000 == 0) {
            // System.out.println(Thread.currentThread().getName() + "::sent #" + i);
          }
        }

        System.out.println(
            "Thread "
                + Thread.currentThread().getName()
                + " received "
                + numberOfMessages
                + " messages");

        session.commit();
      } catch (Throwable e) {
        this.e = e;
      } finally {
        try {
          session.close();
        } catch (Throwable e) {
          this.e = e;
        }
      }
    }
    @Override
    public void run() {
      ClientSession session = null;
      latchReady.countDown();
      try {
        ActiveMQTestBase.waitForLatch(latchStart);
        session = sf.createSession(false, false);
        ClientProducer prod = session.createProducer(ADDRESS);
        for (int i = 0; i < numberOfMessages; i++) {
          if (i % commitInterval == 0) {
            session.commit();
          }
          if (i % 1000 == 0) {
            // System.out.println(Thread.currentThread().getName() + "::received #" + i);
          }
          ClientMessage msg = session.createMessage(true);
          prod.send(msg);
        }

        session.commit();

        System.out.println(
            "Thread "
                + Thread.currentThread().getName()
                + " sent "
                + numberOfMessages
                + "  messages");
      } catch (Throwable e) {
        e.printStackTrace();
        this.e = e;
      } finally {
        try {
          session.close();
        } catch (Throwable e) {
          e.printStackTrace();
        }
      }
    }
  private ClientMessage receiveMessage() throws Exception {
    final ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(null, null, false, true, true, false, 0);

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

    ClientMessage message = consumer.receive(1000);

    session.commit();

    if (message != null) {
      message.acknowledge();
    }

    consumer.close();

    session.close();

    return message;
  }
  public void sendMessages(final int start, final int end) throws Exception {
    try (ClientSession session = factory.createSession(false, false)) {

      try {
        session.createQueue(QUEUE, QUEUE, true);
      } catch (Exception ignored) {
      }

      ClientProducer prod = session.createProducer(QUEUE);

      for (int i = start; i < end; i++) {
        ClientMessage msg = session.createMessage(true);
        msg.putIntProperty(new SimpleString("key"), i);
        msg.getBodyBuffer().writeUTF("message " + i);
        prod.send(msg);
      }

      session.commit();
      session.close();
      // server.stop(); -- this test was not supposed to stop the server, it should crash
    }
  }
  @Test
  public void testLargeMessage() throws Exception {
    server = createServer(true);
    server.start();
    locator = createInVMNonHALocator();
    factory = createSessionFactory(locator);
    ClientSession session = factory.createSession(false, false);

    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();

    session.createQueue("A", "A", true);

    ClientProducer prod = session.createProducer("A");

    prod.send(fileMessage);

    fileMessage.deleteFile();

    session.commit();

    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);
    session.close();
    session = factory.createSession(false, false);
    session.start();

    ClientConsumer cons = session.createConsumer("A");

    ClientMessage msg = cons.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());
    }

    msg.acknowledge();
    session.commit();
  }
  private void doTestTransactional(final TestRunner runner) throws Throwable {
    // For duplication detection
    int executionId = 0;

    while (!runner.isFailed()) {
      ClientSession session = null;

      executionId++;

      log.info("#test doTestTransactional starting now. Execution " + executionId);

      try {

        boolean retry = false;

        final int numMessages = 1000;

        session = sf.createSession(false, false);

        listener = new CountDownSessionFailureListener(session);
        session.addFailureListener(listener);

        do {
          try {
            ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);

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

              message.getBodyBuffer().writeString("message" + i);

              message.putIntProperty("counter", i);

              message.putStringProperty(
                  Message.HDR_DUPLICATE_DETECTION_ID,
                  new SimpleString("id:" + i + ",exec:" + executionId));

              addPayload(message);

              if (log.isDebugEnabled()) {
                log.debug("Sending message " + message);
              }

              producer.send(message);
            }

            log.debug("Sending commit");
            session.commit();

            retry = false;
          } catch (ActiveMQDuplicateIdException die) {
            logAndSystemOut("#test duplicate id rejected on sending");
            break;
          } catch (ActiveMQTransactionRolledBackException trbe) {
            log.info("#test transaction rollback retrying on sending");
            // OK
            retry = true;
          } catch (ActiveMQUnBlockedException ube) {
            log.info("#test transaction rollback retrying on sending");
            // OK
            retry = true;
          } catch (ActiveMQTransactionOutcomeUnknownException toue) {
            log.info("#test transaction rollback retrying on sending");
            // OK
            retry = true;
          } catch (ActiveMQException e) {
            log.info("#test Exception " + e, e);
            throw e;
          }
        } while (retry);

        logAndSystemOut("#test Finished sending, starting consumption now");

        boolean blocked = false;

        retry = false;

        ClientConsumer consumer = null;
        do {
          ArrayList<Integer> msgs = new ArrayList<>();
          try {
            if (consumer == null) {
              consumer = session.createConsumer(FailoverTestBase.ADDRESS);
              session.start();
            }

            for (int i = 0; i < numMessages; i++) {
              if (log.isDebugEnabled()) {
                log.debug("Consumer receiving message " + i);
              }
              ClientMessage message = consumer.receive(10000);
              if (message == null) {
                break;
              }

              if (log.isDebugEnabled()) {
                log.debug("Received message " + message);
              }

              int count = message.getIntProperty("counter");

              if (count != i) {
                log.warn("count was received out of order, " + count + "!=" + i);
              }

              msgs.add(count);

              message.acknowledge();
            }

            log.info("#test commit");
            try {
              session.commit();
            } catch (ActiveMQTransactionRolledBackException trbe) {
              // we know the tx has been rolled back so we just consume again
              retry = true;
              continue;
            } catch (ActiveMQException e) {
              // This could eventually happen
              // We will get rid of this when we implement 2 phase commit on failover
              log.warn("exception during commit, it will be ignored for now" + e.getMessage(), e);
            }

            try {
              if (blocked) {
                assertTrue(
                    "msgs.size is expected to be 0 or "
                        + numMessages
                        + " but it was "
                        + msgs.size(),
                    msgs.size() == 0 || msgs.size() == numMessages);
              } else {
                assertTrue(
                    "msgs.size is expected to be " + numMessages + " but it was " + msgs.size(),
                    msgs.size() == numMessages);
              }
            } catch (Throwable e) {
              log.info(threadDump("Thread dump, messagesReceived = " + msgs.size()));
              logAndSystemOut(e.getMessage() + " messages received");
              for (Integer msg : msgs) {
                logAndSystemOut(msg.toString());
              }
              throw e;
            }

            int i = 0;
            for (Integer msg : msgs) {
              assertEquals(i++, (int) msg);
            }

            retry = false;
            blocked = false;
          } catch (ActiveMQTransactionRolledBackException trbe) {
            logAndSystemOut("Transaction rolled back with " + msgs.size(), trbe);
            // TODO: https://jira.jboss.org/jira/browse/HORNETQ-369
            // ATM RolledBack exception is being called with the transaction is committed.
            // the test will fail if you remove this next line
            blocked = true;
            retry = true;
          } catch (ActiveMQTransactionOutcomeUnknownException tou) {
            logAndSystemOut("Transaction rolled back with " + msgs.size(), tou);
            // TODO: https://jira.jboss.org/jira/browse/HORNETQ-369
            // ATM RolledBack exception is being called with the transaction is committed.
            // the test will fail if you remove this next line
            blocked = true;
            retry = true;
          } catch (ActiveMQUnBlockedException ube) {
            logAndSystemOut("Unblocked with " + msgs.size(), ube);
            // TODO: https://jira.jboss.org/jira/browse/HORNETQ-369
            // This part of the test is never being called.
            blocked = true;
            retry = true;
          } catch (ActiveMQException e) {
            logAndSystemOut(e.getMessage(), e);
            throw e;
          }
        } while (retry);
      } finally {
        if (session != null) {
          session.close();
        }
      }

      listener = null;
    }
  }
  @Test
  public void testProduceAndConsume() throws Throwable {
    int numberOfConsumers = 5;
    // this test assumes numberOfConsumers == numberOfProducers
    int numberOfProducers = numberOfConsumers;
    int produceMessage = 10000;
    int commitIntervalProduce = 100;
    int consumeMessage = (int) (produceMessage * 0.9);
    int commitIntervalConsume = 100;

    ClientSession session = sf.createSession(false, false);
    session.createQueue("compact", "compact-queue", true);

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

    for (int i = 0; i < 100; i++) {
      producer.send(session.createMessage(true));
    }

    session.commit();

    // Number of messages expected to be received after restart
    int numberOfMessagesExpected = (produceMessage - consumeMessage) * numberOfConsumers;

    CountDownLatch latchReady = new CountDownLatch(numberOfConsumers + numberOfProducers);

    CountDownLatch latchStart = new CountDownLatch(1);

    ArrayList<BaseThread> threads = new ArrayList<BaseThread>();

    ProducerThread[] prod = new ProducerThread[numberOfProducers];
    for (int i = 0; i < numberOfProducers; i++) {
      prod[i] =
          new ProducerThread(i, latchReady, latchStart, produceMessage, commitIntervalProduce);
      prod[i].start();
      threads.add(prod[i]);
    }

    ConsumerThread[] cons = new ConsumerThread[numberOfConsumers];

    for (int i = 0; i < numberOfConsumers; i++) {
      cons[i] =
          new ConsumerThread(i, latchReady, latchStart, consumeMessage, commitIntervalConsume);
      cons[i].start();
      threads.add(cons[i]);
    }

    ActiveMQTestBase.waitForLatch(latchReady);
    latchStart.countDown();

    for (BaseThread t : threads) {
      t.join();
      if (t.e != null) {
        throw t.e;
      }
    }

    server.stop();

    setupServer(JournalType.NIO);

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

    ClientConsumer consumer = sess.createConsumer(QUEUE);

    sess.start();

    for (int i = 0; i < numberOfMessagesExpected; i++) {
      ClientMessage msg = consumer.receive(5000);
      Assert.assertNotNull(msg);

      if (i % 1000 == 0) {
        System.out.println("Received #" + i + "  on thread before end");
      }
      msg.acknowledge();
    }

    Assert.assertNull(consumer.receiveImmediate());

    sess.close();
  }