Search in sources :

Example 46 with ClientProducer

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

the class HangConsumerTest method testHangDuplicateQueues.

/**
 * This would recreate the scenario where a queue was duplicated
 *
 * @throws Exception
 */
@Test
public void testHangDuplicateQueues() throws Exception {
    final Semaphore blocked = new Semaphore(1);
    final CountDownLatch latchDelete = new CountDownLatch(1);
    class MyQueueWithBlocking extends QueueImpl {

        /**
         * @param id
         * @param address
         * @param name
         * @param filter
         * @param pageSubscription
         * @param durable
         * @param temporary
         * @param scheduledExecutor
         * @param postOffice
         * @param storageManager
         * @param addressSettingsRepository
         * @param executor
         */
        MyQueueWithBlocking(final long id, final SimpleString address, final SimpleString name, final Filter filter, final SimpleString user, final PageSubscription pageSubscription, final boolean durable, final boolean temporary, final boolean autoCreated, final RoutingType deliveryMode, final Integer maxConsumers, final Boolean purgeOnNoConsumers, final ScheduledExecutorService scheduledExecutor, final PostOffice postOffice, final StorageManager storageManager, final HierarchicalRepository<AddressSettings> addressSettingsRepository, final ArtemisExecutor executor, final ActiveMQServer server) {
            super(id, address, name, filter, pageSubscription, user, durable, temporary, autoCreated, deliveryMode, maxConsumers, purgeOnNoConsumers, scheduledExecutor, postOffice, storageManager, addressSettingsRepository, executor, server, null);
        }

        @Override
        public synchronized int deleteMatchingReferences(final int flushLimit, final Filter filter) throws Exception {
            latchDelete.countDown();
            blocked.acquire();
            blocked.release();
            return super.deleteMatchingReferences(flushLimit, filter);
        }

        @Override
        public void deliverScheduledMessages() {
        }
    }
    class LocalFactory extends QueueFactoryImpl {

        LocalFactory(final ExecutorFactory executorFactory, final ScheduledExecutorService scheduledExecutor, final HierarchicalRepository<AddressSettings> addressSettingsRepository, final StorageManager storageManager, final ActiveMQServer server) {
            super(executorFactory, scheduledExecutor, addressSettingsRepository, storageManager, server);
        }

        @Override
        public Queue createQueueWith(final QueueConfig config) {
            queue = new MyQueueWithBlocking(config.id(), config.address(), config.name(), config.filter(), config.user(), config.pageSubscription(), config.isDurable(), config.isTemporary(), config.isAutoCreated(), config.deliveryMode(), config.maxConsumers(), config.isPurgeOnNoConsumers(), scheduledExecutor, postOffice, storageManager, addressSettingsRepository, executorFactory.getExecutor(), server);
            return queue;
        }

        @Deprecated
        @Override
        public Queue createQueue(final long persistenceID, final SimpleString address, final SimpleString name, final Filter filter, final PageSubscription pageSubscription, final SimpleString user, final boolean durable, final boolean temporary, final boolean autoCreated) {
            queue = new MyQueueWithBlocking(persistenceID, address, name, filter, user, pageSubscription, durable, temporary, autoCreated, RoutingType.MULTICAST, null, null, scheduledExecutor, postOffice, storageManager, addressSettingsRepository, executorFactory.getExecutor(), server);
            return queue;
        }
    }
    LocalFactory queueFactory = new LocalFactory(server.getExecutorFactory(), server.getScheduledPool(), server.getAddressSettingsRepository(), server.getStorageManager(), server);
    queueFactory.setPostOffice(server.getPostOffice());
    ((ActiveMQServerImpl) server).replaceQueueFactory(queueFactory);
    queue = server.createQueue(QUEUE, RoutingType.ANYCAST, QUEUE, null, true, false);
    blocked.acquire();
    ClientSessionFactory factory = locator.createSessionFactory();
    ClientSession session = factory.createSession(false, false, false);
    ClientProducer producer = session.createProducer(QUEUE);
    producer.send(session.createMessage(true));
    session.commit();
    Thread tDelete = new Thread() {

        @Override
        public void run() {
            try {
                server.destroyQueue(QUEUE);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    tDelete.start();
    Assert.assertTrue(latchDelete.await(10, TimeUnit.SECONDS));
    try {
        server.createQueue(QUEUE, RoutingType.ANYCAST, QUEUE, null, true, false);
    } catch (Exception expected) {
    }
    blocked.release();
    server.stop();
    tDelete.join();
    session.close();
    // a duplicate binding would impede the server from starting
    server.start();
    waitForServerToStart(server);
    server.stop();
}
Also used : HierarchicalRepository(org.apache.activemq.artemis.core.settings.HierarchicalRepository) QueueConfig(org.apache.activemq.artemis.core.server.QueueConfig) ArtemisExecutor(org.apache.activemq.artemis.utils.actors.ArtemisExecutor) StorageManager(org.apache.activemq.artemis.core.persistence.StorageManager) Semaphore(java.util.concurrent.Semaphore) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) RoutingType(org.apache.activemq.artemis.api.core.RoutingType) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) PostOffice(org.apache.activemq.artemis.core.postoffice.PostOffice) PageSubscription(org.apache.activemq.artemis.core.paging.cursor.PageSubscription) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQServerImpl(org.apache.activemq.artemis.core.server.impl.ActiveMQServerImpl) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) QueueFactoryImpl(org.apache.activemq.artemis.core.server.impl.QueueFactoryImpl) Filter(org.apache.activemq.artemis.core.filter.Filter) ExecutorFactory(org.apache.activemq.artemis.utils.ExecutorFactory) Test(org.junit.Test)

Example 47 with ClientProducer

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

the class MessageHandlerTest method testSetUnsetResetMessageHandler.

@Test
public void testSetUnsetResetMessageHandler() throws Exception {
    final ClientSession session = sf.createSession(false, true, true);
    session.createQueue(QUEUE, QUEUE, null, false);
    ClientProducer producer = session.createProducer(QUEUE);
    final int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = createTextMessage(session, "m" + i);
        message.putIntProperty(new SimpleString("i"), i);
        producer.send(message);
    }
    final ClientConsumer consumer = session.createConsumer(QUEUE);
    session.start();
    CountDownLatch latch = new CountDownLatch(50);
    class MyHandler implements MessageHandler {

        int messageReceived = 0;

        boolean failed;

        boolean started = true;

        private final CountDownLatch latch;

        MyHandler(final CountDownLatch latch) {
            this.latch = latch;
        }

        @Override
        public void onMessage(final ClientMessage message) {
            try {
                if (!started) {
                    failed = true;
                }
                messageReceived++;
                latch.countDown();
                if (latch.getCount() == 0) {
                    message.acknowledge();
                    started = false;
                    consumer.setMessageHandler(null);
                }
            } catch (Exception e) {
            }
        }
    }
    MyHandler handler = new MyHandler(latch);
    consumer.setMessageHandler(handler);
    waitForLatch(latch);
    Thread.sleep(100);
    Assert.assertFalse(handler.failed);
    // Make sure no exceptions were thrown from onMessage
    Assert.assertNull(consumer.getLastException());
    consumer.setMessageHandler(null);
    ClientMessage cm = consumer.receiveImmediate();
    Assert.assertNotNull(cm);
    latch = new CountDownLatch(49);
    handler = new MyHandler(latch);
    consumer.setMessageHandler(handler);
    session.start();
    Assert.assertTrue("message received " + handler.messageReceived, latch.await(5, TimeUnit.SECONDS));
    Thread.sleep(100);
    Assert.assertFalse(handler.failed);
    Assert.assertNull(consumer.getLastException());
    session.close();
}
Also used : MessageHandler(org.apache.activemq.artemis.api.core.client.MessageHandler) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) 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 48 with ClientProducer

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

the class MessageHandlerTest method testSetUnsetMessageHandler.

@Test
public void testSetUnsetMessageHandler() throws Exception {
    final ClientSession session = sf.createSession(false, true, true);
    session.createQueue(QUEUE, QUEUE, null, false);
    ClientProducer producer = session.createProducer(QUEUE);
    final int numMessages = 100;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = createTextMessage(session, "m" + i);
        message.putIntProperty(new SimpleString("i"), i);
        producer.send(message);
    }
    final ClientConsumer consumer = session.createConsumer(QUEUE);
    session.start();
    CountDownLatch latch = new CountDownLatch(50);
    class MyHandler implements MessageHandler {

        int messageReceived = 0;

        boolean failed;

        boolean started = true;

        private final CountDownLatch latch;

        MyHandler(final CountDownLatch latch) {
            this.latch = latch;
        }

        @Override
        public void onMessage(final ClientMessage message) {
            try {
                if (!started) {
                    failed = true;
                }
                messageReceived++;
                latch.countDown();
                if (latch.getCount() == 0) {
                    message.acknowledge();
                    started = false;
                    consumer.setMessageHandler(null);
                }
            } catch (Exception e) {
            }
        }
    }
    MyHandler handler = new MyHandler(latch);
    consumer.setMessageHandler(handler);
    waitForLatch(latch);
    Thread.sleep(100);
    Assert.assertFalse(handler.failed);
    // Make sure no exceptions were thrown from onMessage
    Assert.assertNull(consumer.getLastException());
    consumer.setMessageHandler(null);
    ClientMessage cm = consumer.receiveImmediate();
    Assert.assertNotNull(cm);
    session.close();
}
Also used : MessageHandler(org.apache.activemq.artemis.api.core.client.MessageHandler) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) 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 49 with ClientProducer

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

the class MessageRateTest method testConsumeRate.

@Test
public void testConsumeRate() throws Exception {
    ActiveMQServer server = createServer(false);
    server.start();
    locator.setConsumerMaxRate(10);
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, true);
    session.createQueue(ADDRESS, ADDRESS, true);
    ClientProducer producer = session.createProducer(ADDRESS);
    for (int i = 0; i < 12; i++) {
        producer.send(session.createMessage(false));
    }
    session.start();
    ClientConsumer consumer = session.createConsumer(ADDRESS);
    long start = System.currentTimeMillis();
    for (int i = 0; i < 12; i++) {
        consumer.receive(1000);
    }
    long end = System.currentTimeMillis();
    Assert.assertTrue("TotalTime = " + (end - start), end - start >= 1000);
    session.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) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 50 with ClientProducer

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

the class HeuristicXATest method internalTest.

private void internalTest(final boolean isCommit) throws Exception {
    Configuration configuration = createDefaultInVMConfig().setJMXManagementEnabled(true);
    ActiveMQServer server = createServer(false, configuration);
    server.setMBeanServer(mbeanServer);
    server.start();
    Xid xid = newXID();
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(true, false, false);
    session.createQueue(ADDRESS, ADDRESS, true);
    session.start(xid, XAResource.TMNOFLAGS);
    ClientProducer producer = session.createProducer(ADDRESS);
    ClientMessage msg = session.createMessage(true);
    msg.getBodyBuffer().writeString(body);
    producer.send(msg);
    session.end(xid, XAResource.TMSUCCESS);
    session.prepare(xid);
    session.close();
    ActiveMQServerControl jmxServer = ManagementControlHelper.createActiveMQServerControl(mbeanServer);
    String[] preparedTransactions = jmxServer.listPreparedTransactions();
    Assert.assertEquals(1, preparedTransactions.length);
    System.out.println(preparedTransactions[0]);
    Assert.assertEquals(0, jmxServer.listHeuristicCommittedTransactions().length);
    Assert.assertEquals(0, jmxServer.listHeuristicRolledBackTransactions().length);
    if (isCommit) {
        jmxServer.commitPreparedTransaction(XidImpl.toBase64String(xid));
    } else {
        jmxServer.rollbackPreparedTransaction(XidImpl.toBase64String(xid));
    }
    Assert.assertEquals(0, jmxServer.listPreparedTransactions().length);
    if (isCommit) {
        Assert.assertEquals(1, jmxServer.listHeuristicCommittedTransactions().length);
        Assert.assertEquals(0, jmxServer.listHeuristicRolledBackTransactions().length);
    } else {
        Assert.assertEquals(0, jmxServer.listHeuristicCommittedTransactions().length);
        Assert.assertEquals(1, jmxServer.listHeuristicRolledBackTransactions().length);
    }
    if (isCommit) {
        Assert.assertEquals(1, getMessageCount(((Queue) server.getPostOffice().getBinding(ADDRESS).getBindable())));
        session = sf.createSession(false, false, false);
        session.start();
        ClientConsumer consumer = session.createConsumer(ADDRESS);
        msg = consumer.receive(1000);
        Assert.assertNotNull(msg);
        msg.acknowledge();
        Assert.assertEquals(body, msg.getBodyBuffer().readString());
        session.commit();
        session.close();
    }
    Assert.assertEquals(0, getMessageCount(((Queue) server.getPostOffice().getBinding(ADDRESS).getBindable())));
}
Also used : ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) Xid(javax.transaction.xa.Xid) ActiveMQServerControl(org.apache.activemq.artemis.api.core.management.ActiveMQServerControl) Configuration(org.apache.activemq.artemis.core.config.Configuration) 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) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer)

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