Search in sources :

Example 41 with ClientConsumer

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

the class ExpiryLargeMessageTest method testExpiryMessagesThenDLQ.

// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testExpiryMessagesThenDLQ() throws Exception {
    ActiveMQServer server = createServer(true);
    server.getConfiguration().setMessageExpiryScanPeriod(600000);
    AddressSettings setting = new AddressSettings().setAddressFullMessagePolicy(AddressFullMessagePolicy.PAGE).setMaxDeliveryAttempts(5).setMaxSizeBytes(50 * 1024).setPageSizeBytes(10 * 1024).setExpiryAddress(EXPIRY).setDeadLetterAddress(DLQ);
    server.getAddressSettingsRepository().addMatch(MY_QUEUE.toString(), setting);
    server.getAddressSettingsRepository().addMatch(EXPIRY.toString(), setting);
    server.start();
    server.createQueue(EXPIRY, RoutingType.ANYCAST, EXPIRY, null, true, false);
    server.createQueue(DLQ, RoutingType.ANYCAST, DLQ, null, true, false);
    server.createQueue(MY_QUEUE, RoutingType.ANYCAST, MY_QUEUE, null, true, false);
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory sf = addSessionFactory(createSessionFactory(locator));
    ClientSession session = sf.createSession(true, true, 0);
    byte[] bufferSample = new byte[messageSize];
    for (int i = 0; i < bufferSample.length; i++) {
        bufferSample[i] = getSamplebyte(i);
    }
    ClientProducer producer = session.createProducer(MY_QUEUE);
    long timeToExpiry = System.currentTimeMillis() + 1000;
    for (int i = 0; i < numberOfMessages; i++) {
        ClientMessage message = session.createMessage(true);
        message.putIntProperty("count", i);
        // Send a few regular messages first, then all is just large messages
        if (i % 2 == 0) {
            message.putBooleanProperty("tst-large", false);
            message.getBodyBuffer().writeBytes(bufferSample);
        } else {
            message.putBooleanProperty("tst-large", true);
            message.setBodyInputStream(createFakeLargeStream(messageSize));
        }
        message.setExpiration(timeToExpiry);
        producer.send(message);
    }
    session.close();
    server.stop();
    server.start();
    Queue queueExpiry = server.locateQueue(EXPIRY);
    Queue myQueue = server.locateQueue(MY_QUEUE);
    sf = createSessionFactory(locator);
    Thread.sleep(1500);
    long timeout = System.currentTimeMillis() + 5000;
    while (timeout > System.currentTimeMillis() && getMessageCount(queueExpiry) != numberOfMessages) {
        // What the Expiry Scan would be doing
        myQueue.expireReferences();
        Thread.sleep(50);
    }
    assertEquals(50, getMessageCount(queueExpiry));
    session = sf.createSession(false, false);
    ClientConsumer cons = session.createConsumer(EXPIRY);
    session.start();
    // Consume half of the messages to make sure all the messages are paging (on the second try)
    for (int i = 0; i < numberOfMessages / 2; i++) {
        ClientMessage msg = cons.receive(5000);
        assertNotNull(msg);
        msg.acknowledge();
    }
    session.commit();
    cons.close();
    for (int rep = 0; rep < 6; rep++) {
        cons = session.createConsumer(EXPIRY);
        session.start();
        log.info("Trying " + rep);
        for (int i = 0; i < numberOfMessages / 2; i++) {
            ClientMessage message = cons.receive(5000);
            assertNotNull(message);
            if (i % 10 == 0) {
                System.out.println("Received " + i);
            }
            for (int location = 0; location < messageSize; location++) {
                assertEquals(getSamplebyte(location), message.getBodyBuffer().readByte());
            }
            message.acknowledge();
        }
        session.rollback();
        cons.close();
        session.close();
        sf.close();
        if (rep == 0) {
            // restart the server at the first try
            server.stop();
            server.start();
        }
        sf = createSessionFactory(locator);
        session = sf.createSession(false, false);
        session.start();
    }
    cons = session.createConsumer(EXPIRY);
    session.start();
    assertNull(cons.receiveImmediate());
    cons.close();
    session.close();
    sf.close();
    for (int rep = 0; rep < 2; rep++) {
        sf = createSessionFactory(locator);
        session = sf.createSession(false, false);
        cons = session.createConsumer(DLQ);
        session.start();
        for (int i = 0; i < numberOfMessages / 2; i++) {
            ClientMessage message = cons.receive(5000);
            assertNotNull(message);
            if (i % 10 == 0) {
                System.out.println("Received " + i);
            }
            for (int location = 0; location < messageSize; location++) {
                assertEquals(getSamplebyte(location), message.getBodyBuffer().readByte());
            }
            message.acknowledge();
        }
        if (rep == 0) {
            session.rollback();
            session.close();
            sf.close();
            server.stop();
            server.start();
        }
    }
    session.commit();
    assertNull(cons.receiveImmediate());
    session.close();
    sf.close();
    locator.close();
    validateNoFilesOnLargeDir();
}
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) Queue(org.apache.activemq.artemis.core.server.Queue) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 42 with ClientConsumer

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

the class FullQualifiedQueueTest method testMixedQueues.

@Test
public void testMixedQueues() throws Exception {
    server.createQueue(mixedAddress, RoutingType.MULTICAST, multicastQ1, null, true, false, -1, false, true);
    server.createQueue(mixedAddress, RoutingType.MULTICAST, multicastQ2, null, true, false, -1, false, true);
    server.createQueue(mixedAddress, RoutingType.MULTICAST, multicastQ3, null, true, false, -1, false, true);
    server.createQueue(mixedAddress, RoutingType.ANYCAST, anycastQ1, null, true, false, -1, false, true);
    server.createQueue(mixedAddress, RoutingType.ANYCAST, anycastQ2, null, true, false, -1, false, true);
    server.createQueue(mixedAddress, RoutingType.ANYCAST, anycastQ3, null, true, false, -1, false, true);
    ClientSessionFactory cf = createSessionFactory(locator);
    ClientSession session = cf.createSession();
    session.start();
    // send 3 messages
    ClientProducer producer = session.createProducer(mixedAddress);
    final int num = 3;
    sendMessages(session, producer, num);
    ClientConsumer consumer1 = session.createConsumer(toFullQN(mixedAddress, anycastQ1));
    ClientConsumer consumer2 = session.createConsumer(toFullQN(mixedAddress, anycastQ2));
    ClientConsumer consumer3 = session.createConsumer(toFullQN(mixedAddress, anycastQ3));
    ClientConsumer consumer4 = session.createConsumer(toFullQN(mixedAddress, multicastQ1));
    ClientConsumer consumer5 = session.createConsumer(toFullQN(mixedAddress, multicastQ2));
    ClientConsumer consumer6 = session.createConsumer(toFullQN(mixedAddress, multicastQ3));
    session.start();
    // each anycast consumer receives one, each multicast receives three.
    ClientMessage m = consumer1.receive(2000);
    assertNotNull(m);
    System.out.println("consumer1 : " + m);
    m.acknowledge();
    m = consumer2.receive(2000);
    assertNotNull(m);
    System.out.println("consumer2 : " + m);
    m.acknowledge();
    m = consumer3.receive(2000);
    assertNotNull(m);
    System.out.println("consumer3 : " + m);
    m.acknowledge();
    for (int i = 0; i < num; i++) {
        m = consumer4.receive(2000);
        assertNotNull(m);
        System.out.println("consumer4 : " + m);
        m.acknowledge();
        m = consumer5.receive(2000);
        assertNotNull(m);
        System.out.println("consumer5 : " + m);
        m.acknowledge();
        m = consumer6.receive(2000);
        assertNotNull(m);
        System.out.println("consumer6 : " + m);
        m.acknowledge();
    }
    session.commit();
    // queues are empty now
    for (SimpleString q : new SimpleString[] { anycastQ1, anycastQ2, anycastQ3, multicastQ1, multicastQ2, multicastQ3 }) {
        QueueQueryResult query = server.queueQuery(toFullQN(mixedAddress, q));
        assertTrue(query.isExists());
        assertEquals(mixedAddress, query.getAddress());
        assertEquals(toFullQN(mixedAddress, q), query.getName());
        assertEquals(0, query.getMessageCount());
    }
}
Also used : 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) QueueQueryResult(org.apache.activemq.artemis.core.server.QueueQueryResult) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 43 with ClientConsumer

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

the class FullQualifiedQueueTest method testAnycastQueues.

@Test
public void testAnycastQueues() throws Exception {
    server.createQueue(anycastAddress, RoutingType.ANYCAST, anycastQ1, null, true, false, -1, false, true);
    server.createQueue(anycastAddress, RoutingType.ANYCAST, anycastQ2, null, true, false, -1, false, true);
    server.createQueue(anycastAddress, RoutingType.ANYCAST, anycastQ3, null, true, false, -1, false, true);
    ClientSessionFactory cf = createSessionFactory(locator);
    ClientSession session = cf.createSession();
    session.start();
    // send 3 messages
    ClientProducer producer = session.createProducer(anycastAddress);
    sendMessages(session, producer, 3);
    ClientConsumer consumer1 = session.createConsumer(toFullQN(anycastAddress, anycastQ1));
    ClientConsumer consumer2 = session.createConsumer(toFullQN(anycastAddress, anycastQ2));
    ClientConsumer consumer3 = session.createConsumer(toFullQN(anycastAddress, anycastQ3));
    session.start();
    // each consumer receives one
    ClientMessage m = consumer1.receive(2000);
    assertNotNull(m);
    m.acknowledge();
    m = consumer2.receive(2000);
    assertNotNull(m);
    m.acknowledge();
    m = consumer3.receive(2000);
    assertNotNull(m);
    m.acknowledge();
    session.commit();
    // queues are empty now
    for (SimpleString q : new SimpleString[] { anycastQ1, anycastQ2, anycastQ3 }) {
        QueueQueryResult query = server.queueQuery(toFullQN(anycastAddress, q));
        assertTrue(query.isExists());
        assertEquals(anycastAddress, query.getAddress());
        assertEquals(toFullQN(anycastAddress, q), query.getName());
        assertEquals(0, query.getMessageCount());
    }
}
Also used : 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) QueueQueryResult(org.apache.activemq.artemis.core.server.QueueQueryResult) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 44 with ClientConsumer

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

the class FullQualifiedQueueTest method testSpecialCase.

@Test
public void testSpecialCase() throws Exception {
    server.createQueue(anycastAddress, RoutingType.ANYCAST, anycastQ1, null, true, false, -1, false, true);
    ClientSessionFactory cf = createSessionFactory(locator);
    ClientSession session = cf.createSession();
    session.start();
    ClientProducer producer = session.createProducer(anycastAddress);
    sendMessages(session, producer, 1);
    // ::queue
    ClientConsumer consumer1 = session.createConsumer(toFullQN(new SimpleString(""), anycastQ1));
    session.start();
    ClientMessage m = consumer1.receive(2000);
    assertNotNull(m);
    m.acknowledge();
    session.commit();
    consumer1.close();
    try {
        // queue::
        session.createConsumer(toFullQN(anycastQ1, new SimpleString("")));
        fail("should get exception");
    } catch (ActiveMQNonExistentQueueException e) {
    // expected.
    }
    try {
        // ::
        session.createConsumer(toFullQN(new SimpleString(""), new SimpleString("")));
        fail("should get exception");
    } catch (ActiveMQNonExistentQueueException e) {
    // expected.
    }
}
Also used : 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) ActiveMQNonExistentQueueException(org.apache.activemq.artemis.api.core.ActiveMQNonExistentQueueException) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 45 with ClientConsumer

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

the class HangConsumerTest method testHangOnDelivery.

@Test
public void testHangOnDelivery() throws Exception {
    queue = server.createQueue(QUEUE, RoutingType.ANYCAST, QUEUE, null, true, false);
    try {
        ClientSessionFactory factory = locator.createSessionFactory();
        ClientSession sessionProducer = factory.createSession(false, false, false);
        ServerLocator consumerLocator = createInVMNonHALocator();
        ClientSessionFactory factoryConsumer = consumerLocator.createSessionFactory();
        ClientSession sessionConsumer = factoryConsumer.createSession();
        ClientProducer producer = sessionProducer.createProducer(QUEUE);
        ClientConsumer consumer = sessionConsumer.createConsumer(QUEUE);
        producer.send(sessionProducer.createMessage(true));
        blockConsumers();
        sessionProducer.commit();
        sessionConsumer.start();
        awaitBlocking();
        // this shouldn't lock
        producer.send(sessionProducer.createMessage(true));
        sessionProducer.commit();
        // These three operations should finish without the test hanging
        queue.getMessagesAdded();
        queue.getMessageCount();
        releaseConsumers();
        // a rollback to make sure everything will be reset on the deliveries
        // and that both consumers will receive each a message
        // this is to guarantee the server will have both consumers regsitered
        sessionConsumer.rollback();
        // a flush to guarantee any pending task is finished on flushing out delivery and pending msgs
        queue.flushExecutor();
        Wait.waitFor(() -> getMessageCount(queue) == 2);
        Wait.assertEquals(2, queue::getMessageCount);
        Wait.assertEquals(2, queue::getMessagesAdded);
        ClientMessage msg = consumer.receive(5000);
        Assert.assertNotNull(msg);
        msg.acknowledge();
        msg = consumer.receive(5000);
        Assert.assertNotNull(msg);
        msg.acknowledge();
        sessionProducer.commit();
        sessionConsumer.commit();
        sessionProducer.close();
        sessionConsumer.close();
    } finally {
        releaseConsumers();
    }
}
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) 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