Search in sources :

Example 41 with ClientSessionFactory

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

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

Example 43 with ClientSessionFactory

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

the class HangConsumerTest method testForceDuplicationOnBindings.

/**
 * This would force a journal duplication on bindings even with the scenario that generated fixed,
 * the server shouldn't hold of from starting
 *
 * @throws Exception
 */
@Test
public void testForceDuplicationOnBindings() throws Exception {
    queue = server.createQueue(QUEUE, RoutingType.ANYCAST, QUEUE, null, true, false);
    ClientSessionFactory factory = locator.createSessionFactory();
    ClientSession session = factory.createSession(false, false, false);
    ClientProducer producer = session.createProducer(QUEUE);
    producer.send(session.createMessage(true));
    session.commit();
    long queueID = server.getStorageManager().generateID();
    long txID = server.getStorageManager().generateID();
    // Forcing a situation where the server would unexpectedly create a duplicated queue. The server should still start normally
    LocalQueueBinding newBinding = new LocalQueueBinding(QUEUE, new QueueImpl(queueID, QUEUE, QUEUE, null, null, true, false, false, null, null, null, null, null, null, null), server.getNodeID());
    server.getStorageManager().addQueueBinding(txID, newBinding);
    server.getStorageManager().commitBindings(txID);
    server.stop();
    // a duplicate binding would impede the server from starting
    server.start();
    waitForServerToStart(server);
    server.stop();
}
Also used : LocalQueueBinding(org.apache.activemq.artemis.core.postoffice.impl.LocalQueueBinding) 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) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) Test(org.junit.Test)

Example 44 with ClientSessionFactory

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

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

the class InVMNonPersistentMessageBufferTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    server = createServer(isPersistent(), isNetty());
    server.start();
    ServerLocator locator = createFactory();
    ClientSessionFactory cf = createSessionFactory(locator);
    session = cf.createSession();
    session.createQueue(InVMNonPersistentMessageBufferTest.address, RoutingType.ANYCAST, InVMNonPersistentMessageBufferTest.queueName);
    producer = session.createProducer(InVMNonPersistentMessageBufferTest.address);
    consumer = session.createConsumer(InVMNonPersistentMessageBufferTest.queueName);
    session.start();
}
Also used : ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Before(org.junit.Before)

Aggregations

ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)601 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)518 Test (org.junit.Test)465 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)395 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)372 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)350 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)277 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)254 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)206 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)108 Configuration (org.apache.activemq.artemis.core.config.Configuration)78 AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)65 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)63 CountDownLatch (java.util.concurrent.CountDownLatch)54 Queue (org.apache.activemq.artemis.core.server.Queue)49 ArrayList (java.util.ArrayList)44 HashMap (java.util.HashMap)43 HashSet (java.util.HashSet)42 ActiveMQJAASSecurityManager (org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager)42 Message (org.apache.activemq.artemis.api.core.Message)41