Search in sources :

Example 91 with ClientMessage

use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.

the class LargeMessageTest method internalTestRollbackPartiallyConsumedBuffer.

private void internalTestRollbackPartiallyConsumedBuffer(final boolean redeliveryDelay) throws Exception {
    final int messageSize = largeMessageSize;
    final ClientSession session;
    ActiveMQServer server = createServer(true, isNetty(), storeType);
    AddressSettings settings = new AddressSettings();
    if (redeliveryDelay) {
        settings.setRedeliveryDelay(100);
        if (locator.isCompressLargeMessage()) {
            locator.setConsumerWindowSize(0);
        }
    }
    settings.setMaxDeliveryAttempts(-1);
    server.getAddressSettingsRepository().addMatch("#", settings);
    server.start();
    ClientSessionFactory sf = addSessionFactory(createSessionFactory(locator));
    session = sf.createSession(false, false, false);
    session.createQueue(ADDRESS, ADDRESS, true);
    ClientProducer producer = session.createProducer(ADDRESS);
    for (int i = 0; i < 20; i++) {
        Message clientFile = createLargeClientMessageStreaming(session, messageSize, true);
        clientFile.putIntProperty("value", i);
        producer.send(clientFile);
    }
    session.commit();
    session.start();
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger errors = new AtomicInteger(0);
    ClientConsumer consumer = session.createConsumer(ADDRESS);
    consumer.setMessageHandler(new MessageHandler() {

        int counter = 0;

        @Override
        public void onMessage(ClientMessage message) {
            message.getBodyBuffer().readByte();
            // System.out.println("message:" + message);
            try {
                if (counter++ < 20) {
                    Thread.sleep(100);
                    // System.out.println("Rollback");
                    message.acknowledge();
                    session.rollback();
                } else {
                    message.acknowledge();
                    session.commit();
                }
                if (counter == 40) {
                    latch.countDown();
                }
            } catch (Exception e) {
                latch.countDown();
                e.printStackTrace();
                errors.incrementAndGet();
            }
        }
    });
    assertTrue(latch.await(40, TimeUnit.SECONDS));
    consumer.close();
    session.close();
    validateNoFilesOnLargeDir();
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(org.apache.activemq.artemis.api.core.Message) MessageHandler(org.apache.activemq.artemis.api.core.client.MessageHandler) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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)

Example 92 with ClientMessage

use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.

the class LargeMessageTest method testPageOnLargeMessage.

protected void testPageOnLargeMessage(final boolean realFiles, final boolean sendBlocking) throws Exception {
    Configuration config = createDefaultConfig(isNetty());
    final int PAGE_MAX = 20 * 1024;
    final int PAGE_SIZE = 10 * 1024;
    HashMap<String, AddressSettings> map = new HashMap<>();
    AddressSettings value = new AddressSettings();
    map.put(ADDRESS.toString(), value);
    ActiveMQServer server = createServer(realFiles, config, PAGE_SIZE, PAGE_MAX, map, storeType);
    server.start();
    final int numberOfBytes = 1024;
    final int numberOfBytesBigMessage = 400000;
    ClientSessionFactory sf = addSessionFactory(createSessionFactory(locator));
    if (sendBlocking) {
        sf.getServerLocator().setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setBlockOnAcknowledge(true);
    }
    ClientSession session = sf.createSession(null, null, false, true, true, false, 0);
    session.createQueue(ADDRESS, ADDRESS, null, true);
    ClientProducer producer = session.createProducer(ADDRESS);
    ClientMessage message = null;
    for (int i = 0; i < 100; i++) {
        message = session.createMessage(true);
        // TODO: Why do I need to reset the writerIndex?
        message.getBodyBuffer().writerIndex(0);
        for (int j = 1; j <= numberOfBytes; j++) {
            message.getBodyBuffer().writeInt(j);
        }
        producer.send(message);
    }
    ClientMessage clientFile = createLargeClientMessageStreaming(session, numberOfBytesBigMessage);
    producer.send(clientFile);
    session.close();
    if (realFiles) {
        server.stop();
        server = createServer(true, config, PAGE_SIZE, PAGE_MAX, map, storeType);
        server.start();
        sf = createSessionFactory(locator);
    }
    session = sf.createSession(null, null, false, true, true, false, 0);
    ClientConsumer consumer = session.createConsumer(ADDRESS);
    session.start();
    for (int i = 0; i < 100; i++) {
        ClientMessage message2 = consumer.receive(LargeMessageTest.RECEIVE_WAIT_TIME);
        Assert.assertNotNull(message2);
        message2.acknowledge();
        Assert.assertNotNull(message2);
        message.getBodyBuffer().readerIndex(0);
        for (int j = 1; j <= numberOfBytes; j++) {
            Assert.assertEquals(j, message.getBodyBuffer().readInt());
        }
    }
    consumer.close();
    session.close();
    session = sf.createSession(null, null, false, true, true, false, 0);
    readMessage(session, ADDRESS, numberOfBytesBigMessage);
    // printBuffer("message received : ", message2.getBody());
    session.close();
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) Configuration(org.apache.activemq.artemis.core.config.Configuration) StoreConfiguration(org.apache.activemq.artemis.core.config.StoreConfiguration) HashMap(java.util.HashMap) 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) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer)

Example 93 with ClientMessage

use of org.apache.activemq.artemis.api.core.client.ClientMessage 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 94 with ClientMessage

use of org.apache.activemq.artemis.api.core.client.ClientMessage in project activemq-artemis by apache.

the class LargeMessageTest method testTwoBindingsTwoStartedConsumers.

@Test
public void testTwoBindingsTwoStartedConsumers() throws Exception {
    // there are two bindings.. one is ACKed, the other is not, the server is restarted
    // The other binding is acked... The file must be deleted
    ActiveMQServer server = createServer(true, isNetty(), storeType);
    server.start();
    SimpleString[] queue = new SimpleString[] { new SimpleString("queue1"), new SimpleString("queue2") };
    ClientSessionFactory sf = addSessionFactory(createSessionFactory(locator));
    ClientSession session = sf.createSession(null, null, false, true, true, false, 0);
    session.createQueue(ADDRESS, queue[0], null, true);
    session.createQueue(ADDRESS, queue[1], null, true);
    int numberOfBytes = 400000;
    Message clientFile = createLargeClientMessageStreaming(session, numberOfBytes);
    ClientProducer producer = session.createProducer(ADDRESS);
    session.start();
    producer.send(clientFile);
    producer.close();
    ClientConsumer consumer = session.createConsumer(queue[1]);
    ClientMessage msg = consumer.receive(LargeMessageTest.RECEIVE_WAIT_TIME);
    msg.getBodyBuffer().readByte();
    Assert.assertNull(consumer.receiveImmediate());
    Assert.assertNotNull(msg);
    msg.acknowledge();
    consumer.close();
    log.debug("Stopping");
    session.stop();
    ClientConsumer consumer1 = session.createConsumer(queue[0]);
    session.start();
    msg = consumer1.receive(LargeMessageTest.RECEIVE_WAIT_TIME);
    Assert.assertNotNull(msg);
    msg.acknowledge();
    consumer1.close();
    session.commit();
    session.close();
    validateNoFilesOnLargeDir();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Message(org.apache.activemq.artemis.api.core.Message) 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) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 95 with ClientMessage

use of org.apache.activemq.artemis.api.core.client.ClientMessage 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)

Aggregations

ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)889 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)708 Test (org.junit.Test)647 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)623 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)606 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)445 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)363 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)167 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)140 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)102 CountDownLatch (java.util.concurrent.CountDownLatch)99 Configuration (org.apache.activemq.artemis.core.config.Configuration)87 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)85 Queue (org.apache.activemq.artemis.core.server.Queue)72 Xid (javax.transaction.xa.Xid)67 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)63 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)54 HashMap (java.util.HashMap)53 ArrayList (java.util.ArrayList)51 DatabaseStorageConfiguration (org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration)47