Search in sources :

Example 6 with ClientConsumerInternal

use of org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal in project activemq-artemis by apache.

the class LargeMessageTest method testBufferMultipleLargeMessages.

@Test
public void testBufferMultipleLargeMessages() throws Exception {
    ClientSession session = null;
    ActiveMQServer server = null;
    final int SIZE = 10 * 1024;
    final int NUMBER_OF_MESSAGES = 30;
    try {
        server = createServer(true, isNetty(), storeType);
        server.start();
        locator.setMinLargeMessageSize(1024).setConsumerWindowSize(1024 * 1024);
        ClientSessionFactory sf = addSessionFactory(createSessionFactory(locator));
        session = sf.createSession(null, null, false, false, false, false, 0);
        session.createQueue(ADDRESS, ADDRESS, null, true);
        ClientProducer producer = session.createProducer(ADDRESS);
        for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
            ClientMessage clientFile = session.createMessage(true);
            clientFile.setBodyInputStream(ActiveMQTestBase.createFakeLargeStream(SIZE));
            producer.send(clientFile);
        }
        session.commit();
        producer.close();
        session.start();
        ClientConsumerInternal consumer = (ClientConsumerInternal) session.createConsumer(ADDRESS);
        // Wait the consumer to be complete with 10 messages before getting others
        long timeout = System.currentTimeMillis() + 10000;
        while (consumer.getBufferSize() < NUMBER_OF_MESSAGES && timeout > System.currentTimeMillis()) {
            Thread.sleep(10);
        }
        Assert.assertEquals(NUMBER_OF_MESSAGES, consumer.getBufferSize());
        // Reads the messages, rollback.. read them again
        for (int trans = 0; trans < 2; trans++) {
            for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
                ClientMessage msg = consumer.receive(10000);
                Assert.assertNotNull(msg);
                // it will ignore the buffer (not read it) on the first try
                if (trans == 0) {
                    for (int byteRead = 0; byteRead < SIZE; byteRead++) {
                        Assert.assertEquals(ActiveMQTestBase.getSamplebyte(byteRead), msg.getBodyBuffer().readByte());
                    }
                }
                msg.acknowledge();
            }
            if (trans == 0) {
                session.rollback();
            } else {
                session.commit();
            }
        }
        Assert.assertEquals(0, ((Queue) server.getPostOffice().getBinding(ADDRESS).getBindable()).getDeliveringCount());
        Assert.assertEquals(0, getMessageCount(((Queue) server.getPostOffice().getBinding(ADDRESS).getBindable())));
    } finally {
        try {
            session.close();
        } catch (Throwable ignored) {
        }
        try {
            server.stop();
        } catch (Throwable ignored) {
        }
    }
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ClientConsumerInternal(org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 7 with ClientConsumerInternal

use of org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal in project activemq-artemis by apache.

the class LargeMessageTest method testReceiveMultipleMessages.

@Test
public void testReceiveMultipleMessages() throws Exception {
    ClientSession session = null;
    ActiveMQServer server = null;
    final int SIZE = 10 * 1024;
    final int NUMBER_OF_MESSAGES = 1000;
    try {
        server = createServer(true, isNetty(), storeType);
        server.start();
        locator.setMinLargeMessageSize(1024).setConsumerWindowSize(1024 * 1024);
        ClientSessionFactory sf = addSessionFactory(createSessionFactory(locator));
        session = sf.createSession(null, null, false, false, false, false, 0);
        session.createQueue(ADDRESS, ADDRESS, null, true);
        ClientProducer producer = session.createProducer(ADDRESS);
        for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
            ClientMessage clientFile = session.createMessage(true);
            clientFile.setBodyInputStream(ActiveMQTestBase.createFakeLargeStream(SIZE));
            producer.send(clientFile);
        }
        session.commit();
        producer.close();
        session.start();
        // Reads the messages, rollback.. read them again
        for (int trans = 0; trans < 2; trans++) {
            ClientConsumerInternal consumer = (ClientConsumerInternal) session.createConsumer(ADDRESS);
            // Wait the consumer to be complete with 10 messages before getting others
            long timeout = System.currentTimeMillis() + 10000;
            while (consumer.getBufferSize() < 10 && timeout > System.currentTimeMillis()) {
                Thread.sleep(10);
            }
            for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
                ClientMessage msg = consumer.receive(10000);
                Assert.assertNotNull(msg);
                // it will ignore the buffer (not read it) on the first try
                if (trans == 0) {
                    for (int byteRead = 0; byteRead < SIZE; byteRead++) {
                        Assert.assertEquals(ActiveMQTestBase.getSamplebyte(byteRead), msg.getBodyBuffer().readByte());
                    }
                }
                msg.acknowledge();
            }
            if (trans == 0) {
                session.rollback();
            } else {
                session.commit();
            }
            consumer.close();
        }
        Assert.assertEquals(0, ((Queue) server.getPostOffice().getBinding(ADDRESS).getBindable()).getDeliveringCount());
        Assert.assertEquals(0, getMessageCount(((Queue) server.getPostOffice().getBinding(ADDRESS).getBindable())));
    } finally {
        try {
            session.close();
        } catch (Throwable ignored) {
        }
        try {
            server.stop();
        } catch (Throwable ignored) {
        }
    }
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ClientConsumerInternal(org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 8 with ClientConsumerInternal

use of org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal in project activemq-artemis by apache.

the class ConsumerWindowSizeTest method testSaveBuffersOnLargeMessage.

@Test
public void testSaveBuffersOnLargeMessage() throws Exception {
    ActiveMQServer server = createServer(false, isNetty());
    ClientSession session1 = null;
    try {
        final int numberOfMessages = 10;
        server.start();
        locator.setConsumerWindowSize(0).setMinLargeMessageSize(100);
        ClientSessionFactory sf = createSessionFactory(locator);
        session1 = sf.createSession(false, true, true);
        session1.start();
        SimpleString ADDRESS = new SimpleString("some-queue");
        session1.createQueue(ADDRESS, ADDRESS, true);
        ClientConsumerInternal cons1 = (ClientConsumerInternal) session1.createConsumer(ADDRESS);
        // Note we make sure we send the messages *before* cons2 is created
        ClientProducer prod = session1.createProducer(ADDRESS);
        for (int i = 0; i < numberOfMessages; i++) {
            ClientMessage msg = session1.createMessage(true);
            msg.getBodyBuffer().writeBytes(new byte[600]);
            prod.send(msg);
        }
        for (int i = 0; i < numberOfMessages; i++) {
            ClientMessage msg = cons1.receive(1000);
            Assert.assertNotNull("expected message at i = " + i, msg);
            msg.saveToOutputStream(new FakeOutputStream());
            msg.acknowledge();
            Assert.assertEquals("A slow consumer shouldn't buffer anything on the client side!", 0, cons1.getBufferSize());
        }
        // just to make sure everything is flushed and no pending packets on the sending buffer, or
        session1.close();
        session1.close();
        session1 = null;
        Assert.assertEquals(0, getMessageCount(server, ADDRESS.toString()));
    } finally {
        try {
            if (session1 != null) {
                session1.close();
            }
        } catch (Exception ignored) {
        }
    }
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ClientConsumerInternal(org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) IOException(java.io.IOException) Test(org.junit.Test)

Example 9 with ClientConsumerInternal

use of org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal in project activemq-artemis by apache.

the class PagingTest method testDeleteQueueRestart.

@Test
public void testDeleteQueueRestart() throws Exception {
    clearDataRecreateServerDirs();
    // disable compact
    Configuration config = createDefaultInVMConfig().setJournalDirectory(getJournalDir()).setJournalSyncNonTransactional(false).setJournalCompactMinFiles(0);
    ActiveMQServer server = createServer(true, config, PagingTest.PAGE_SIZE, PagingTest.PAGE_MAX);
    server.start();
    final int numberOfMessages = 5000;
    locator = createInVMNonHALocator().setConsumerWindowSize(10 * 1024 * 1024).setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true);
    SimpleString QUEUE2 = ADDRESS.concat("-2");
    ClientSessionFactory sf = locator.createSessionFactory();
    ClientSession session = sf.createSession(false, false, false);
    session.createQueue(PagingTest.ADDRESS, PagingTest.ADDRESS, null, true);
    session.createQueue(PagingTest.ADDRESS, QUEUE2, null, true);
    ClientProducer producer = session.createProducer(PagingTest.ADDRESS);
    // This is just to hold some messages as being delivered
    ClientConsumerInternal cons = (ClientConsumerInternal) session.createConsumer(ADDRESS);
    ClientConsumerInternal cons2 = (ClientConsumerInternal) session.createConsumer(QUEUE2);
    ClientMessage message = null;
    byte[] body = new byte[MESSAGE_SIZE];
    ByteBuffer bb = ByteBuffer.wrap(body);
    for (int j = 1; j <= MESSAGE_SIZE; j++) {
        bb.put(getSamplebyte(j));
    }
    for (int i = 0; i < numberOfMessages; i++) {
        message = session.createMessage(true);
        ActiveMQBuffer bodyLocal = message.getBodyBuffer();
        bodyLocal.writeBytes(body);
        producer.send(message);
        if (i % 1000 == 0) {
            session.commit();
        }
    }
    session.commit();
    producer.close();
    session.start();
    long timeout = System.currentTimeMillis() + 30000;
    // I want the buffer full to make sure there are pending messages on the server's side
    while (System.currentTimeMillis() < timeout && (cons.getBufferSize() < 1000 || cons2.getBufferSize() < 1000)) {
        System.out.println("cons1 buffer = " + cons.getBufferSize() + ", cons2 buffer = " + cons2.getBufferSize());
        Thread.sleep(100);
    }
    assertTrue(cons.getBufferSize() >= 1000);
    assertTrue(cons2.getBufferSize() >= 1000);
    session.close();
    Queue queue = server.locateQueue(QUEUE2);
    long deletedQueueID = queue.getID();
    server.destroyQueue(QUEUE2);
    sf.close();
    locator.close();
    locator = null;
    sf = null;
    server.stop();
    final HashMap<Integer, AtomicInteger> recordsType = countJournal(config);
    for (Map.Entry<Integer, AtomicInteger> entry : recordsType.entrySet()) {
        System.out.println(entry.getKey() + "=" + entry.getValue());
    }
    assertNull("The system is acking page records instead of just delete data", recordsType.get(new Integer(JournalRecordIds.ACKNOWLEDGE_CURSOR)));
    Pair<List<RecordInfo>, List<PreparedTransactionInfo>> journalData = loadMessageJournal(config);
    HashSet<Long> deletedQueueReferences = new HashSet<>();
    for (RecordInfo info : journalData.getA()) {
        if (info.getUserRecordType() == JournalRecordIds.ADD_REF) {
            DescribeJournal.ReferenceDescribe ref = (ReferenceDescribe) DescribeJournal.newObjectEncoding(info);
            if (ref.refEncoding.queueID == deletedQueueID) {
                deletedQueueReferences.add(new Long(info.id));
            }
        } else if (info.getUserRecordType() == JournalRecordIds.ACKNOWLEDGE_REF) {
            AckDescribe ref = (AckDescribe) DescribeJournal.newObjectEncoding(info);
            if (ref.refEncoding.queueID == deletedQueueID) {
                deletedQueueReferences.remove(new Long(info.id));
            }
        }
    }
    if (!deletedQueueReferences.isEmpty()) {
        for (Long value : deletedQueueReferences) {
            System.out.println("Deleted Queue still has a reference:" + value);
        }
        fail("Deleted queue still have references");
    }
    server.start();
    locator = createInVMNonHALocator();
    locator.setConsumerWindowSize(10 * 1024 * 1024);
    sf = locator.createSessionFactory();
    session = sf.createSession(false, false, false);
    cons = (ClientConsumerInternal) session.createConsumer(ADDRESS);
    session.start();
    for (int i = 0; i < numberOfMessages; i++) {
        message = cons.receive(5000);
        assertNotNull(message);
        message.acknowledge();
        if (i % 1000 == 0) {
            session.commit();
        }
    }
    session.commit();
    producer.close();
    session.close();
    queue = server.locateQueue(PagingTest.ADDRESS);
    assertEquals(0, getMessageCount(queue));
    timeout = System.currentTimeMillis() + 10000;
    while (timeout > System.currentTimeMillis() && queue.getPageSubscription().getPagingStore().isPaging()) {
        Thread.sleep(100);
    }
    assertFalse(queue.getPageSubscription().getPagingStore().isPaging());
    server.stop();
}
Also used : ClientConsumerInternal(org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal) 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) AckDescribe(org.apache.activemq.artemis.core.persistence.impl.journal.AckDescribe) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) 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) ReferenceDescribe(org.apache.activemq.artemis.core.persistence.impl.journal.DescribeJournal.ReferenceDescribe) ActiveMQBuffer(org.apache.activemq.artemis.api.core.ActiveMQBuffer) HashSet(java.util.HashSet) RecordInfo(org.apache.activemq.artemis.core.journal.RecordInfo) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ByteBuffer(java.nio.ByteBuffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ReferenceDescribe(org.apache.activemq.artemis.core.persistence.impl.journal.DescribeJournal.ReferenceDescribe) DescribeJournal(org.apache.activemq.artemis.core.persistence.impl.journal.DescribeJournal) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 10 with ClientConsumerInternal

use of org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal in project activemq-artemis by apache.

the class ConsumerWindowSizeTest method internalTestSlowConsumerOnMessageHandlerBufferOne.

private void internalTestSlowConsumerOnMessageHandlerBufferOne(final boolean largeMessage) throws Exception {
    ActiveMQServer server = createServer(false, isNetty());
    ClientSession sessionB = null;
    ClientSession session = null;
    try {
        final int numberOfMessages = 100;
        server.start();
        locator.setConsumerWindowSize(1);
        if (largeMessage) {
            locator.setMinLargeMessageSize(100);
        }
        ClientSessionFactory sf = createSessionFactory(locator);
        session = sf.createSession(false, true, true);
        SimpleString ADDRESS = new SimpleString("some-queue");
        session.createQueue(ADDRESS, ADDRESS, true);
        sessionB = sf.createSession(false, true, true);
        sessionB.start();
        session.start();
        ClientConsumerInternal consReceiveOneAndHold = (ClientConsumerInternal) sessionB.createConsumer(ADDRESS);
        final CountDownLatch latchReceived = new CountDownLatch(2);
        final CountDownLatch latchReceivedBuffered = new CountDownLatch(3);
        final CountDownLatch latchDone = new CountDownLatch(1);
        // It should receive two messages and then give up
        class LocalHandler implements MessageHandler {

            boolean failed = false;

            int count = 0;

            /* (non-Javadoc)
             * @see MessageHandler#onMessage(ClientMessage)
             */
            @Override
            public synchronized void onMessage(final ClientMessage message) {
                try {
                    log.info("received msg " + message);
                    String str = getTextMessage(message);
                    if (ConsumerWindowSizeTest.isTrace) {
                        ConsumerWindowSizeTest.log.trace("Received message " + str);
                    }
                    ConsumerWindowSizeTest.log.info("Received message " + str);
                    failed = failed || !str.equals("Msg" + count);
                    message.acknowledge();
                    latchReceived.countDown();
                    latchReceivedBuffered.countDown();
                    if (count++ == 1) {
                        // it will hold here for a while
                        if (!latchDone.await(TIMEOUT, TimeUnit.SECONDS)) {
                            new Exception("ClientConsuemrWindowSizeTest Handler couldn't receive signal in less than 5 seconds").printStackTrace();
                            failed = true;
                        }
                    }
                } catch (Exception e) {
                    // Hudson / JUnit report
                    e.printStackTrace();
                    failed = true;
                }
            }
        }
        LocalHandler handler = new LocalHandler();
        ClientProducer prod = session.createProducer(ADDRESS);
        for (int i = 0; i < numberOfMessages; i++) {
            ClientMessage msg = createTextMessage(session, "Msg" + i);
            if (largeMessage) {
                msg.getBodyBuffer().writeBytes(new byte[600]);
            }
            prod.send(msg);
        }
        consReceiveOneAndHold.setMessageHandler(handler);
        Assert.assertTrue(latchReceived.await(TIMEOUT, TimeUnit.SECONDS));
        log.info("bs " + consReceiveOneAndHold.getBufferSize());
        long timeout = System.currentTimeMillis() + 1000 * TIMEOUT;
        while (consReceiveOneAndHold.getBufferSize() == 0 && System.currentTimeMillis() < timeout) {
            log.info("bs " + consReceiveOneAndHold.getBufferSize());
            Thread.sleep(10);
        }
        Assert.assertEquals(1, consReceiveOneAndHold.getBufferSize());
        ClientConsumer cons1 = session.createConsumer(ADDRESS);
        for (int i = 3; i < numberOfMessages; i++) {
            ClientMessage msg = cons1.receive(1000);
            Assert.assertNotNull("expected message at i = " + i, msg);
            String text = getTextMessage(msg);
            Assert.assertEquals("Msg" + i, text);
            msg.acknowledge();
        }
        latchDone.countDown();
        Assert.assertTrue(latchReceivedBuffered.await(TIMEOUT, TimeUnit.SECONDS));
        session.close();
        session = null;
        sessionB.close();
        sessionB = null;
        Assert.assertEquals(0, getMessageCount(server, ADDRESS.toString()));
        Assert.assertFalse("MessageHandler received a failure", handler.failed);
    } finally {
        try {
            if (session != null) {
                session.close();
            }
            if (sessionB != null) {
                sessionB.close();
            }
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }
    }
}
Also used : ClientConsumerInternal(org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal) MessageHandler(org.apache.activemq.artemis.api.core.client.MessageHandler) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) 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)

Aggregations

ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)12 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)12 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)12 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)12 ClientConsumerInternal (org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal)12 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)10 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)9 IOException (java.io.IOException)7 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)6 Test (org.junit.Test)5 ByteBuffer (java.nio.ByteBuffer)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)2 MessageHandler (org.apache.activemq.artemis.api.core.client.MessageHandler)2 Configuration (org.apache.activemq.artemis.core.config.Configuration)2 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)2 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)2 DatabaseStorageConfiguration (org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration)2 Queue (org.apache.activemq.artemis.core.server.Queue)2 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)2