Search in sources :

Example 26 with ClientConsumer

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

the class ConsumerTest method testReceiveAndResend.

@Test
public void testReceiveAndResend() throws Exception {
    final Set<Object> sessions = new ConcurrentHashSet<>();
    final AtomicInteger errors = new AtomicInteger(0);
    final SimpleString QUEUE_RESPONSE = SimpleString.toSimpleString("QUEUE_RESPONSE");
    final int numberOfSessions = 50;
    final int numberOfMessages = 10;
    final CountDownLatch latchReceive = new CountDownLatch(numberOfSessions * numberOfMessages);
    ClientSessionFactory sf = locator.createSessionFactory();
    for (int i = 0; i < numberOfSessions; i++) {
        ClientSession session = sf.createSession(false, true, true);
        sessions.add(session);
        session.createQueue(QUEUE, QUEUE.concat("" + i), null, true);
        if (i == 0) {
            session.createQueue(QUEUE_RESPONSE, QUEUE_RESPONSE);
        }
        ClientConsumer consumer = session.createConsumer(QUEUE.concat("" + i));
        sessions.add(consumer);
        {
            consumer.setMessageHandler(new MessageHandler() {

                @Override
                public void onMessage(final ClientMessage msg) {
                    try {
                        ServerLocator locatorSendx = createFactory(isNetty()).setReconnectAttempts(15);
                        ClientSessionFactory factoryx = locatorSendx.createSessionFactory();
                        ClientSession sessionSend = factoryx.createSession(true, true);
                        sessions.add(sessionSend);
                        sessions.add(locatorSendx);
                        sessions.add(factoryx);
                        final ClientProducer prod = sessionSend.createProducer(QUEUE_RESPONSE);
                        sessionSend.start();
                        sessions.add(prod);
                        msg.acknowledge();
                        prod.send(sessionSend.createMessage(true));
                        prod.close();
                        sessionSend.commit();
                        sessionSend.close();
                        factoryx.close();
                        if (Thread.currentThread().isInterrupted()) {
                            System.err.println("Netty has interrupted a thread!!!");
                            errors.incrementAndGet();
                        }
                    } catch (Throwable e) {
                        e.printStackTrace();
                        errors.incrementAndGet();
                    } finally {
                        latchReceive.countDown();
                    }
                }
            });
        }
        session.start();
    }
    Thread tCons = new Thread() {

        @Override
        public void run() {
            try {
                final ServerLocator locatorSend = createFactory(isNetty());
                final ClientSessionFactory factory = locatorSend.createSessionFactory();
                final ClientSession sessionSend = factory.createSession(true, true);
                ClientConsumer cons = sessionSend.createConsumer(QUEUE_RESPONSE);
                sessionSend.start();
                for (int i = 0; i < numberOfMessages * numberOfSessions; i++) {
                    ClientMessage msg = cons.receive(5000);
                    if (msg == null) {
                        break;
                    }
                    msg.acknowledge();
                }
                if (cons.receiveImmediate() != null) {
                    System.out.println("ERROR: Received an extra message");
                    errors.incrementAndGet();
                }
                sessionSend.close();
                factory.close();
                locatorSend.close();
            } catch (Exception e) {
                e.printStackTrace();
                errors.incrementAndGet();
            }
        }
    };
    tCons.start();
    ClientSession mainSessionSend = sf.createSession(true, true);
    ClientProducer mainProd = mainSessionSend.createProducer(QUEUE);
    for (int i = 0; i < numberOfMessages; i++) {
        mainProd.send(mainSessionSend.createMessage(true));
    }
    latchReceive.await(2, TimeUnit.MINUTES);
    tCons.join();
    sf.close();
    assertEquals("Had errors along the execution", 0, errors.get());
}
Also used : 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) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQIllegalStateException(org.apache.activemq.artemis.api.core.ActiveMQIllegalStateException) JMSException(javax.jms.JMSException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashSet(org.apache.activemq.artemis.utils.collections.ConcurrentHashSet) 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) Test(org.junit.Test)

Example 27 with ClientConsumer

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

the class ConsumerTest method testInVMURI.

// https://jira.jboss.org/jira/browse/HORNETQ-111
// Test that, on rollback credits are released for messages cleared in the buffer
@Test
public void testInVMURI() throws Exception {
    locator.close();
    ServerLocator locator = addServerLocator(ServerLocatorImpl.newLocator("vm:/1"));
    ClientSessionFactory factory = locator.createSessionFactory();
    ClientSession session = factory.createSession();
    ClientProducer producer = session.createProducer(QUEUE);
    producer.send(session.createMessage(true));
    ClientConsumer consumer = session.createConsumer(QUEUE);
    session.start();
    Assert.assertNotNull(consumer.receiveImmediate());
    session.close();
    factory.close();
}
Also used : 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) Test(org.junit.Test)

Example 28 with ClientConsumer

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

the class ConsumerTest method testConsumerAckImmediateAutoCommitTrue.

@Test
public void testConsumerAckImmediateAutoCommitTrue() throws Exception {
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, true, true);
    ClientProducer producer = session.createProducer(QUEUE);
    final int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = createTextMessage(session, "m" + i);
        producer.send(message);
    }
    ClientConsumer consumer = session.createConsumer(QUEUE);
    session.start();
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message2 = consumer.receive(1000);
        Assert.assertEquals("m" + i, message2.getBodyBuffer().readString());
    }
    // assert that all the messages are there and none have been acked
    Assert.assertEquals(0, ((Queue) server.getPostOffice().getBinding(QUEUE).getBindable()).getDeliveringCount());
    Assert.assertEquals(0, getMessageCount(((Queue) server.getPostOffice().getBinding(QUEUE).getBindable())));
    session.close();
}
Also used : 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)

Example 29 with ClientConsumer

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

the class AcknowledgeTest method testAsyncConsumerAck.

@Test
public void testAsyncConsumerAck() throws Exception {
    ActiveMQServer server = createServer(false);
    server.start();
    ServerLocator locator = createInVMNonHALocator().setBlockOnAcknowledge(true).setAckBatchSize(0);
    ClientSessionFactory cf = createSessionFactory(locator);
    ClientSession sendSession = cf.createSession(false, true, true);
    final ClientSession session = cf.createSession(false, true, true);
    sendSession.createQueue(addressA, queueA, false);
    ClientProducer cp = sendSession.createProducer(addressA);
    ClientConsumer cc = session.createConsumer(queueA);
    int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        cp.send(sendSession.createMessage(false));
    }
    final CountDownLatch latch = new CountDownLatch(numMessages);
    session.start();
    cc.setMessageHandler(new MessageHandler() {

        @Override
        public void onMessage(final ClientMessage message) {
            try {
                message.acknowledge();
            } catch (ActiveMQException e) {
                try {
                    session.close();
                } catch (ActiveMQException e1) {
                    e1.printStackTrace();
                }
            }
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    Queue q = (Queue) server.getPostOffice().getBinding(queueA).getBindable();
    Assert.assertEquals(0, q.getDeliveringCount());
    sendSession.close();
    session.close();
}
Also used : 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) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) 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) Queue(org.apache.activemq.artemis.core.server.Queue) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 30 with ClientConsumer

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

the class AcknowledgeTest method testReceiveAckLastMessageOnly.

@Test
public void testReceiveAckLastMessageOnly() throws Exception {
    ActiveMQServer server = createServer(false);
    server.start();
    ServerLocator locator = createInVMNonHALocator().setAckBatchSize(0).setBlockOnAcknowledge(true);
    ClientSessionFactory cf = createSessionFactory(locator);
    ClientSession sendSession = cf.createSession(false, true, true);
    ClientSession session = cf.createSession(false, true, true);
    sendSession.createQueue(addressA, queueA, false);
    ClientProducer cp = sendSession.createProducer(addressA);
    ClientConsumer cc = session.createConsumer(queueA);
    int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        cp.send(sendSession.createMessage(false));
    }
    session.start();
    ClientMessage cm = null;
    for (int i = 0; i < numMessages; i++) {
        cm = cc.receive(5000);
        Assert.assertNotNull(cm);
    }
    cm.acknowledge();
    Queue q = (Queue) server.getPostOffice().getBinding(queueA).getBindable();
    Assert.assertEquals(0, q.getDeliveringCount());
    session.close();
    sendSession.close();
}
Also used : 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) 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) Queue(org.apache.activemq.artemis.core.server.Queue) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Aggregations

ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)720 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)642 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)624 Test (org.junit.Test)584 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)569 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)409 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)348 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)154 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)132 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)88 Configuration (org.apache.activemq.artemis.core.config.Configuration)81 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)79 CountDownLatch (java.util.concurrent.CountDownLatch)72 Queue (org.apache.activemq.artemis.core.server.Queue)63 DivertConfiguration (org.apache.activemq.artemis.core.config.DivertConfiguration)62 Xid (javax.transaction.xa.Xid)55 StoreConfiguration (org.apache.activemq.artemis.core.config.StoreConfiguration)52 HashMap (java.util.HashMap)48 DatabaseStorageConfiguration (org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration)46 ArrayList (java.util.ArrayList)44