Search in sources :

Example 81 with QueueControl

use of org.apache.activemq.artemis.api.core.management.QueueControl in project activemq-artemis by apache.

the class QueueControlTest method testMoveMessagesToUnknownQueue.

@Test
public void testMoveMessagesToUnknownQueue() throws Exception {
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    SimpleString unknownQueue = RandomUtil.randomSimpleString();
    session.createQueue(address, RoutingType.MULTICAST, queue, null, durable);
    ClientProducer producer = session.createProducer(address);
    // send on queue
    ClientMessage message = session.createMessage(durable);
    SimpleString key = RandomUtil.randomSimpleString();
    long value = RandomUtil.randomLong();
    message.putLongProperty(key, value);
    producer.send(message);
    QueueControl queueControl = createManagementControl(address, queue);
    assertMessageMetrics(queueControl, 1, durable);
    // moved all messages to unknown queue
    try {
        queueControl.moveMessages(null, unknownQueue.toString());
        Assert.fail("operation must fail if the other queue does not exist");
    } catch (Exception e) {
    }
    Assert.assertEquals(1, getMessageCount(queueControl));
    assertMessageMetrics(queueControl, 1, durable);
    consumeMessages(1, session, queue);
    session.deleteQueue(queue);
}
Also used : SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) QueueControl(org.apache.activemq.artemis.api.core.management.QueueControl) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) Test(org.junit.Test)

Example 82 with QueueControl

use of org.apache.activemq.artemis.api.core.management.QueueControl in project activemq-artemis by apache.

the class QueueControlTest method testRetryMessage.

/**
 * Test retry - get a message from DLQ and put on original queue.
 */
@Test
public void testRetryMessage() throws Exception {
    final SimpleString dla = new SimpleString("DLA");
    final SimpleString qName = new SimpleString("q1");
    final SimpleString adName = new SimpleString("ad1");
    final SimpleString dlq = new SimpleString("DLQ1");
    final String sampleText = "Put me on DLQ";
    AddressSettings addressSettings = new AddressSettings().setMaxDeliveryAttempts(1).setDeadLetterAddress(dla);
    server.getAddressSettingsRepository().addMatch(adName.toString(), addressSettings);
    session.createQueue(dla, RoutingType.MULTICAST, dlq, null, durable);
    session.createQueue(adName, RoutingType.MULTICAST, qName, null, durable);
    // Send message to queue.
    ClientProducer producer = session.createProducer(adName);
    producer.send(createTextMessage(session, sampleText));
    session.start();
    ClientConsumer clientConsumer = session.createConsumer(qName);
    ClientMessage clientMessage = clientConsumer.receive(500);
    clientMessage.acknowledge();
    Assert.assertNotNull(clientMessage);
    Assert.assertEquals(clientMessage.getBodyBuffer().readString(), sampleText);
    // force a rollback to DLQ
    session.rollback();
    clientMessage = clientConsumer.receiveImmediate();
    Assert.assertNull(clientMessage);
    QueueControl queueControl = createManagementControl(dla, dlq);
    assertMessageMetrics(queueControl, 1, durable);
    final long messageID = getFirstMessageId(queueControl);
    // Retry the message - i.e. it should go from DLQ to original Queue.
    Assert.assertTrue(queueControl.retryMessage(messageID));
    // Assert DLQ is empty...
    Assert.assertEquals(0, getMessageCount(queueControl));
    assertMessageMetrics(queueControl, 0, durable);
    // .. and that the message is now on the original queue once more.
    clientMessage = clientConsumer.receive(500);
    clientMessage.acknowledge();
    Assert.assertNotNull(clientMessage);
    Assert.assertEquals(sampleText, clientMessage.getBodyBuffer().readString());
    clientConsumer.close();
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) QueueControl(org.apache.activemq.artemis.api.core.management.QueueControl) Test(org.junit.Test)

Example 83 with QueueControl

use of org.apache.activemq.artemis.api.core.management.QueueControl in project activemq-artemis by apache.

the class QueueControlTest method testListDeliveringMessagesWithRASession.

// https://issues.jboss.org/browse/HORNETQ-1231
@Test
public void testListDeliveringMessagesWithRASession() throws Exception {
    ServerLocator locator1 = createInVMNonHALocator().setBlockOnNonDurableSend(true).setConsumerWindowSize(10240).setAckBatchSize(0);
    ClientSessionFactory sf = locator1.createSessionFactory();
    final ClientSession transSession = sf.createSession(false, true, false);
    ClientConsumer consumer = null;
    SimpleString queue = null;
    int numMsg = 10;
    try {
        // a session from RA does this
        transSession.addMetaData("resource-adapter", "inbound");
        transSession.addMetaData("jms-session", "");
        SimpleString address = RandomUtil.randomSimpleString();
        queue = RandomUtil.randomSimpleString();
        transSession.createQueue(address, RoutingType.MULTICAST, queue, null, durable);
        final QueueControl queueControl = createManagementControl(address, queue);
        ClientProducer producer = transSession.createProducer(address);
        for (int i = 0; i < numMsg; i++) {
            ClientMessage message = transSession.createMessage(durable);
            message.putIntProperty(new SimpleString("seqno"), i);
            producer.send(message);
        }
        consumer = transSession.createConsumer(queue);
        transSession.start();
        /**
         * the following latches are used to make sure that
         *
         * 1. the first call on queueControl happens after the
         * first message arrived at the message handler.
         *
         * 2. the message handler wait on the first message until
         * the queueControl returns the right/wrong result.
         *
         * 3. the test exits after all messages are received.
         */
        final CountDownLatch latch1 = new CountDownLatch(1);
        final CountDownLatch latch2 = new CountDownLatch(1);
        final CountDownLatch latch3 = new CountDownLatch(10);
        consumer.setMessageHandler(new MessageHandler() {

            @Override
            public void onMessage(ClientMessage message) {
                try {
                    message.acknowledge();
                } catch (ActiveMQException e1) {
                    e1.printStackTrace();
                }
                latch1.countDown();
                try {
                    latch2.await(10, TimeUnit.SECONDS);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                latch3.countDown();
            }
        });
        latch1.await(10, TimeUnit.SECONDS);
        // now we know the ack of the message is sent but to make sure
        // the server has received it, we try 5 times
        int n = 0;
        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000);
            String jsonStr = queueControl.listDeliveringMessagesAsJSON();
            n = countOccurrencesOf(jsonStr, "seqno");
            if (n == numMsg) {
                break;
            }
        }
        assertEquals(numMsg, n);
        latch2.countDown();
        latch3.await(10, TimeUnit.SECONDS);
        transSession.commit();
    } finally {
        consumer.close();
        transSession.deleteQueue(queue);
        transSession.close();
        locator1.close();
    }
}
Also used : MessageHandler(org.apache.activemq.artemis.api.core.client.MessageHandler) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) CountDownLatch(java.util.concurrent.CountDownLatch) QueueControl(org.apache.activemq.artemis.api.core.management.QueueControl) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 84 with QueueControl

use of org.apache.activemq.artemis.api.core.management.QueueControl in project activemq-artemis by apache.

the class QueueControlTest method testGetConsumerJSON.

@Test
public void testGetConsumerJSON() throws Exception {
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    session.createQueue(address, RoutingType.MULTICAST, queue, null, durable);
    QueueControl queueControl = createManagementControl(address, queue);
    Assert.assertEquals(0, queueControl.getConsumerCount());
    ClientConsumer consumer = session.createConsumer(queue);
    Assert.assertEquals(1, queueControl.getConsumerCount());
    System.out.println("Consumers: " + queueControl.listConsumersAsJSON());
    JsonArray obj = JsonUtil.readJsonArray(queueControl.listConsumersAsJSON());
    assertEquals(1, obj.size());
    consumer.close();
    Assert.assertEquals(0, queueControl.getConsumerCount());
    obj = JsonUtil.readJsonArray(queueControl.listConsumersAsJSON());
    assertEquals(0, obj.size());
    session.deleteQueue(queue);
}
Also used : JsonArray(javax.json.JsonArray) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) QueueControl(org.apache.activemq.artemis.api.core.management.QueueControl) Test(org.junit.Test)

Example 85 with QueueControl

use of org.apache.activemq.artemis.api.core.management.QueueControl in project activemq-artemis by apache.

the class QueueControlTest method testListMessageCounterHistory.

@Test
public void testListMessageCounterHistory() throws Exception {
    long counterPeriod = 1000;
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    session.createQueue(address, RoutingType.MULTICAST, queue, null, durable);
    QueueControl queueControl = createManagementControl(address, queue);
    ActiveMQServerControl serverControl = ManagementControlHelper.createActiveMQServerControl(mbeanServer);
    serverControl.enableMessageCounters();
    serverControl.setMessageCounterSamplePeriod(counterPeriod);
    String jsonString = queueControl.listMessageCounterHistory();
    DayCounterInfo[] infos = DayCounterInfo.fromJSON(jsonString);
    Assert.assertEquals(1, infos.length);
    session.deleteQueue(queue);
}
Also used : ActiveMQServerControl(org.apache.activemq.artemis.api.core.management.ActiveMQServerControl) DayCounterInfo(org.apache.activemq.artemis.api.core.management.DayCounterInfo) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) QueueControl(org.apache.activemq.artemis.api.core.management.QueueControl) Test(org.junit.Test)

Aggregations

QueueControl (org.apache.activemq.artemis.api.core.management.QueueControl)109 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)87 Test (org.junit.Test)79 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)50 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)33 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)24 HashMap (java.util.HashMap)21 Map (java.util.Map)18 ActiveMQServerControl (org.apache.activemq.artemis.api.core.management.ActiveMQServerControl)15 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)9 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)9 ArrayList (java.util.ArrayList)7 JsonObject (javax.json.JsonObject)7 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)7 MessageProducer (javax.jms.MessageProducer)6 TextMessage (javax.jms.TextMessage)6 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)6 Session (javax.jms.Session)5 JsonArray (javax.json.JsonArray)5 Queue (org.apache.activemq.artemis.core.server.Queue)5