Search in sources :

Example 46 with MessageReference

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

the class QueueImplTest method testRoundRobinWithQueueing.

@Test
public void testRoundRobinWithQueueing() throws Exception {
    QueueImpl queue = getTemporaryQueue();
    final int numMessages = 10;
    List<MessageReference> refs = new ArrayList<>();
    queue.pause();
    for (int i = 0; i < numMessages; i++) {
        MessageReference ref = generateReference(queue, i);
        refs.add(ref);
        queue.addTail(ref);
    }
    FakeConsumer cons1 = new FakeConsumer();
    FakeConsumer cons2 = new FakeConsumer();
    queue.addConsumer(cons1);
    queue.addConsumer(cons2);
    queue.resume();
    // Need to make sure the consumers will receive the messages before we do these assertions
    long timeout = System.currentTimeMillis() + 5000;
    while (cons1.getReferences().size() != numMessages / 2 && timeout > System.currentTimeMillis()) {
        Thread.sleep(1);
    }
    while (cons2.getReferences().size() != numMessages / 2 && timeout > System.currentTimeMillis()) {
        Thread.sleep(1);
    }
    Assert.assertEquals(numMessages / 2, cons1.getReferences().size());
    Assert.assertEquals(numMessages / 2, cons2.getReferences().size());
    for (int i = 0; i < numMessages; i++) {
        MessageReference ref;
        ref = i % 2 == 0 ? cons1.getReferences().get(i / 2) : cons2.getReferences().get(i / 2);
        Assert.assertEquals(refs.get(i), ref);
    }
}
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 47 with MessageReference

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

the class QueueImplTest method testaddHeadadd.

@Test
public void testaddHeadadd() throws Exception {
    QueueImpl queue = getTemporaryQueue();
    final int numMessages = 10;
    List<MessageReference> refs1 = new ArrayList<>();
    for (int i = 0; i < numMessages; i++) {
        MessageReference ref = generateReference(queue, i);
        refs1.add(ref);
        queue.addTail(ref);
    }
    LinkedList<MessageReference> refs2 = new LinkedList<>();
    for (int i = 0; i < numMessages; i++) {
        MessageReference ref = generateReference(queue, i + numMessages);
        refs2.addFirst(ref);
        queue.addHead(ref, false);
    }
    List<MessageReference> refs3 = new ArrayList<>();
    for (int i = 0; i < numMessages; i++) {
        MessageReference ref = generateReference(queue, i + 2 * numMessages);
        refs3.add(ref);
        queue.addTail(ref);
    }
    FakeConsumer consumer = new FakeConsumer();
    queue.addConsumer(consumer);
    queue.deliverNow();
    List<MessageReference> allRefs = new ArrayList<>();
    allRefs.addAll(refs2);
    allRefs.addAll(refs1);
    allRefs.addAll(refs3);
    assertRefListsIdenticalRefs(allRefs, consumer.getReferences());
}
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) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 48 with MessageReference

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

the class QueueImplTest method testIterator.

@Test
public void testIterator() {
    QueueImpl queue = getTemporaryQueue();
    final int numMessages = 20;
    List<MessageReference> refs = new ArrayList<>();
    for (int i = 0; i < numMessages; i++) {
        MessageReference ref = generateReference(queue, i);
        queue.addTail(ref);
        refs.add(ref);
    }
    Assert.assertEquals(numMessages, getMessageCount(queue));
    Iterator<MessageReference> iterator = queue.iterator();
    List<MessageReference> list = new ArrayList<>();
    while (iterator.hasNext()) {
        list.add(iterator.next());
    }
    assertRefListsIdenticalRefs(refs, list);
}
Also used : 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 49 with MessageReference

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

the class QueueImplTest method testBusyConsumerWithFilterFirstCallBusy.

@Test
public void testBusyConsumerWithFilterFirstCallBusy() 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", "green");
        refs.add(ref);
        queue.addTail(ref);
    }
    Assert.assertEquals(10, getMessageCount(queue));
    Assert.assertEquals(0, queue.getScheduledCount());
    Assert.assertEquals(0, queue.getDeliveringCount());
    queue.deliverNow();
    consumer.setStatusImmediate(null);
    queue.deliverNow();
    List<MessageReference> receeivedRefs = consumer.getReferences();
    int currId = 0;
    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 50 with MessageReference

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

the class QueueImplTest method testTotalIteratorOrder.

@Test
public void testTotalIteratorOrder() throws Exception {
    final String MY_ADDRESS = "myAddress";
    final String MY_QUEUE = "myQueue";
    ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(createDefaultInVMConfig(), true));
    AddressSettings defaultSetting = new AddressSettings().setPageSizeBytes(10 * 1024).setMaxSizeBytes(20 * 1024);
    server.getAddressSettingsRepository().addMatch("#", defaultSetting);
    server.start();
    ServerLocator locator = createInVMNonHALocator().setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true);
    ClientSessionFactory factory = createSessionFactory(locator);
    ClientSession session = addClientSession(factory.createSession(false, true, true));
    session.createQueue(MY_ADDRESS, MY_QUEUE, true);
    ClientProducer producer = addClientProducer(session.createProducer(MY_ADDRESS));
    for (int i = 0; i < 50; i++) {
        ClientMessage message = session.createMessage(true);
        message.getBodyBuffer().writeBytes(new byte[1024]);
        message.putIntProperty("order", i);
        producer.send(message);
    }
    producer.close();
    session.close();
    factory.close();
    locator.close();
    Queue queue = ((LocalQueueBinding) server.getPostOffice().getBinding(new SimpleString(MY_QUEUE))).getQueue();
    LinkedListIterator<MessageReference> totalIterator = queue.browserIterator();
    try {
        int i = 0;
        while (totalIterator.hasNext()) {
            MessageReference ref = totalIterator.next();
            Assert.assertEquals(i++, ref.getMessage().getIntProperty("order").intValue());
        }
    } finally {
        totalIterator.close();
        server.stop();
    }
}
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) MessageReference(org.apache.activemq.artemis.core.server.MessageReference) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) LocalQueueBinding(org.apache.activemq.artemis.core.postoffice.impl.LocalQueueBinding) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Queue(org.apache.activemq.artemis.core.server.Queue) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) 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