Search in sources :

Example 16 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class QueueImplTest method testBusyConsumerWithFilterThenAddMoreMessages.

@Test
public void testBusyConsumerWithFilterThenAddMoreMessages() throws Exception {
    QueueImpl queue = getTemporaryQueue();
    FakeConsumer consumer = new FakeConsumer(FilterImpl.createFilter("color = 'green'"));
    consumer.setStatusImmediate(HandleStatus.BUSY);
    queue.addConsumer(consumer);
    final int numMessages = 10;
    List<MessageReference> refs = new ArrayList<>();
    for (int i = 0; i < numMessages; i++) {
        MessageReference ref = generateReference(queue, i);
        ref.getMessage().putStringProperty("color", "red");
        refs.add(ref);
        queue.addTail(ref);
    }
    Assert.assertEquals(10, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(0, queue.getDeliveringCount());
    queue.deliverNow();
    Assert.assertEquals(10, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(0, queue.getDeliveringCount());
    Assert.assertTrue(consumer.getReferences().isEmpty());
    for (int i = numMessages; i < numMessages * 2; i++) {
        MessageReference ref = generateReference(queue, i);
        refs.add(ref);
        ref.getMessage().putStringProperty("color", "green");
        queue.addTail(ref);
    }
    Assert.assertEquals(20, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(0, queue.getDeliveringCount());
    Assert.assertTrue(consumer.getReferences().isEmpty());
    consumer.setStatusImmediate(null);
    for (int i = numMessages * 2; i < numMessages * 3; i++) {
        MessageReference ref = generateReference(queue, i);
        refs.add(ref);
        queue.addTail(ref);
    }
    queue.deliverNow();
    Assert.assertEquals(numMessages, consumer.getReferences().size());
    Assert.assertEquals(30, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(10, queue.getDeliveringCount());
    List<MessageReference> receeivedRefs = consumer.getReferences();
    int currId = 10;
    for (MessageReference receeivedRef : receeivedRefs) {
        Assert.assertEquals("messages received out of order", receeivedRef.getMessage().getMessageID(), currId++);
    }
}
Also used : FakeConsumer(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer) ArrayList(java.util.ArrayList) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) Test(org.junit.Test)

Example 17 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class QueueImplTest method testGetReference.

@Test
public void testGetReference() throws Exception {
    QueueImpl queue = getTemporaryQueue();
    MessageReference messageReference = generateReference(queue, 1);
    MessageReference messageReference2 = generateReference(queue, 2);
    MessageReference messageReference3 = generateReference(queue, 3);
    queue.addHead(messageReference, false);
    queue.addHead(messageReference2, false);
    queue.addHead(messageReference3, false);
    Assert.assertEquals(queue.getReference(2), messageReference2);
}
Also used : MessageReference(org.apache.activemq.artemis.core.server.MessageReference) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) Test(org.junit.Test)

Example 18 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class QueueImplTest method testConsumerWithFilters.

// Private ------------------------------------------------------------------------------
private void testConsumerWithFilters(final boolean direct) throws Exception {
    QueueImpl queue = getTemporaryQueue();
    Filter filter = new FakeFilter("fruit", "orange");
    FakeConsumer consumer = new FakeConsumer(filter);
    if (direct) {
        queue.addConsumer(consumer);
    }
    List<MessageReference> refs = new ArrayList<>();
    MessageReference ref1 = generateReference(queue, 1);
    ref1.getMessage().putStringProperty(new SimpleString("fruit"), new SimpleString("banana"));
    queue.addTail(ref1);
    MessageReference ref2 = generateReference(queue, 2);
    ref2.getMessage().putStringProperty(new SimpleString("cheese"), new SimpleString("stilton"));
    queue.addTail(ref2);
    MessageReference ref3 = generateReference(queue, 3);
    ref3.getMessage().putStringProperty(new SimpleString("cake"), new SimpleString("sponge"));
    queue.addTail(ref3);
    MessageReference ref4 = generateReference(queue, 4);
    ref4.getMessage().putStringProperty(new SimpleString("fruit"), new SimpleString("orange"));
    refs.add(ref4);
    queue.addTail(ref4);
    MessageReference ref5 = generateReference(queue, 5);
    ref5.getMessage().putStringProperty(new SimpleString("fruit"), new SimpleString("apple"));
    queue.addTail(ref5);
    MessageReference ref6 = generateReference(queue, 6);
    ref6.getMessage().putStringProperty(new SimpleString("fruit"), new SimpleString("orange"));
    refs.add(ref6);
    queue.addTail(ref6);
    if (!direct) {
        queue.addConsumer(consumer);
        queue.deliverNow();
    }
    Assert.assertEquals(6, getMessageCount(queue));
    awaitExecution();
    Assert.assertEquals(2, consumer.getReferences().size());
    Assert.assertEquals(2, queue.getDeliveringCount());
    assertRefListsIdenticalRefs(refs, consumer.getReferences());
    queue.acknowledge(ref5);
    queue.acknowledge(ref6);
    queue.removeConsumer(consumer);
    consumer = new FakeConsumer();
    queue.addConsumer(consumer);
    queue.deliverNow();
    Assert.assertEquals(4, getMessageCount(queue));
    Assert.assertEquals(4, consumer.getReferences().size());
    Assert.assertEquals(4, queue.getDeliveringCount());
}
Also used : FakeConsumer(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer) Filter(org.apache.activemq.artemis.core.filter.Filter) FakeFilter(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeFilter) ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) FakeFilter(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeFilter)

Example 19 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class QueueImplTest method testPauseAndResumeWithAsync.

/**
 * Test the paused and resumed states with async deliveries.
 *
 * @throws Exception
 */
@Test
public void testPauseAndResumeWithAsync() throws Exception {
    QueueImpl queue = getTemporaryQueue();
    // pauses the queue
    queue.pause();
    final int numMessages = 10;
    List<MessageReference> refs = new ArrayList<>();
    for (int i = 0; i < numMessages; i++) {
        MessageReference ref = generateReference(queue, i);
        refs.add(ref);
        queue.addTail(ref);
    }
    // even as this queue is paused, it will receive the messages anyway
    Assert.assertEquals(10, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(0, queue.getDeliveringCount());
    // Now add a consumer
    FakeConsumer consumer = new FakeConsumer();
    queue.addConsumer(consumer);
    Assert.assertTrue(consumer.getReferences().isEmpty());
    Assert.assertEquals(10, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    // explicit order of delivery
    queue.deliverNow();
    // As the queue is paused, even an explicit order of delivery will not work.
    Assert.assertEquals(0, consumer.getReferences().size());
    Assert.assertEquals(numMessages, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(0, queue.getDeliveringCount());
    // resuming work
    queue.resume();
    awaitExecution();
    // after resuming the delivery begins.
    assertRefListsIdenticalRefs(refs, consumer.getReferences());
    Assert.assertEquals(numMessages, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(numMessages, queue.getDeliveringCount());
}
Also used : FakeConsumer(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer) ArrayList(java.util.ArrayList) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) Test(org.junit.Test)

Example 20 with MessageReference

use of org.apache.activemq.artemis.core.server.MessageReference in project activemq-artemis by apache.

the class QueueImplTest method testConsumerWithFilterThenAddMoreMessages.

@Test
public void testConsumerWithFilterThenAddMoreMessages() throws Exception {
    QueueImpl queue = getTemporaryQueue();
    final int numMessages = 10;
    List<MessageReference> refs = new ArrayList<>();
    for (int i = 0; i < numMessages; i++) {
        MessageReference ref = generateReference(queue, i);
        ref.getMessage().putStringProperty("color", "red");
        refs.add(ref);
        queue.addTail(ref);
    }
    Assert.assertEquals(10, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(0, queue.getDeliveringCount());
    queue.deliverNow();
    Assert.assertEquals(10, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(0, queue.getDeliveringCount());
    for (int i = numMessages; i < numMessages * 2; i++) {
        MessageReference ref = generateReference(queue, i);
        refs.add(ref);
        ref.getMessage().putStringProperty("color", "green");
        queue.addTail(ref);
    }
    FakeConsumer consumer = new FakeConsumer(FilterImpl.createFilter("color = 'green'"));
    queue.addConsumer(consumer);
    queue.deliverNow();
    Assert.assertEquals(20, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(10, queue.getDeliveringCount());
    for (int i = numMessages * 2; i < numMessages * 3; i++) {
        MessageReference ref = generateReference(queue, i);
        refs.add(ref);
        ref.getMessage().putStringProperty("color", "green");
        queue.addTail(ref);
    }
    queue.deliverNow();
    Assert.assertEquals(20, consumer.getReferences().size());
    Assert.assertEquals(30, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(20, queue.getDeliveringCount());
}
Also used : FakeConsumer(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer) ArrayList(java.util.ArrayList) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) Test(org.junit.Test)

Aggregations

MessageReference (org.apache.activemq.artemis.core.server.MessageReference)82 ArrayList (java.util.ArrayList)29 QueueImpl (org.apache.activemq.artemis.core.server.impl.QueueImpl)26 Test (org.junit.Test)26 FakeConsumer (org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer)18 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)17 Transaction (org.apache.activemq.artemis.core.transaction.Transaction)14 Message (org.apache.activemq.artemis.api.core.Message)12 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)11 TransactionImpl (org.apache.activemq.artemis.core.transaction.impl.TransactionImpl)11 Queue (org.apache.activemq.artemis.core.server.Queue)10 HashMap (java.util.HashMap)9 NoSuchElementException (java.util.NoSuchElementException)9 Map (java.util.Map)8 Filter (org.apache.activemq.artemis.core.filter.Filter)7 LinkedList (java.util.LinkedList)5 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)5 ActiveMQIllegalStateException (org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException)4 BindingsTransactionImpl (org.apache.activemq.artemis.core.transaction.impl.BindingsTransactionImpl)4 HashSet (java.util.HashSet)3