Search in sources :

Example 91 with ClientProducer

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

the class ProducerFlowControlTest method testFlowControlMessageNotRouted.

@Test
public void testFlowControlMessageNotRouted() throws Exception {
    final SimpleString address = new SimpleString("testaddress");
    server = createServer(false, isNetty());
    AddressSettings addressSettings = new AddressSettings().setMaxSizeBytes(1024).setAddressFullMessagePolicy(AddressFullMessagePolicy.BLOCK);
    HierarchicalRepository<AddressSettings> repos = server.getAddressSettingsRepository();
    repos.addMatch(address.toString(), addressSettings);
    server.start();
    waitForServerToStart(server);
    locator.setProducerWindowSize(1024).setConsumerWindowSize(1024).setAckBatchSize(1024);
    sf = createSessionFactory(locator);
    session = sf.createSession(false, true, true, true);
    ClientProducer producer = session.createProducer(address);
    byte[] bytes = new byte[100];
    final int numMessages = 1000;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session.createMessage(false);
        message.getBodyBuffer().writeBytes(bytes);
        producer.send(message);
    }
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 92 with ClientProducer

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

the class ProducerFlowControlTest method testFlowControl.

private void testFlowControl(final int numMessages, final int messageSize, final int maxSize, final int producerWindowSize, final int consumerWindowSize, final int ackBatchSize, final int numConsumers, final int numProducers, final long consumerDelay, final boolean anon, final int minLargeMessageSize, final boolean realFiles) throws Exception {
    final SimpleString address = new SimpleString("testaddress");
    server = createServer(realFiles, isNetty());
    AddressSettings addressSettings = new AddressSettings().setMaxSizeBytes(maxSize).setAddressFullMessagePolicy(AddressFullMessagePolicy.BLOCK);
    HierarchicalRepository<AddressSettings> repos = server.getAddressSettingsRepository();
    repos.addMatch(address.toString(), addressSettings);
    server.start();
    waitForServerToStart(server);
    locator.setProducerWindowSize(producerWindowSize).setConsumerWindowSize(consumerWindowSize).setAckBatchSize(ackBatchSize);
    if (minLargeMessageSize != -1) {
        locator.setMinLargeMessageSize(minLargeMessageSize);
    }
    sf = createSessionFactory(locator);
    session = sf.createSession(false, true, true, true);
    session.start();
    final String queueName = "testqueue";
    for (int i = 0; i < numConsumers; i++) {
        session.createQueue(address, new SimpleString(queueName + i), null, false);
    }
    final byte[] bytes = RandomUtil.randomBytes(messageSize);
    class MyHandler implements MessageHandler {

        int count = 0;

        final CountDownLatch latch = new CountDownLatch(1);

        volatile Exception exception;

        @Override
        public void onMessage(final ClientMessage message) {
            try {
                byte[] bytesRead = new byte[messageSize];
                message.getBodyBuffer().readBytes(bytesRead);
                ActiveMQTestBase.assertEqualsByteArrays(bytes, bytesRead);
                message.acknowledge();
                if (++count == numMessages * numProducers) {
                    latch.countDown();
                }
                if (consumerDelay > 0) {
                    Thread.sleep(consumerDelay);
                }
            } catch (Exception e) {
                ProducerFlowControlTest.log.error("Failed to handle message", e);
                exception = e;
                latch.countDown();
            }
        }
    }
    MyHandler[] handlers = new MyHandler[numConsumers];
    for (int i = 0; i < numConsumers; i++) {
        handlers[i] = new MyHandler();
        ClientConsumer consumer = session.createConsumer(new SimpleString(queueName + i));
        consumer.setMessageHandler(handlers[i]);
    }
    ClientProducer[] producers = new ClientProducer[numProducers];
    for (int i = 0; i < numProducers; i++) {
        if (anon) {
            producers[i] = session.createProducer();
        } else {
            producers[i] = session.createProducer(address);
        }
    }
    long start = System.currentTimeMillis();
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session.createMessage(false);
        message.getBodyBuffer().writeBytes(bytes);
        for (int j = 0; j < numProducers; j++) {
            if (anon) {
                producers[j].send(address, message);
            } else {
                producers[j].send(message);
            }
        }
    }
    for (int i = 0; i < numConsumers; i++) {
        Assert.assertTrue(handlers[i].latch.await(5, TimeUnit.MINUTES));
        Assert.assertNull(handlers[i].exception);
    }
    long end = System.currentTimeMillis();
    double rate = 1000 * (double) numMessages / (end - start);
    ProducerFlowControlTest.log.info("rate is " + rate + " msgs / sec");
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) MessageHandler(org.apache.activemq.artemis.api.core.client.MessageHandler) 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) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQObjectClosedException(org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer)

Example 93 with ClientProducer

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

the class ProducerFlowControlTest method testClosingSessionUnblocksBlockedProducer.

@Test
public void testClosingSessionUnblocksBlockedProducer() throws Exception {
    final SimpleString address = new SimpleString("testaddress");
    server = createServer(false, isNetty());
    AddressSettings addressSettings = new AddressSettings().setMaxSizeBytes(1024).setAddressFullMessagePolicy(AddressFullMessagePolicy.BLOCK);
    HierarchicalRepository<AddressSettings> repos = server.getAddressSettingsRepository();
    repos.addMatch(address.toString(), addressSettings);
    server.start();
    waitForServerToStart(server);
    locator.setProducerWindowSize(1024).setConsumerWindowSize(1024).setAckBatchSize(1024);
    sf = createSessionFactory(locator);
    session = sf.createSession(false, true, true, true);
    final SimpleString queueName = new SimpleString("testqueue");
    session.createQueue(address, queueName, null, false);
    ClientProducer producer = session.createProducer(address);
    byte[] bytes = new byte[2000];
    ClientMessage message = session.createMessage(false);
    message.getBodyBuffer().writeBytes(bytes);
    final AtomicBoolean closed = new AtomicBoolean(false);
    Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(500);
                closed.set(true);
                session.close();
            } catch (Exception e) {
            }
        }
    });
    t.start();
    try {
        // This will block
        for (int i = 0; i < 10; i++) {
            producer.send(message);
        }
    } catch (ActiveMQObjectClosedException expected) {
    }
    Assert.assertTrue(closed.get());
    t.join();
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ActiveMQObjectClosedException(org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ActiveMQObjectClosedException(org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 94 with ClientProducer

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

the class ProducerFlowControlTest method testProducerCreditsCaching4.

@Test
public void testProducerCreditsCaching4() throws Exception {
    server = createServer(false, isNetty());
    server.start();
    waitForServerToStart(server);
    sf = createSessionFactory(locator);
    session = sf.createSession(false, true, true, true);
    session.createQueue("address", "queue1", null, false);
    ClientProducerCredits credits = null;
    for (int i = 0; i < ClientProducerCreditManagerImpl.MAX_UNREFERENCED_CREDITS_CACHE_SIZE; i++) {
        ClientProducer prod = session.createProducer("address" + i);
        ClientProducerCredits newCredits = ((ClientProducerInternal) prod).getProducerCredits();
        if (credits != null) {
            Assert.assertFalse(newCredits == credits);
        }
        credits = newCredits;
        prod.close();
        Assert.assertEquals(i + 1, ((ClientSessionInternal) session).getProducerCreditManager().creditsMapSize());
        Assert.assertEquals(i + 1, ((ClientSessionInternal) session).getProducerCreditManager().unReferencedCreditsSize());
    }
}
Also used : ClientProducerInternal(org.apache.activemq.artemis.core.client.impl.ClientProducerInternal) ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) ClientProducerCredits(org.apache.activemq.artemis.core.client.impl.ClientProducerCredits) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 95 with ClientProducer

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

the class ProducerFlowControlTest method testProducerCreditsCaching6.

@Test
public void testProducerCreditsCaching6() throws Exception {
    server = createServer(false, isNetty());
    server.start();
    waitForServerToStart(server);
    sf = createSessionFactory(locator);
    session = sf.createSession(false, true, true, true);
    session.createQueue("address", "queue1", null, false);
    for (int i = 0; i < ClientProducerCreditManagerImpl.MAX_UNREFERENCED_CREDITS_CACHE_SIZE; i++) {
        ClientProducer prod = session.createProducer((String) null);
        prod.send("address", session.createMessage(false));
        Assert.assertEquals(1, ((ClientSessionInternal) session).getProducerCreditManager().creditsMapSize());
        Assert.assertEquals(1, ((ClientSessionInternal) session).getProducerCreditManager().unReferencedCreditsSize());
    }
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Aggregations

ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)859 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)716 Test (org.junit.Test)702 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)658 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)644 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)478 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)395 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)173 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)166 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)106 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)103 CountDownLatch (java.util.concurrent.CountDownLatch)102 Configuration (org.apache.activemq.artemis.core.config.Configuration)93 Queue (org.apache.activemq.artemis.core.server.Queue)85 Xid (javax.transaction.xa.Xid)70 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)64 HashMap (java.util.HashMap)63 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)55 ArrayList (java.util.ArrayList)51 QueueControl (org.apache.activemq.artemis.api.core.management.QueueControl)50