Search in sources :

Example 76 with ActiveMQConnectionFactory

use of org.apache.activemq.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class ConfigTest method testConnectorConfig.

@Test
public void testConnectorConfig() throws Exception {
    File journalFile = new File(JOURNAL_ROOT + "testMemoryConfig");
    recursiveDelete(journalFile);
    File derbyFile = new File(DERBY_ROOT + "testMemoryConfig");
    recursiveDelete(derbyFile);
    final int MAX_PRODUCERS = 5;
    final int MAX_CONSUMERS = 10;
    BrokerService broker = createBroker(new FileSystemResource(CONF_ROOT + "connector-properties.xml"));
    broker.start();
    try {
        assertEquals(broker.getTransportConnectorByScheme("tcp").getMaximumProducersAllowedPerConnection(), MAX_PRODUCERS);
        assertEquals(broker.getTransportConnectorByScheme("tcp").getMaximumConsumersAllowedPerConnection(), MAX_CONSUMERS);
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61631");
        javax.jms.Connection connection = activeMQConnectionFactory.createConnection();
        connection.start();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic("test.foo");
        for (int i = 0; i < MAX_PRODUCERS; i++) {
            session.createProducer(topic);
        }
        try {
            session.createProducer(topic);
            fail("Should have got an exception on exceeding MAX_PRODUCERS");
        } catch (JMSException expected) {
        }
        try {
            for (int i = 0; i < (MAX_CONSUMERS + 1); i++) {
                MessageConsumer consumer = session.createConsumer(topic);
                assertNotNull(consumer);
            }
            fail("Should have caught an exception");
        } catch (JMSException e) {
        }
        LOG.info("Success");
    } finally {
        broker.stop();
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) JMSException(javax.jms.JMSException) FileSystemResource(org.springframework.core.io.FileSystemResource) Topic(javax.jms.Topic) ActiveMQTopic(org.apache.activemq.command.ActiveMQTopic) File(java.io.File) BrokerService(org.apache.activemq.broker.BrokerService) Session(javax.jms.Session) Test(org.junit.Test)

Example 77 with ActiveMQConnectionFactory

use of org.apache.activemq.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class BrokerNetworkWithStuckMessagesTest method browseQueueWithJms.

@SuppressWarnings("unused")
private Object[] browseQueueWithJms(BrokerService broker) throws Exception {
    Object[] messages = null;
    Connection connection = null;
    Session session = null;
    try {
        URI brokerUri = connector.getUri();
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUri.toString());
        connection = connectionFactory.createConnection();
        connection.start();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Queue destination = session.createQueue(queueName);
        QueueBrowser browser = session.createBrowser(destination);
        List<Message> list = new ArrayList<>();
        for (Enumeration<Message> enumn = browser.getEnumeration(); enumn.hasMoreElements(); ) {
            list.add(enumn.nextElement());
        }
        messages = list.toArray();
    } finally {
        if (session != null) {
            session.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
    LOG.info("+Browsed with JMS: " + messages.length);
    return messages;
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ActiveMQMessage(org.apache.activemq.command.ActiveMQMessage) Message(org.apache.activemq.command.Message) ActiveMQTextMessage(org.apache.activemq.command.ActiveMQTextMessage) StubConnection(org.apache.activemq.broker.StubConnection) Connection(javax.jms.Connection) ArrayList(java.util.ArrayList) URI(java.net.URI) Queue(javax.jms.Queue) QueueBrowser(javax.jms.QueueBrowser) Session(javax.jms.Session)

Example 78 with ActiveMQConnectionFactory

use of org.apache.activemq.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class NetworkConnectionsTest method testNetworkConnectionReAddURI.

@Test
public void testNetworkConnectionReAddURI() throws Exception {
    LOG.info("testNetworkConnectionReAddURI is starting...");
    LOG.info("Adding network connector 'NC1'...");
    NetworkConnector nc = localBroker.addNetworkConnector("static:(" + REMOTE_BROKER_TRANSPORT_URI + ")");
    nc.setName("NC1");
    nc.start();
    assertTrue(nc.isStarted());
    LOG.info("Looking up network connector by name...");
    NetworkConnector nc1 = localBroker.getNetworkConnectorByName("NC1");
    assertNotNull("Should find network connector 'NC1'", nc1);
    assertTrue(nc1.isStarted());
    assertEquals(nc, nc1);
    LOG.info("Setting up producer and consumer...");
    ActiveMQQueue destination = new ActiveMQQueue(DESTINATION_NAME);
    ActiveMQConnectionFactory localFactory = new ActiveMQConnectionFactory(LOCAL_BROKER_TRANSPORT_URI);
    Connection localConnection = localFactory.createConnection();
    localConnection.start();
    Session localSession = localConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageProducer localProducer = localSession.createProducer(destination);
    ActiveMQConnectionFactory remoteFactory = new ActiveMQConnectionFactory(REMOTE_BROKER_TRANSPORT_URI);
    Connection remoteConnection = remoteFactory.createConnection();
    remoteConnection.start();
    Session remoteSession = remoteConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer remoteConsumer = remoteSession.createConsumer(destination);
    Message message = localSession.createTextMessage("test");
    localProducer.send(message);
    LOG.info("Testing initial network connection...");
    message = remoteConsumer.receive(10000);
    assertNotNull(message);
    LOG.info("Stopping network connector 'NC1'...");
    nc.stop();
    assertFalse(nc.isStarted());
    LOG.info("Removing network connector...");
    assertTrue(localBroker.removeNetworkConnector(nc));
    nc1 = localBroker.getNetworkConnectorByName("NC1");
    assertNull("Should not find network connector 'NC1'", nc1);
    LOG.info("Re-adding network connector 'NC2'...");
    nc = localBroker.addNetworkConnector("static:(" + REMOTE_BROKER_TRANSPORT_URI + ")");
    nc.setName("NC2");
    nc.start();
    assertTrue(nc.isStarted());
    LOG.info("Looking up network connector by name...");
    NetworkConnector nc2 = localBroker.getNetworkConnectorByName("NC2");
    assertNotNull(nc2);
    assertTrue(nc2.isStarted());
    assertEquals(nc, nc2);
    LOG.info("Testing re-added network connection...");
    message = localSession.createTextMessage("test");
    localProducer.send(message);
    message = remoteConsumer.receive(10000);
    assertNotNull(message);
    LOG.info("Stopping network connector...");
    nc.stop();
    assertFalse(nc.isStarted());
    LOG.info("Removing network connection 'NC2'");
    assertTrue(localBroker.removeNetworkConnector(nc));
    nc2 = localBroker.getNetworkConnectorByName("NC2");
    assertNull("Should not find network connector 'NC2'", nc2);
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Message(javax.jms.Message) Connection(javax.jms.Connection) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) Test(org.junit.Test)

Example 79 with ActiveMQConnectionFactory

use of org.apache.activemq.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class ActiveMQAdmin method createConnectionFactory.

@Override
public void createConnectionFactory(String name) {
    try {
        final ConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost");
        ((ActiveMQConnectionFactory) factory).setNestedMapAndListEnabled(false);
        context.bind(name, factory);
    } catch (NamingException e) {
        throw new RuntimeException(e);
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) NamingException(javax.naming.NamingException)

Example 80 with ActiveMQConnectionFactory

use of org.apache.activemq.ActiveMQConnectionFactory in project activemq-artemis by apache.

the class ConfigUsingDestinationOptionsTest method testValidSelectorConfig.

@Test(timeout = 60000)
public void testValidSelectorConfig() throws JMSException {
    ActiveMQQueue queue = new ActiveMQQueue("TEST.FOO?consumer.selector=test=1");
    ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost");
    Connection conn = factory.createConnection();
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ActiveMQMessageConsumer cons;
    // JMS selector should be priority
    cons = (ActiveMQMessageConsumer) sess.createConsumer(queue, "test=2");
    assertEquals("test=2", cons.getMessageSelector());
    // Test setting using JMS destinations
    cons = (ActiveMQMessageConsumer) sess.createConsumer(queue);
    assertEquals("test=1", cons.getMessageSelector());
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) ActiveMQMessageConsumer(org.apache.activemq.ActiveMQMessageConsumer) Connection(javax.jms.Connection) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) Session(javax.jms.Session) Test(org.junit.Test)

Aggregations

ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)436 Session (javax.jms.Session)148 Connection (javax.jms.Connection)144 Test (org.junit.Test)107 MessageConsumer (javax.jms.MessageConsumer)103 TextMessage (javax.jms.TextMessage)81 MessageProducer (javax.jms.MessageProducer)79 Message (javax.jms.Message)74 Queue (javax.jms.Queue)66 JMSException (javax.jms.JMSException)65 ActiveMQConnection (org.apache.activemq.ActiveMQConnection)51 ConnectionFactory (javax.jms.ConnectionFactory)48 Destination (javax.jms.Destination)42 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)36 BrokerService (org.apache.activemq.broker.BrokerService)32 CountDownLatch (java.util.concurrent.CountDownLatch)28 Before (org.junit.Before)27 MessageListener (javax.jms.MessageListener)26 CamelContext (org.apache.camel.CamelContext)26 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)23