Search in sources :

Example 31 with ClientMessage

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

the class ConsumerWindowSizeTest method testNoWindowRoundRobin.

private void testNoWindowRoundRobin(final boolean largeMessages) throws Exception {
    ActiveMQServer server = createServer(false, isNetty());
    ClientSession sessionA = null;
    ClientSession sessionB = null;
    try {
        final int numberOfMessages = 100;
        server.start();
        locator.setConsumerWindowSize(-1);
        if (largeMessages) {
            locator.setMinLargeMessageSize(100);
        }
        ClientSessionFactory sf = createSessionFactory(locator);
        sessionA = sf.createSession(false, true, true);
        SimpleString ADDRESS = new SimpleString("some-queue");
        sessionA.createQueue(ADDRESS, ADDRESS, true);
        sessionB = sf.createSession(false, true, true);
        sessionA.start();
        sessionB.start();
        ClientConsumerInternal consA = (ClientConsumerInternal) sessionA.createConsumer(ADDRESS);
        ClientConsumerInternal consB = (ClientConsumerInternal) sessionB.createConsumer(ADDRESS);
        {
            // We can only guarantee round robing with WindowSize = -1, after the ServerConsumer object received
            // SessionConsumerFlowCreditMessage(-1)
            // Since that is done asynchronously we verify that the information was received before we proceed on
            // sending messages or else the distribution won't be
            // even as expected by the test
            Bindings bindings = server.getPostOffice().getBindingsForAddress(ADDRESS);
            Assert.assertEquals(1, bindings.getBindings().size());
            for (Binding binding : bindings.getBindings()) {
                Collection<Consumer> consumers = ((QueueBinding) binding).getQueue().getConsumers();
                for (Consumer consumer : consumers) {
                    ServerConsumerImpl consumerImpl = (ServerConsumerImpl) consumer;
                    long timeout = System.currentTimeMillis() + 5000;
                    while (timeout > System.currentTimeMillis() && consumerImpl.getAvailableCredits() != null) {
                        Thread.sleep(10);
                    }
                    Assert.assertNull(consumerImpl.getAvailableCredits());
                }
            }
        }
        ClientProducer prod = sessionA.createProducer(ADDRESS);
        for (int i = 0; i < numberOfMessages; i++) {
            ClientMessage msg = createTextMessage(sessionA, "Msg" + i);
            if (largeMessages) {
                msg.getBodyBuffer().writeBytes(new byte[600]);
            }
            prod.send(msg);
        }
        long timeout = System.currentTimeMillis() + TIMEOUT * 1000;
        boolean foundA = false;
        boolean foundB = false;
        do {
            foundA = consA.getBufferSize() == numberOfMessages / 2;
            foundB = consB.getBufferSize() == numberOfMessages / 2;
            Thread.sleep(10);
        } while ((!foundA || !foundB) && System.currentTimeMillis() < timeout);
        Assert.assertTrue("ConsumerA didn't receive the expected number of messages on buffer (consA=" + consA.getBufferSize() + ", consB=" + consB.getBufferSize() + ") foundA = " + foundA + " foundB = " + foundB, foundA);
        Assert.assertTrue("ConsumerB didn't receive the expected number of messages on buffer (consA=" + consA.getBufferSize() + ", consB=" + consB.getBufferSize() + ") foundA = " + foundA + " foundB = " + foundB, foundB);
    } finally {
        try {
            if (sessionA != null) {
                sessionA.close();
            }
            if (sessionB != null) {
                sessionB.close();
            }
        } catch (Exception ignored) {
        }
    }
}
Also used : Binding(org.apache.activemq.artemis.core.postoffice.Binding) QueueBinding(org.apache.activemq.artemis.core.postoffice.QueueBinding) ClientConsumerInternal(org.apache.activemq.artemis.core.client.impl.ClientConsumerInternal) QueueBinding(org.apache.activemq.artemis.core.postoffice.QueueBinding) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) Bindings(org.apache.activemq.artemis.core.postoffice.Bindings) IOException(java.io.IOException) ServerConsumerImpl(org.apache.activemq.artemis.core.server.impl.ServerConsumerImpl) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) Consumer(org.apache.activemq.artemis.core.server.Consumer) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Collection(java.util.Collection) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer)

Example 32 with ClientMessage

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

the class ConsumerWindowSizeTest method testReceiveImmediateWithZeroWindow3.

// https://jira.jboss.org/jira/browse/HORNETQ-385
@Test
public void testReceiveImmediateWithZeroWindow3() throws Exception {
    ActiveMQServer server = createServer(false, isNetty());
    server.start();
    locator.setConsumerWindowSize(0);
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(false, false, false);
    session.createQueue("testWindow", "testWindow", true);
    session.close();
    int numConsumers = 5;
    ArrayList<ClientConsumer> consumers = new ArrayList<>();
    ArrayList<ClientSession> sessions = new ArrayList<>();
    for (int i = 0; i < numConsumers; i++) {
        ClientSession session1 = sf.createSession();
        ClientConsumer consumer = session1.createConsumer("testWindow");
        consumers.add(consumer);
        session1.start();
        sessions.add(session1);
        consumer.receive(10);
    }
    ClientSession senderSession = sf.createSession(false, false);
    ClientProducer producer = senderSession.createProducer("testWindow");
    ClientMessage sent = senderSession.createMessage(true);
    sent.putStringProperty("hello", "world");
    producer.send(sent);
    senderSession.commit();
    senderSession.start();
    ClientConsumer consumer = consumers.get(2);
    ClientMessage received = consumer.receive(1000);
    Assert.assertNotNull(received);
    for (ClientSession tmpSess : sessions) {
        tmpSess.close();
    }
    senderSession.close();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ArrayList(java.util.ArrayList) 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 33 with ClientMessage

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

the class ConsumerWindowSizeTest method testReceiveImmediateWithZeroWindow4.

@Test
public void testReceiveImmediateWithZeroWindow4() throws Exception {
    ActiveMQServer server = createServer(false, isNetty());
    server.start();
    locator.setConsumerWindowSize(0);
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(false, false, false);
    session.createQueue("testWindow", "testWindow", true);
    session.close();
    int numConsumers = 5;
    ArrayList<ClientConsumer> consumers = new ArrayList<>();
    ArrayList<ClientSession> sessions = new ArrayList<>();
    for (int i = 0; i < numConsumers; i++) {
        ClientSession session1 = sf.createSession();
        ClientConsumer consumer = session1.createConsumer("testWindow");
        consumers.add(consumer);
        session1.start();
        sessions.add(session1);
        Assert.assertNull(consumer.receive(10));
    }
    ClientSession senderSession = sf.createSession(false, false);
    ClientProducer producer = senderSession.createProducer("testWindow");
    ClientMessage sent = senderSession.createMessage(true);
    sent.putStringProperty("hello", "world");
    producer.send(sent);
    senderSession.commit();
    senderSession.start();
    ClientConsumer consumer = consumers.get(2);
    ClientMessage received = consumer.receive(5000);
    Assert.assertNotNull(received);
    for (ClientSession tmpSess : sessions) {
        tmpSess.close();
    }
    senderSession.close();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ArrayList(java.util.ArrayList) 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 34 with ClientMessage

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

the class ConsumerWindowSizeTest method internalTestSlowConsumerOnMessageHandlerNoBuffers.

public void internalTestSlowConsumerOnMessageHandlerNoBuffers(final boolean largeMessages) throws Exception {
    ActiveMQServer server = createServer(false, isNetty());
    ClientSession sessionB = null;
    ClientSession session = null;
    try {
        final int numberOfMessages = 100;
        server.start();
        locator.setConsumerWindowSize(0);
        if (largeMessages) {
            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 latchDone = new CountDownLatch(1);
        // It can't close the session while the large message is being read
        final CountDownLatch latchRead = 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 {
                    String str = getTextMessage(message);
                    failed = failed || !str.equals("Msg" + count);
                    message.acknowledge();
                    latchReceived.countDown();
                    if (count++ == 1) {
                        // it will hold here for a while
                        if (!latchDone.await(TIMEOUT, TimeUnit.SECONDS)) {
                            // a timed wait, so if the test fails, one less thread around
                            new Exception("ClientConsuemrWindowSizeTest Handler couldn't receive signal in less than 5 seconds").printStackTrace();
                            failed = true;
                        }
                        if (largeMessages) {
                            message.getBodyBuffer().readBytes(new byte[600]);
                        }
                        latchRead.countDown();
                    }
                } catch (Exception e) {
                    // Hudson / JUnit report
                    e.printStackTrace();
                    failed = true;
                }
            }
        }
        LocalHandler handler = new LocalHandler();
        ClientConsumer cons1 = session.createConsumer(ADDRESS);
        ClientProducer prod = session.createProducer(ADDRESS);
        for (int i = 0; i < numberOfMessages; i++) {
            ClientMessage msg = createTextMessage(session, "Msg" + i);
            if (largeMessages) {
                msg.getBodyBuffer().writeBytes(new byte[600]);
            }
            prod.send(msg);
        }
        consReceiveOneAndHold.setMessageHandler(handler);
        Assert.assertTrue(latchReceived.await(TIMEOUT, TimeUnit.SECONDS));
        Assert.assertEquals(0, consReceiveOneAndHold.getBufferSize());
        for (int i = 2; i < numberOfMessages; i++) {
            ClientMessage msg = cons1.receive(1000);
            Assert.assertNotNull("expected message at i = " + i, msg);
            Assert.assertEquals("Msg" + i, getTextMessage(msg));
            msg.acknowledge();
        }
        Assert.assertEquals(0, consReceiveOneAndHold.getBufferSize());
        latchDone.countDown();
        // The test can' t close the session while the message is still being read, or it could interrupt the data
        Assert.assertTrue(latchRead.await(10, 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) {
        }
    }
}
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)

Example 35 with ClientMessage

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

the class ConsumerWindowSizeTest method testSingleImmediate.

@Test
public void testSingleImmediate() throws Exception {
    final int NUMBER_OF_MESSAGES = 200;
    ActiveMQServer server = createServer(false, isNetty());
    server.start();
    locator.setConsumerWindowSize(0);
    final ClientSessionFactory sf = createSessionFactory(locator);
    {
        ClientSession session = sf.createSession(false, false, false);
        session.createQueue("testWindow", "testWindow", true);
        session.close();
    }
    final AtomicInteger received = new AtomicInteger(0);
    ClientSession senderSession = sf.createSession(false, false);
    ClientProducer producer = senderSession.createProducer("testWindow");
    ClientMessage sent = senderSession.createMessage(true);
    sent.putStringProperty("hello", "world");
    for (int i = 0; i < NUMBER_OF_MESSAGES; i++) {
        producer.send(sent);
    }
    senderSession.commit();
    ClientSession session = sf.createSession(false, false);
    ClientConsumer consumer = session.createConsumer("testWindow");
    session.start();
    while (true) {
        ClientMessage msg = consumer.receiveImmediate();
        if (msg == null) {
            System.out.println("Returning null");
            break;
        }
        msg.acknowledge();
        session.commit();
        received.incrementAndGet();
    }
    Assert.assertEquals(NUMBER_OF_MESSAGES, received.get());
}
Also used : 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) 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)

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