Search in sources :

Example 26 with QueueControl

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

the class QueueControlTest method testRetryDivertedMessage.

/**
 * Test retry - get a diverted message from DLQ and put on original queue.
 */
@Test
public void testRetryDivertedMessage() throws Exception {
    final SimpleString dla = new SimpleString("DLA");
    final SimpleString dlq = new SimpleString("DLQ");
    final SimpleString forwardingQueue = new SimpleString("forwardingQueue");
    final SimpleString forwardingAddress = new SimpleString("forwardingAddress");
    final SimpleString myTopic = new SimpleString("myTopic");
    final String sampleText = "Put me on DLQ";
    AddressSettings addressSettings = new AddressSettings().setMaxDeliveryAttempts(1).setDeadLetterAddress(dla);
    server.getAddressSettingsRepository().addMatch(forwardingAddress.toString(), addressSettings);
    // create target queue, DLQ and source topic
    session.createQueue(dla, RoutingType.MULTICAST, dlq, null, durable);
    session.createQueue(forwardingAddress, RoutingType.MULTICAST, forwardingQueue, null, durable);
    session.createAddress(myTopic, RoutingType.MULTICAST, false);
    DivertConfiguration divert = new DivertConfiguration().setName("local-divert").setRoutingName("some-name").setAddress(myTopic.toString()).setForwardingAddress(forwardingAddress.toString()).setExclusive(false);
    server.deployDivert(divert);
    // Send message to topic.
    ClientProducer producer = session.createProducer(myTopic);
    producer.send(createTextMessage(session, sampleText));
    session.start();
    ClientConsumer clientConsumer = session.createConsumer(forwardingQueue);
    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, RoutingType.MULTICAST);
    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...
    assertMessageMetrics(queueControl, 0, durable);
    // .. and that the message is now on the original queue once more.
    clientMessage = clientConsumer.receive(500);
    // fails because of AMQ222196 !!!
    Assert.assertNotNull(clientMessage);
    clientMessage.acknowledge();
    Assert.assertEquals(sampleText, clientMessage.getBodyBuffer().readString());
    clientConsumer.close();
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) DivertConfiguration(org.apache.activemq.artemis.core.config.DivertConfiguration) 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 27 with QueueControl

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

the class QueueControlTest method testExpireMessagesWithFilter.

@Test
public void testExpireMessagesWithFilter() throws Exception {
    SimpleString key = new SimpleString("key");
    long matchingValue = RandomUtil.randomLong();
    long unmatchingValue = matchingValue + 1;
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    session.createQueue(address, RoutingType.MULTICAST, queue, null, durable);
    ClientProducer producer = session.createProducer(address);
    // send on queue
    ClientMessage matchingMessage = session.createMessage(durable);
    matchingMessage.putLongProperty(key, matchingValue);
    producer.send(matchingMessage);
    ClientMessage unmatchingMessage = session.createMessage(durable);
    unmatchingMessage.putLongProperty(key, unmatchingValue);
    producer.send(unmatchingMessage);
    QueueControl queueControl = createManagementControl(address, queue);
    Assert.assertEquals(2, getMessageCount(queueControl));
    int expiredMessagesCount = queueControl.expireMessages(key + " =" + matchingValue);
    Assert.assertEquals(1, expiredMessagesCount);
    assertMessageMetrics(queueControl, 1, durable);
    // consume the unmatched message from queue
    ClientConsumer consumer = session.createConsumer(queue);
    ClientMessage m = consumer.receive(500);
    Assert.assertNotNull(m);
    Assert.assertEquals(unmatchingValue, m.getObjectProperty(key));
    m.acknowledge();
    // check there is no other message to consume:
    m = consumer.receiveImmediate();
    Assert.assertNull(m);
    consumer.close();
    session.deleteQueue(queue);
    session.close();
}
Also used : 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 28 with QueueControl

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

the class QueueControlTest method testRemoveMessagesWithNullFilter.

@Test
public void testRemoveMessagesWithNullFilter() throws Exception {
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    session.createQueue(address, RoutingType.MULTICAST, queue, null, durable);
    ClientProducer producer = session.createProducer(address);
    // send on queue
    producer.send(session.createMessage(durable));
    producer.send(session.createMessage(durable));
    QueueControl queueControl = createManagementControl(address, queue);
    assertMessageMetrics(queueControl, 2, durable);
    // removed matching messages to otherQueue
    int removedMatchedMessagesCount = queueControl.removeMessages(null);
    Assert.assertEquals(2, removedMatchedMessagesCount);
    assertMessageMetrics(queueControl, 0, durable);
    session.deleteQueue(queue);
}
Also used : SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) QueueControl(org.apache.activemq.artemis.api.core.management.QueueControl) Test(org.junit.Test)

Example 29 with QueueControl

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

the class QueueControlTest method testRemoveScheduledMessage.

@Test
public void testRemoveScheduledMessage() throws Exception {
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    session.createQueue(address, RoutingType.MULTICAST, queue, null, durable);
    ClientProducer producer = session.createProducer(address);
    // send 2 messages on queue, both scheduled
    long timeout = System.currentTimeMillis() + 5000;
    ClientMessage m1 = session.createMessage(durable);
    m1.putLongProperty(Message.HDR_SCHEDULED_DELIVERY_TIME, timeout);
    producer.send(m1);
    ClientMessage m2 = session.createMessage(durable);
    m2.putLongProperty(Message.HDR_SCHEDULED_DELIVERY_TIME, timeout);
    producer.send(m2);
    QueueControl queueControl = createManagementControl(address, queue);
    Assert.assertEquals(2, queueControl.getScheduledCount());
    // the message IDs are set on the server
    Map<String, Object>[] messages = queueControl.listScheduledMessages();
    Assert.assertEquals(2, messages.length);
    long messageID = (Long) messages[0].get("messageID");
    // delete 1st message
    boolean deleted = queueControl.removeMessage(messageID);
    Assert.assertTrue(deleted);
    assertScheduledMetrics(queueControl, 1, durable);
    // check there is a single message to consume from queue
    while (timeout > System.currentTimeMillis() && queueControl.getScheduledCount() == 1) {
        Thread.sleep(100);
    }
    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) Map(java.util.Map) HashMap(java.util.HashMap) QueueControl(org.apache.activemq.artemis.api.core.management.QueueControl) Test(org.junit.Test)

Example 30 with QueueControl

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

the class QueueControlTest method testRemoveAllMessages.

@Test
public void testRemoveAllMessages() throws Exception {
    SimpleString address = RandomUtil.randomSimpleString();
    SimpleString queue = RandomUtil.randomSimpleString();
    session.createQueue(address, RoutingType.MULTICAST, queue, null, durable);
    ClientProducer producer = session.createProducer(address);
    // send on queue
    producer.send(session.createMessage(durable));
    producer.send(session.createMessage(durable));
    QueueControl queueControl = createManagementControl(address, queue);
    assertMessageMetrics(queueControl, 2, durable);
    // removed matching messages to otherQueue
    int removedMatchedMessagesCount = queueControl.removeAllMessages();
    Assert.assertEquals(2, removedMatchedMessagesCount);
    assertMessageMetrics(queueControl, 0, durable);
    session.deleteQueue(queue);
}
Also used : SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) 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