Search in sources :

Example 31 with ClientConsumer

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

the class AcknowledgeTest method testInvalidACK.

/**
 * This is validating a case where a consumer will try to ack a message right after failover, but the consumer at the target server didn't
 * receive the message yet.
 * on that case the system should rollback any acks done and redeliver any messages
 */
@Test
public void testInvalidACK() throws Exception {
    ActiveMQServer server = createServer(false);
    server.start();
    ServerLocator locator = createInVMNonHALocator().setAckBatchSize(0).setBlockOnAcknowledge(true);
    ClientSessionFactory cf = createSessionFactory(locator);
    int numMessages = 100;
    ClientSession sessionConsumer = cf.createSession(true, true, 0);
    sessionConsumer.start();
    sessionConsumer.createQueue(addressA, queueA, true);
    ClientConsumer consumer = sessionConsumer.createConsumer(queueA);
    // sending message
    {
        ClientSession sendSession = cf.createSession(false, true, true);
        ClientProducer cp = sendSession.createProducer(addressA);
        for (int i = 0; i < numMessages; i++) {
            ClientMessage msg = sendSession.createMessage(true);
            msg.putIntProperty("seq", i);
            cp.send(msg);
        }
        sendSession.close();
    }
    {
        ClientMessage msg = consumer.receive(5000);
        // need to way some time before all the possible references are sent to the consumer
        // as we need to guarantee the order on cancellation on this test
        Thread.sleep(1000);
        try {
            // pretending to be an unbehaved client doing an invalid ack right after failover
            ((ClientSessionInternal) sessionConsumer).acknowledge(new FakeConsumerWithID(0), new FakeMessageWithID(12343));
            fail("supposed to throw an exception here");
        } catch (Exception e) {
        }
        try {
            // pretending to be an unbehaved client doing an invalid ack right after failover
            ((ClientSessionInternal) sessionConsumer).acknowledge(new FakeConsumerWithID(3), new FakeMessageWithID(12343));
            fail("supposed to throw an exception here");
        } catch (Exception e) {
            e.printStackTrace();
        }
        consumer.close();
        consumer = sessionConsumer.createConsumer(queueA);
        for (int i = 0; i < numMessages; i++) {
            msg = consumer.receive(5000);
            assertNotNull(msg);
            assertEquals(i, msg.getIntProperty("seq").intValue());
            msg.acknowledge();
        }
    }
}
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) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQPropertyConversionException(org.apache.activemq.artemis.api.core.ActiveMQPropertyConversionException) Test(org.junit.Test)

Example 32 with ClientConsumer

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

the class AcknowledgeTest method testAsyncConsumerAckLastMessageOnly.

@Test
public void testAsyncConsumerAckLastMessageOnly() 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) {
            if (latch.getCount() == 1) {
                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 33 with ClientConsumer

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

the class AddressSettingsTest method test2LevelWordHierarchyWithDLA.

@Test
public void test2LevelWordHierarchyWithDLA() throws Exception {
    ActiveMQServer server = createServer(false);
    server.start();
    AddressSettings addressSettings = new AddressSettings().setDeadLetterAddress(dlaA).setMaxDeliveryAttempts(1);
    AddressSettings addressSettings2 = new AddressSettings().setDeadLetterAddress(dlaB).setMaxDeliveryAttempts(1);
    HierarchicalRepository<AddressSettings> repos = server.getAddressSettingsRepository();
    repos.addMatch(addressA.toString(), addressSettings);
    repos.addMatch("*", addressSettings2);
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, false);
    session.createQueue(addressA, queueA, false);
    session.createQueue(addressB, queueB, false);
    session.createQueue(dlaA, dlqA, false);
    session.createQueue(dlaB, dlqB, false);
    ClientSession sendSession = sf.createSession(false, true, true);
    ClientMessage cm = sendSession.createMessage(true);
    cm.getBodyBuffer().writeString("A");
    ClientMessage cm2 = sendSession.createMessage(true);
    cm2.getBodyBuffer().writeString("B");
    ClientProducer cp1 = sendSession.createProducer(addressA);
    ClientProducer cp2 = sendSession.createProducer(addressB);
    cp1.send(cm);
    cp2.send(cm2);
    ClientConsumer dlqARec = session.createConsumer(dlqA);
    ClientConsumer dlqBrec = session.createConsumer(dlqB);
    ClientConsumer cc1 = session.createConsumer(queueA);
    ClientConsumer cc2 = session.createConsumer(queueB);
    session.start();
    ClientMessage message = cc1.receive(5000);
    Assert.assertNotNull(message);
    message.acknowledge();
    message = cc2.receive(5000);
    Assert.assertNotNull(message);
    message.acknowledge();
    session.rollback();
    cc1.close();
    cc2.close();
    message = dlqARec.receive(5000);
    Assert.assertNotNull(message);
    Assert.assertEquals("A", message.getBodyBuffer().readString());
    message = dlqBrec.receive(5000);
    Assert.assertNotNull(message);
    Assert.assertEquals("B", message.getBodyBuffer().readString());
    sendSession.close();
    session.close();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) 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) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 34 with ClientConsumer

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

the class AddressSettingsTest method testOverrideHierarchyWithDLA.

@Test
public void testOverrideHierarchyWithDLA() throws Exception {
    ActiveMQServer server = createServer(false);
    server.start();
    AddressSettings addressSettings = new AddressSettings().setMaxDeliveryAttempts(1);
    AddressSettings addressSettings2 = new AddressSettings().setMaxDeliveryAttempts(1);
    AddressSettings addressSettings3 = new AddressSettings().setDeadLetterAddress(dlaC).setMaxDeliveryAttempts(1);
    HierarchicalRepository<AddressSettings> repos = server.getAddressSettingsRepository();
    repos.addMatch(addressA2.toString(), addressSettings);
    repos.addMatch("add.*", addressSettings2);
    repos.addMatch("#", addressSettings3);
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, false);
    session.createQueue(addressA2, queueA, false);
    session.createQueue(addressB2, queueB, false);
    session.createQueue(addressC, queueC, false);
    session.createQueue(dlaA, dlqA, false);
    session.createQueue(dlaB, dlqB, false);
    session.createQueue(dlaC, dlqC, false);
    ClientSession sendSession = sf.createSession(false, true, true);
    ClientMessage cm = sendSession.createMessage(true);
    ClientMessage cm2 = sendSession.createMessage(true);
    ClientMessage cm3 = sendSession.createMessage(true);
    ClientProducer cp1 = sendSession.createProducer(addressA2);
    ClientProducer cp2 = sendSession.createProducer(addressB2);
    ClientProducer cp3 = sendSession.createProducer(addressC);
    cp1.send(cm);
    cp2.send(cm2);
    cp3.send(cm3);
    ClientConsumer dlqCrec = session.createConsumer(dlqC);
    ClientConsumer cc1 = session.createConsumer(queueA);
    ClientConsumer cc2 = session.createConsumer(queueB);
    ClientConsumer cc3 = session.createConsumer(queueC);
    session.start();
    ClientMessage message = cc1.receive(5000);
    Assert.assertNotNull(message);
    message.acknowledge();
    message = cc2.receive(5000);
    Assert.assertNotNull(message);
    message.acknowledge();
    message = cc3.receive(5000);
    Assert.assertNotNull(message);
    message.acknowledge();
    session.rollback();
    cc1.close();
    cc2.close();
    cc3.close();
    message = dlqCrec.receive(5000);
    Assert.assertNotNull(message);
    message = dlqCrec.receive(5000);
    Assert.assertNotNull(message);
    message = dlqCrec.receive(5000);
    Assert.assertNotNull(message);
    sendSession.close();
    session.close();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) 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) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 35 with ClientConsumer

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

the class AddressSettingsTest method test2LevelHierarchyWithDLA.

@Test
public void test2LevelHierarchyWithDLA() throws Exception {
    ActiveMQServer server = createServer(false);
    server.start();
    AddressSettings addressSettings = new AddressSettings().setDeadLetterAddress(dlaA).setMaxDeliveryAttempts(1);
    AddressSettings addressSettings2 = new AddressSettings().setDeadLetterAddress(dlaB).setMaxDeliveryAttempts(1);
    HierarchicalRepository<AddressSettings> repos = server.getAddressSettingsRepository();
    repos.addMatch(addressA.toString(), addressSettings);
    repos.addMatch("#", addressSettings2);
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, false);
    session.createQueue(addressA, queueA, false);
    session.createQueue(addressB, queueB, false);
    session.createQueue(dlaA, dlqA, false);
    session.createQueue(dlaB, dlqB, false);
    ClientSession sendSession = sf.createSession(false, true, true);
    ClientMessage cm = sendSession.createMessage(true);
    cm.getBodyBuffer().writeString("A");
    ClientMessage cm2 = sendSession.createMessage(true);
    cm2.getBodyBuffer().writeString("B");
    ClientProducer cp1 = sendSession.createProducer(addressA);
    ClientProducer cp2 = sendSession.createProducer(addressB);
    cp1.send(cm);
    cp2.send(cm2);
    ClientConsumer dlqARec = session.createConsumer(dlqA);
    ClientConsumer dlqBrec = session.createConsumer(dlqB);
    ClientConsumer cc1 = session.createConsumer(queueA);
    ClientConsumer cc2 = session.createConsumer(queueB);
    session.start();
    ClientMessage message = cc1.receive(5000);
    Assert.assertNotNull(message);
    message.acknowledge();
    message = cc2.receive(5000);
    Assert.assertNotNull(message);
    message.acknowledge();
    session.rollback();
    cc1.close();
    cc2.close();
    message = dlqARec.receive(5000);
    Assert.assertNotNull(message);
    Assert.assertEquals("A", message.getBodyBuffer().readString());
    message = dlqBrec.receive(5000);
    Assert.assertNotNull(message);
    Assert.assertEquals("B", message.getBodyBuffer().readString());
    sendSession.close();
    session.close();
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) 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) 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