Search in sources :

Example 41 with ClientProducer

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

the class DeliveryOrderTest method testMultipleConsumersMessageOrder.

@Test
public void testMultipleConsumersMessageOrder() throws Exception {
    ClientSession sendSession = cf.createSession(false, true, true);
    ClientSession recSession = cf.createSession(false, true, true);
    sendSession.createQueue(addressA, queueA, false);
    int numReceivers = 100;
    AtomicInteger count = new AtomicInteger(0);
    int numMessage = 10000;
    ClientConsumer[] clientConsumers = new ClientConsumer[numReceivers];
    Receiver[] receivers = new Receiver[numReceivers];
    CountDownLatch latch = new CountDownLatch(numMessage);
    for (int i = 0; i < numReceivers; i++) {
        clientConsumers[i] = recSession.createConsumer(queueA);
        receivers[i] = new Receiver(latch);
        clientConsumers[i].setMessageHandler(receivers[i]);
    }
    recSession.start();
    ClientProducer clientProducer = sendSession.createProducer(addressA);
    for (int i = 0; i < numMessage; i++) {
        ClientMessage cm = sendSession.createMessage(false);
        cm.getBodyBuffer().writeInt(count.getAndIncrement());
        clientProducer.send(cm);
    }
    Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
    for (Receiver receiver : receivers) {
        Assert.assertFalse("" + receiver.lastMessage, receiver.failed);
    }
    sendSession.close();
    recSession.close();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) CountDownLatch(java.util.concurrent.CountDownLatch) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 42 with ClientProducer

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

the class ExpireTestOnRestartTest method testRestartWithExpire.

// The biggest problem on this test was the exceptions that happened. I couldn't find any wrong state beyond the exceptions
@Test
public void testRestartWithExpire() throws Exception {
    int NUMBER_OF_EXPIRED_MESSAGES = 1000;
    ServerLocator locator = createInVMNonHALocator();
    locator.setBlockOnDurableSend(false);
    ClientSessionFactory factory = locator.createSessionFactory();
    ClientSession session = factory.createSession(true, true);
    session.createQueue("test", "test", true);
    session.createQueue("exp", "exp", true);
    ClientProducer prod = session.createProducer("test");
    for (int i = 0; i < 10; i++) {
        ClientMessage message = session.createMessage(true);
        message.getBodyBuffer().writeBytes(new byte[1024 * 10]);
        prod.send(message);
    }
    for (int i = 0; i < NUMBER_OF_EXPIRED_MESSAGES; i++) {
        ClientMessage message = session.createMessage(true);
        message.putIntProperty("i", i);
        message.getBodyBuffer().writeBytes(new byte[1024 * 10]);
        message.setExpiration(System.currentTimeMillis() + 5000);
        prod.send(message);
    }
    session.commit();
    session.close();
    server.stop();
    server.getConfiguration().setMessageExpiryScanPeriod(1);
    // enough time for expiration of the messages
    Thread.sleep(5500);
    server.start();
    Queue queue = server.locateQueue(SimpleString.toSimpleString("test"));
    factory = locator.createSessionFactory();
    session = factory.createSession(false, false);
    ClientConsumer cons = session.createConsumer("test");
    session.start();
    for (int i = 0; i < 10; i++) {
        ClientMessage msg = cons.receive(5000);
        assertNotNull(msg);
        msg.acknowledge();
    }
    assertNull(cons.receiveImmediate());
    cons.close();
    long timeout = System.currentTimeMillis() + 60000;
    while (queue.getPageSubscription().getPagingStore().isPaging() && timeout > System.currentTimeMillis()) {
        Thread.sleep(1);
    }
    assertFalse(queue.getPageSubscription().getPagingStore().isPaging());
    cons = session.createConsumer("exp");
    for (int i = 0; i < NUMBER_OF_EXPIRED_MESSAGES; i++) {
        ClientMessage msg = cons.receive(5000);
        assertNotNull(msg);
        msg.acknowledge();
    }
    session.commit();
    int extras = 0;
    ClientMessage msg;
    while ((msg = cons.receiveImmediate()) != null) {
        System.out.println(msg);
        extras++;
    }
    assertEquals("Received extra messages on expire address", 0, extras);
    session.commit();
    session.close();
    locator.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) Queue(org.apache.activemq.artemis.core.server.Queue) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 43 with ClientProducer

use of org.apache.activemq.artemis.api.core.client.ClientProducer 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 44 with ClientProducer

use of org.apache.activemq.artemis.api.core.client.ClientProducer 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 45 with ClientProducer

use of org.apache.activemq.artemis.api.core.client.ClientProducer 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)

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