Search in sources :

Example 21 with QueueImpl

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

the class PagingTest method testPurge.

@Test
public void testPurge() throws Exception {
    clearDataRecreateServerDirs();
    Configuration config = createDefaultNettyConfig().setJournalSyncNonTransactional(false);
    server = createServer(true, config, PagingTest.PAGE_SIZE, PagingTest.PAGE_MAX);
    server.start();
    String queue = "purgeQueue";
    SimpleString ssQueue = new SimpleString(queue);
    server.addAddressInfo(new AddressInfo(ssQueue, RoutingType.ANYCAST));
    QueueImpl purgeQueue = (QueueImpl) server.createQueue(ssQueue, RoutingType.ANYCAST, ssQueue, null, true, false, 1, true, false);
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
    Connection connection = cf.createConnection();
    Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    javax.jms.Queue jmsQueue = session.createQueue(queue);
    MessageProducer producer = session.createProducer(jmsQueue);
    for (int i = 0; i < 100; i++) {
        producer.send(session.createTextMessage("hello" + i));
    }
    session.commit();
    Wait.assertEquals(0, purgeQueue::getMessageCount);
    Assert.assertEquals(0, purgeQueue.getPageSubscription().getPagingStore().getAddressSize());
    MessageConsumer consumer = session.createConsumer(jmsQueue);
    for (int i = 0; i < 100; i++) {
        producer.send(session.createTextMessage("hello" + i));
        if (i == 10) {
            purgeQueue.getPageSubscription().getPagingStore().startPaging();
        }
    }
    session.commit();
    consumer.close();
    Wait.assertEquals(0, purgeQueue::getMessageCount);
    Wait.assertFalse(purgeQueue.getPageSubscription()::isPaging);
    Wait.assertEquals(0, purgeQueue.getPageSubscription().getPagingStore()::getAddressSize);
    purgeQueue.getPageSubscription().getPagingStore().startPaging();
    Wait.assertTrue(purgeQueue.getPageSubscription()::isPaging);
    consumer = session.createConsumer(jmsQueue);
    for (int i = 0; i < 100; i++) {
        purgeQueue.getPageSubscription().getPagingStore().startPaging();
        Assert.assertTrue(purgeQueue.getPageSubscription().isPaging());
        producer.send(session.createTextMessage("hello" + i));
        if (i % 2 == 0) {
            session.commit();
        }
    }
    session.commit();
    connection.start();
    server.getStorageManager().getMessageJournal().scheduleCompactAndBlock(50000);
    Assert.assertNotNull(consumer.receive(5000));
    session.commit();
    consumer.close();
    Wait.assertEquals(0, purgeQueue::getMessageCount);
    Wait.assertEquals(0, purgeQueue.getPageSubscription().getPagingStore()::getAddressSize);
    Wait.assertFalse(purgeQueue.getPageSubscription()::isPaging);
    StorageManager sm = server.getStorageManager();
    for (int i = 0; i < 1000; i++) {
        long tx = sm.generateID();
        PageTransactionInfoImpl txinfo = new PageTransactionInfoImpl(tx);
        sm.storePageTransaction(tx, txinfo);
        sm.commit(tx);
        tx = sm.generateID();
        sm.updatePageTransaction(tx, txinfo, 1);
        sm.commit(tx);
    }
    server.stop();
    server.start();
    Assert.assertEquals(0, server.getPagingManager().getTransactions().size());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) DivertConfiguration(org.apache.activemq.artemis.core.config.DivertConfiguration) StoreConfiguration(org.apache.activemq.artemis.core.config.StoreConfiguration) Configuration(org.apache.activemq.artemis.core.config.Configuration) DatabaseStorageConfiguration(org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration) PageTransactionInfoImpl(org.apache.activemq.artemis.core.paging.impl.PageTransactionInfoImpl) Connection(javax.jms.Connection) StorageManager(org.apache.activemq.artemis.core.persistence.StorageManager) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Test(org.junit.Test)

Example 22 with QueueImpl

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

the class PagingOrderTest method testOrderOverRollback2.

@Test
public void testOrderOverRollback2() throws Throwable {
    boolean persistentMessages = true;
    Configuration config = createDefaultInVMConfig().setJournalSyncNonTransactional(false);
    ActiveMQServer server = createServer(true, config, PAGE_SIZE, PAGE_MAX, new HashMap<String, AddressSettings>());
    server.start();
    final int messageSize = 1024;
    final int numberOfMessages = 200;
    ServerLocator locator = createInVMNonHALocator().setClientFailureCheckPeriod(1000).setConnectionTTL(2000).setReconnectAttempts(0).setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true).setConsumerWindowSize(0);
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(false, false, false);
    server.addAddressInfo(new AddressInfo(ADDRESS, RoutingType.ANYCAST));
    QueueImpl queue = (QueueImpl) server.createQueue(ADDRESS, RoutingType.ANYCAST, ADDRESS, null, true, false);
    ClientProducer producer = session.createProducer(PagingTest.ADDRESS);
    byte[] body = new byte[messageSize];
    ByteBuffer bb = ByteBuffer.wrap(body);
    for (int j = 1; j <= messageSize; j++) {
        bb.put(getSamplebyte(j));
    }
    for (int i = 0; i < numberOfMessages; i++) {
        ClientMessage message = session.createMessage(persistentMessages);
        ActiveMQBuffer bodyLocal = message.getBodyBuffer();
        bodyLocal.writeBytes(body);
        message.putIntProperty(new SimpleString("id"), i);
        producer.send(message);
        if (i % 1000 == 0) {
            session.commit();
        }
    }
    session.commit();
    session.close();
    session = sf.createSession(false, false, 0);
    session.start();
    ClientConsumer consumer = session.createConsumer(ADDRESS);
    // number of references without paging
    int numberOfRefs = queue.getNumberOfReferences();
    // consume all non-paged references
    for (int ref = 0; ref < numberOfRefs; ref++) {
        ClientMessage msg = consumer.receive(5000);
        assertNotNull(msg);
        msg.acknowledge();
    }
    session.commit();
    session.close();
    session = sf.createSession(false, false, 0);
    session.start();
    consumer = session.createConsumer(ADDRESS);
    ClientMessage msg = consumer.receive(5000);
    assertNotNull(msg);
    int msgIDRolledBack = msg.getIntProperty("id").intValue();
    msg.acknowledge();
    session.rollback();
    msg = consumer.receive(5000);
    assertNotNull(msg);
    assertEquals(msgIDRolledBack, msg.getIntProperty("id").intValue());
    session.rollback();
    session.close();
    sf.close();
    locator.close();
    server.stop();
    server.start();
    locator = createInVMNonHALocator().setClientFailureCheckPeriod(1000).setConnectionTTL(2000).setReconnectAttempts(0).setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true).setConsumerWindowSize(0);
    sf = createSessionFactory(locator);
    session = sf.createSession(false, false, 0);
    session.start();
    consumer = session.createConsumer(ADDRESS);
    for (int i = msgIDRolledBack; i < numberOfMessages; i++) {
        ClientMessage message = consumer.receive(5000);
        assertNotNull(message);
        assertEquals(i, message.getIntProperty("id").intValue());
        message.acknowledge();
    }
    session.commit();
    session.close();
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) Configuration(org.apache.activemq.artemis.core.config.Configuration) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) 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) ByteBuffer(java.nio.ByteBuffer) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) 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) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) Test(org.junit.Test)

Example 23 with QueueImpl

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

the class QueueImplTest method testAddRemoveConsumer.

@Test
public void testAddRemoveConsumer() throws Exception {
    Consumer cons1 = new FakeConsumer();
    Consumer cons2 = new FakeConsumer();
    Consumer cons3 = new FakeConsumer();
    QueueImpl queue = getTemporaryQueue();
    Assert.assertEquals(0, queue.getConsumerCount());
    queue.addConsumer(cons1);
    Assert.assertEquals(1, queue.getConsumerCount());
    queue.removeConsumer(cons1);
    Assert.assertEquals(0, queue.getConsumerCount());
    queue.addConsumer(cons1);
    queue.addConsumer(cons2);
    queue.addConsumer(cons3);
    Assert.assertEquals(3, queue.getConsumerCount());
    queue.removeConsumer(new FakeConsumer());
    Assert.assertEquals(3, queue.getConsumerCount());
    queue.removeConsumer(cons1);
    Assert.assertEquals(2, queue.getConsumerCount());
    queue.removeConsumer(cons2);
    Assert.assertEquals(1, queue.getConsumerCount());
    queue.removeConsumer(cons3);
    Assert.assertEquals(0, queue.getConsumerCount());
    queue.removeConsumer(cons3);
}
Also used : FakeConsumer(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer) Consumer(org.apache.activemq.artemis.core.server.Consumer) FakeConsumer(org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) Test(org.junit.Test)

Example 24 with QueueImpl

use of org.apache.activemq.artemis.core.server.impl.QueueImpl 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 25 with QueueImpl

use of org.apache.activemq.artemis.core.server.impl.QueueImpl 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)

Aggregations

QueueImpl (org.apache.activemq.artemis.core.server.impl.QueueImpl)48 Test (org.junit.Test)41 MessageReference (org.apache.activemq.artemis.core.server.MessageReference)26 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)21 FakeConsumer (org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeConsumer)21 ArrayList (java.util.ArrayList)17 MessageConsumer (javax.jms.MessageConsumer)6 MessageProducer (javax.jms.MessageProducer)6 Session (javax.jms.Session)6 Filter (org.apache.activemq.artemis.core.filter.Filter)6 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)5 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)5 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)5 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)4 StorageManager (org.apache.activemq.artemis.core.persistence.StorageManager)4 AddressInfo (org.apache.activemq.artemis.core.server.impl.AddressInfo)4 FakeFilter (org.apache.activemq.artemis.tests.unit.core.server.impl.fakes.FakeFilter)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 Connection (javax.jms.Connection)3 Queue (javax.jms.Queue)3