Search in sources :

Example 51 with AddressSettings

use of org.apache.activemq.artemis.core.settings.impl.AddressSettings in project activemq-artemis by apache.

the class ConcurrentDeliveryCancelTest method testConcurrentCancels.

@Test
@BMRules(rules = { @BMRule(name = "enterCancel-holdThere", targetClass = "org.apache.activemq.artemis.core.server.impl.ServerConsumerImpl", targetMethod = "close", targetLocation = "ENTRY", action = "org.apache.activemq.artemis.tests.extras.byteman.ConcurrentDeliveryCancelTest.enterCancel();") })
public void testConcurrentCancels() throws Exception {
    System.out.println(server.getConfiguration().getJournalLocation().toString());
    server.getAddressSettingsRepository().clear();
    AddressSettings settings = new AddressSettings();
    settings.setMaxDeliveryAttempts(-1);
    server.getAddressSettingsRepository().addMatch("#", settings);
    ActiveMQConnectionFactory cf = ActiveMQJMSClient.createConnectionFactory("tcp://localhost:61616", "test");
    cf.setReconnectAttempts(0);
    cf.setRetryInterval(10);
    System.out.println(".....");
    for (ServerSession srvSess : server.getSessions()) {
        System.out.println(srvSess);
    }
    String queueName = RandomUtil.randomString();
    Queue queue = createQueue(queueName);
    int numberOfMessages = 10000;
    {
        Connection connection = cf.createConnection();
        Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
        MessageProducer producer = session.createProducer(queue);
        for (int i = 0; i < numberOfMessages; i++) {
            TextMessage msg = session.createTextMessage("message " + i);
            msg.setIntProperty("i", i);
            producer.send(msg);
        }
        session.commit();
        connection.close();
    }
    for (int i = 0; i < 100; i++) {
        XAConnectionFactory xacf = ActiveMQJMSClient.createConnectionFactory("tcp://localhost:61616", "test");
        final XAConnection connection = xacf.createXAConnection();
        final XASession theSession = connection.createXASession();
        ((ActiveMQSession) theSession).getCoreSession().addMetaData("theSession", "true");
        connection.start();
        final MessageConsumer consumer = theSession.createConsumer(queue);
        XidImpl xid = newXID();
        theSession.getXAResource().start(xid, XAResource.TMNOFLAGS);
        // I'm setting a small timeout just because I'm lazy to call end myself
        theSession.getXAResource().setTransactionTimeout(1);
        for (int msg = 0; msg < 11; msg++) {
            Assert.assertNotNull(consumer.receiveNoWait());
        }
        System.out.println(".....");
        final List<ServerSession> serverSessions = new LinkedList<>();
        // We will force now the failure simultaneously from several places
        for (ServerSession srvSess : server.getSessions()) {
            if (srvSess.getMetaData("theSession") != null) {
                System.out.println(srvSess);
                serverSessions.add(srvSess);
            }
        }
        // from Transactional reaper
        resetLatches(2);
        List<Thread> threads = new LinkedList<>();
        threads.add(new Thread("ConsumerCloser") {

            @Override
            public void run() {
                try {
                    System.out.println(Thread.currentThread().getName() + " closing consumer");
                    consumer.close();
                    System.out.println(Thread.currentThread().getName() + " closed consumer");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        threads.add(new Thread("SessionCloser") {

            @Override
            public void run() {
                for (ServerSession sess : serverSessions) {
                    System.out.println("Thread " + Thread.currentThread().getName() + " starting");
                    try {
                        // A session.close could sneak in through failover or some other scenarios.
                        // a call to RemotingConnection.fail wasn't replicating the issue.
                        // I needed to call Session.close() directly to replicate what was happening in production
                        sess.close(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println("Thread " + Thread.currentThread().getName() + " done");
                }
            }
        });
        for (Thread t : threads) {
            t.start();
        }
        Assert.assertTrue(latchEnter.await(10, TimeUnit.MINUTES));
        latchFlag.countDown();
        for (Thread t : threads) {
            t.join(5000);
            Assert.assertFalse(t.isAlive());
        }
        connection.close();
    }
    Connection connection = cf.createConnection();
    try {
        connection.setClientID("myID");
        // I am too lazy to call end on all the transactions
        Thread.sleep(5000);
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageConsumer consumer = session.createConsumer(queue);
        HashMap<Integer, AtomicInteger> mapCount = new HashMap<>();
        while (true) {
            TextMessage message = (TextMessage) consumer.receiveNoWait();
            if (message == null) {
                break;
            }
            Integer value = message.getIntProperty("i");
            AtomicInteger count = mapCount.get(value);
            if (count == null) {
                count = new AtomicInteger(0);
                mapCount.put(message.getIntProperty("i"), count);
            }
            count.incrementAndGet();
        }
        boolean failed = false;
        for (int i = 0; i < numberOfMessages; i++) {
            AtomicInteger count = mapCount.get(i);
            if (count == null) {
                System.out.println("Message " + i + " not received");
                failed = true;
            } else if (count.get() > 1) {
                System.out.println("Message " + i + " received " + count.get() + " times");
                failed = true;
            }
        }
        Assert.assertFalse("test failed, look at the system.out of the test for more information", failed);
    } finally {
        connection.close();
    }
}
Also used : HashMap(java.util.HashMap) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) XASession(javax.jms.XASession) Queue(javax.jms.Queue) XAConnection(javax.jms.XAConnection) AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) MessageConsumer(javax.jms.MessageConsumer) XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) LinkedList(java.util.LinkedList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) XAConnectionFactory(javax.jms.XAConnectionFactory) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) XASession(javax.jms.XASession) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ServerSession(org.apache.activemq.artemis.core.server.ServerSession) Test(org.junit.Test) BMRules(org.jboss.byteman.contrib.bmunit.BMRules)

Example 52 with AddressSettings

use of org.apache.activemq.artemis.core.settings.impl.AddressSettings in project activemq-artemis by apache.

the class DivertTest method testSingleDivertWithExpiry.

@Test
public void testSingleDivertWithExpiry() throws Exception {
    final String testAddress = "testAddress";
    final String forwardAddress = "forwardAddress";
    final String expiryAddress = "expiryAddress";
    AddressSettings expirySettings = new AddressSettings().setExpiryAddress(new SimpleString(expiryAddress));
    DivertConfiguration divertConf = new DivertConfiguration().setName("divert1").setRoutingName("divert1").setAddress(testAddress).setForwardingAddress(forwardAddress);
    Configuration config = createDefaultInVMConfig().addDivertConfiguration(divertConf).clearAddressesSettings().addAddressesSetting("#", expirySettings);
    ActiveMQServer server = addServer(ActiveMQServers.newActiveMQServer(config, true));
    server.start();
    ServerLocator locator = createInVMNonHALocator();
    ClientSessionFactory sf = createSessionFactory(locator);
    ClientSession session = sf.createSession(false, false, false);
    final SimpleString queueName1 = new SimpleString("queue1");
    final SimpleString queueName2 = new SimpleString("queue2");
    session.createQueue(new SimpleString(forwardAddress), RoutingType.MULTICAST, queueName1, null, true);
    session.createQueue(new SimpleString(testAddress), RoutingType.MULTICAST, queueName2, null, true);
    session.createQueue(new SimpleString(expiryAddress), RoutingType.MULTICAST, new SimpleString(expiryAddress), null, true);
    session.start();
    ClientProducer producer = session.createProducer(new SimpleString(testAddress));
    ClientConsumer consumer1 = session.createConsumer(queueName1);
    ClientConsumer consumer2 = session.createConsumer(queueName2);
    final int numMessages = 1;
    final SimpleString propKey = new SimpleString("testkey");
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session.createMessage(true);
        message.putIntProperty(propKey, i);
        message.setExpiration(System.currentTimeMillis() + 1000);
        producer.send(message);
    }
    session.commit();
    // this context is validating if these messages are routed correctly
    {
        int count1 = 0;
        ClientMessage message = null;
        while ((message = consumer1.receiveImmediate()) != null) {
            message.acknowledge();
            count1++;
        }
        int count2 = 0;
        while ((message = consumer2.receiveImmediate()) != null) {
            message.acknowledge();
            count2++;
        }
        assertEquals(1, count1);
        assertEquals(1, count2);
        session.rollback();
    }
    Thread.sleep(2000);
    // it must been expired by now
    assertNull(consumer1.receiveImmediate());
    // it must been expired by now
    assertNull(consumer2.receiveImmediate());
    int countOriginal1 = 0;
    int countOriginal2 = 0;
    ClientConsumer consumerExpiry = session.createConsumer(expiryAddress);
    for (int i = 0; i < numMessages * 2; i++) {
        ClientMessage message = consumerExpiry.receive(5000);
        System.out.println("Received message " + message);
        assertNotNull(message);
        if (message.getStringProperty(Message.HDR_ORIGINAL_QUEUE).equals("divert1")) {
            countOriginal1++;
        } else if (message.getStringProperty(Message.HDR_ORIGINAL_QUEUE).equals("queue2")) {
            countOriginal2++;
        } else {
            System.out.println("message not part of any expired queue" + message);
        }
    }
    assertEquals(numMessages, countOriginal1);
    assertEquals(numMessages, countOriginal2);
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) Configuration(org.apache.activemq.artemis.core.config.Configuration) DivertConfiguration(org.apache.activemq.artemis.core.config.DivertConfiguration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) DivertConfiguration(org.apache.activemq.artemis.core.config.DivertConfiguration) 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) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 53 with AddressSettings

use of org.apache.activemq.artemis.core.settings.impl.AddressSettings in project activemq-artemis by apache.

the class JmsProducerTest method defaultAutoCreatedQueueConfigTest.

@Test
public void defaultAutoCreatedQueueConfigTest() throws Exception {
    final String queueName = "q1";
    server.getAddressSettingsRepository().addMatch(queueName, new AddressSettings().setDefaultMaxConsumers(5).setDefaultPurgeOnNoConsumers(true));
    Queue q1 = context.createQueue(queueName);
    context.createProducer().setProperty("prop1", 1).setProperty("prop2", 2).send(q1, "Text1");
    org.apache.activemq.artemis.core.server.Queue queue = server.locateQueue(SimpleString.toSimpleString(queueName));
    assertEquals(5, queue.getMaxConsumers());
    assertEquals(true, queue.isPurgeOnNoConsumers());
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Queue(javax.jms.Queue) Test(org.junit.Test)

Example 54 with AddressSettings

use of org.apache.activemq.artemis.core.settings.impl.AddressSettings in project activemq-artemis by apache.

the class AMQPToOpenwireTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    server = createServer(true, true);
    server.start();
    server.waitForActivation(10, TimeUnit.SECONDS);
    Configuration serverConfig = server.getConfiguration();
    serverConfig.getAddressesSettings().put("#", new AddressSettings().setAutoCreateQueues(false).setAutoCreateAddresses(false).setDeadLetterAddress(new SimpleString("ActiveMQ.DLQ")));
    serverConfig.setSecurityEnabled(false);
    coreQueue = new SimpleString(queueName);
    server.createQueue(coreQueue, RoutingType.ANYCAST, coreQueue, null, false, false);
    qpidfactory = new JmsConnectionFactory("amqp://localhost:61616");
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) Configuration(org.apache.activemq.artemis.core.config.Configuration) JmsConnectionFactory(org.apache.qpid.jms.JmsConnectionFactory) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Before(org.junit.Before)

Example 55 with AddressSettings

use of org.apache.activemq.artemis.core.settings.impl.AddressSettings in project activemq-artemis by apache.

the class AMQPToJMSCoreTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    server = createServer(true, true);
    server.start();
    server.waitForActivation(10, TimeUnit.SECONDS);
    Configuration serverConfig = server.getConfiguration();
    serverConfig.getAddressesSettings().put("#", new AddressSettings().setAutoCreateQueues(false).setAutoCreateAddresses(false).setDeadLetterAddress(new SimpleString("ActiveMQ.DLQ")));
    serverConfig.setSecurityEnabled(false);
    coreQueue = new SimpleString(queueName);
    server.createQueue(coreQueue, RoutingType.ANYCAST, coreQueue, null, false, false);
}
Also used : AddressSettings(org.apache.activemq.artemis.core.settings.impl.AddressSettings) Configuration(org.apache.activemq.artemis.core.config.Configuration) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Before(org.junit.Before)

Aggregations

AddressSettings (org.apache.activemq.artemis.core.settings.impl.AddressSettings)273 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)162 Test (org.junit.Test)161 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)103 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)103 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)88 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)81 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)65 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)54 Configuration (org.apache.activemq.artemis.core.config.Configuration)52 HashMap (java.util.HashMap)31 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)30 Queue (org.apache.activemq.artemis.core.server.Queue)24 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)21 Before (org.junit.Before)19 Session (javax.jms.Session)18 Message (org.apache.activemq.artemis.api.core.Message)18 ArrayList (java.util.ArrayList)15 PagingStore (org.apache.activemq.artemis.core.paging.PagingStore)15 ActiveMQBuffer (org.apache.activemq.artemis.api.core.ActiveMQBuffer)14