Search in sources :

Example 6 with QueueControl

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

the class QueueControlTest method testGetScheduledCount.

@Test
public void testGetScheduledCount() throws Exception {
    long delay = 500;
    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.getScheduledCount());
    ClientProducer producer = session.createProducer(address);
    ClientMessage message = session.createMessage(durable);
    message.putLongProperty(Message.HDR_SCHEDULED_DELIVERY_TIME, System.currentTimeMillis() + delay);
    producer.send(message);
    long timeout = System.currentTimeMillis() + 5000;
    while (timeout > System.currentTimeMillis() && queueControl.getScheduledCount() != 1) {
        Thread.sleep(100);
    }
    assertScheduledMetrics(queueControl, 1, durable);
    assertMessageMetrics(queueControl, 1, durable);
    consumeMessages(0, session, queue);
    Thread.sleep(delay * 2);
    Assert.assertEquals(0, queueControl.getScheduledCount());
    consumeMessages(1, session, queue);
    assertMessageMetrics(queueControl, 0, durable);
    assertScheduledMetrics(queueControl, 0, durable);
    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) Test(org.junit.Test)

Example 7 with QueueControl

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

the class QueueControlTest method testSendMessageToDeadLetterAddress.

@Test
public void testSendMessageToDeadLetterAddress() throws Exception {
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    SimpleString deadLetterAddress = RandomUtil.randomSimpleString();
    SimpleString deadLetterQueue = RandomUtil.randomSimpleString();
    session.createQueue(address, RoutingType.MULTICAST, queue, null, durable);
    session.createQueue(deadLetterAddress, RoutingType.MULTICAST, deadLetterQueue, null, durable);
    ClientProducer producer = session.createProducer(address);
    // send 2 messages on queue
    producer.send(session.createMessage(durable));
    producer.send(session.createMessage(durable));
    QueueControl queueControl = createManagementControl(address, queue);
    QueueControl deadLetterQueueControl = createManagementControl(deadLetterAddress, deadLetterQueue);
    assertMessageMetrics(queueControl, 2, durable);
    // the message IDs are set on the server
    Map<String, Object>[] messages = queueControl.listMessages(null);
    Assert.assertEquals(2, messages.length);
    long messageID = (Long) messages[0].get("messageID");
    AddressSettings addressSettings = new AddressSettings().setDeadLetterAddress(deadLetterAddress);
    server.getAddressSettingsRepository().addMatch(address.toString(), addressSettings);
    Assert.assertEquals(0, getMessageCount(deadLetterQueueControl));
    boolean movedToDeadLetterAddress = queueControl.sendMessageToDeadLetterAddress(messageID);
    Assert.assertTrue(movedToDeadLetterAddress);
    assertMessageMetrics(queueControl, 1, durable);
    Thread.sleep(200);
    assertMessageMetrics(deadLetterQueueControl, 1, durable);
    // check there is a single message to consume from queue
    consumeMessages(1, session, queue);
    // check there is a single message to consume from deadletter queue
    consumeMessages(1, session, deadLetterQueue);
    session.deleteQueue(queue);
    session.deleteQueue(deadLetterQueue);
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Map(java.util.Map) HashMap(java.util.HashMap) QueueControl(org.apache.activemq.artemis.api.core.management.QueueControl) Test(org.junit.Test)

Example 8 with QueueControl

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

the class QueueControlTest method testRemoveMessage2.

@Test
public void testRemoveMessage2() throws Exception {
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    session.createQueue(address, RoutingType.MULTICAST, queue, null, durable);
    ClientProducer producer = session.createProducer(address);
    for (int i = 0; i < 100; i++) {
        ClientMessage msg = session.createMessage(durable);
        msg.putIntProperty("count", i);
        producer.send(msg);
    }
    ClientConsumer cons = session.createConsumer(queue);
    session.start();
    LinkedList<ClientMessage> msgs = new LinkedList<>();
    for (int i = 0; i < 50; i++) {
        ClientMessage msg = cons.receive(1000);
        msgs.add(msg);
    }
    QueueControl queueControl = createManagementControl(address, queue);
    assertMessageMetrics(queueControl, 100, durable);
    // the message IDs are set on the server
    Map<String, Object>[] messages = queueControl.listMessages(null);
    Assert.assertEquals(50, messages.length);
    int i = Integer.parseInt((messages[0].get("count")).toString());
    assertEquals(50, i);
    long messageID = (Long) messages[0].get("messageID");
    // delete 1st message
    boolean deleted = queueControl.removeMessage(messageID);
    Assert.assertTrue(deleted);
    assertMessageMetrics(queueControl, 99, durable);
    cons.close();
    // check there is a single message to consume from queue
    consumeMessages(99, session, queue);
    session.deleteQueue(queue);
}
Also used : SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) LinkedList(java.util.LinkedList) QueueControl(org.apache.activemq.artemis.api.core.management.QueueControl) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 9 with QueueControl

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

the class QueueControlTest method testListDeliveringMessages.

@Test
public void testListDeliveringMessages() throws Exception {
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    int intValue = RandomUtil.randomInt();
    session.createQueue(address, RoutingType.MULTICAST, queue, null, durable);
    Queue srvqueue = server.locateQueue(queue);
    QueueControl queueControl = createManagementControl(address, queue);
    ClientProducer producer = session.createProducer(address);
    ClientMessage message = session.createMessage(durable);
    message.putIntProperty(new SimpleString("key"), intValue);
    producer.send(message);
    producer.send(session.createMessage(durable));
    ClientConsumer consumer = session.createConsumer(queue);
    session.start();
    ClientMessage msgRec = consumer.receive(5000);
    assertNotNull(msgRec);
    assertEquals(msgRec.getIntProperty("key").intValue(), intValue);
    ClientSessionFactory sf2 = createSessionFactory(locator);
    ClientSession session2 = sf2.createSession(false, true, false);
    ClientConsumer consumer2 = session2.createConsumer(queue);
    session2.start();
    ClientMessage msgRec2 = consumer2.receive(5000);
    assertNotNull(msgRec2);
    assertEquals(2, srvqueue.getDeliveringCount());
    assertEquals(2, srvqueue.getConsumerCount());
    System.out.println(queueControl.listDeliveringMessagesAsJSON());
    Map<String, Map<String, Object>[]> deliveringMap = queueControl.listDeliveringMessages();
    assertEquals(2, deliveringMap.size());
    consumer.close();
    consumer2.close();
    session.deleteQueue(queue);
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) JsonObject(javax.json.JsonObject) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) Queue(org.apache.activemq.artemis.core.server.Queue) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) QueueControl(org.apache.activemq.artemis.api.core.management.QueueControl) Test(org.junit.Test)

Example 10 with QueueControl

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

the class QueueControlTest method testRetryMultipleMessages.

/**
 * Test retry multiple messages from  DLQ to original queue.
 */
@Test
public void testRetryMultipleMessages() 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";
    final int numMessagesToTest = 10;
    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);
    for (int i = 0; i < numMessagesToTest; i++) {
        producer.send(createTextMessage(session, sampleText));
    }
    session.start();
    final LocalQueueBinding binding = (LocalQueueBinding) server.getPostOffice().getBinding(qName);
    Queue q = binding.getQueue();
    final LocalQueueBinding binding2 = (LocalQueueBinding) server.getPostOffice().getBinding(dlq);
    Queue q2 = binding2.getQueue();
    Field queueMemorySizeField = QueueImpl.class.getDeclaredField("queueMemorySize");
    queueMemorySizeField.setAccessible(true);
    // Get memory size counters to verify
    AtomicInteger queueMemorySize1 = (AtomicInteger) queueMemorySizeField.get(q);
    AtomicInteger queueMemorySize2 = (AtomicInteger) queueMemorySizeField.get(q2);
    // Verify that original queue has a memory size greater than 0 and DLQ is 0
    assertTrue(queueMemorySize1.get() > 0);
    assertTrue(queueMemorySize2.get() == 0);
    // Read and rollback all messages to DLQ
    ClientConsumer clientConsumer = session.createConsumer(qName);
    for (int i = 0; i < numMessagesToTest; i++) {
        ClientMessage clientMessage = clientConsumer.receive(500);
        clientMessage.acknowledge();
        Assert.assertNotNull(clientMessage);
        Assert.assertEquals(clientMessage.getBodyBuffer().readString(), sampleText);
        session.rollback();
    }
    Assert.assertNull(clientConsumer.receiveImmediate());
    // Verify that original queue has a memory size of 0 and DLQ is greater than 0 after rollback
    assertTrue(queueMemorySize1.get() == 0);
    assertTrue(queueMemorySize2.get() > 0);
    QueueControl dlqQueueControl = createManagementControl(dla, dlq);
    assertMessageMetrics(dlqQueueControl, numMessagesToTest, durable);
    // Retry all messages - i.e. they should go from DLQ to original Queue.
    Assert.assertEquals(numMessagesToTest, dlqQueueControl.retryMessages());
    // Assert DLQ is empty...
    assertMessageMetrics(dlqQueueControl, 0, durable);
    // Verify that original queue has a memory size of greater than 0 and DLQ is 0 after move
    assertTrue(queueMemorySize1.get() > 0);
    assertTrue(queueMemorySize2.get() == 0);
    // .. and that the messages is now on the original queue once more.
    for (int i = 0; i < numMessagesToTest; i++) {
        ClientMessage clientMessage = clientConsumer.receive(500);
        clientMessage.acknowledge();
        Assert.assertNotNull(clientMessage);
        Assert.assertEquals(clientMessage.getBodyBuffer().readString(), sampleText);
    }
    clientConsumer.close();
    // Verify that original queue and DLQ have a memory size of 0
    assertTrue(queueMemorySize1.get() == 0);
    assertTrue(queueMemorySize2.get() == 0);
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) LocalQueueBinding(org.apache.activemq.artemis.core.postoffice.impl.LocalQueueBinding) Field(java.lang.reflect.Field) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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) Queue(org.apache.activemq.artemis.core.server.Queue) 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