Search in sources :

Example 96 with ActiveMQConnectionFactory

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

the class ClusteredQueueExample method main.

public static void main(final String[] args) throws Exception {
    Connection connection0 = null;
    Connection connection1 = null;
    try {
        // Step 2. Instantiate the Queue
        Queue queue = ActiveMQJMSClient.createQueue("exampleQueue");
        // Instantiate connection towards server 0
        ConnectionFactory cf0 = new ActiveMQConnectionFactory("tcp://localhost:61616");
        // Step 5. Look-up a JMS Connection Factory object from JNDI on server 1
        ConnectionFactory cf1 = new ActiveMQConnectionFactory("tcp://localhost:61617");
        // Step 6. We create a JMS Connection connection0 which is a connection to server 0
        connection0 = cf0.createConnection();
        // Step 7. We create a JMS Connection connection1 which is a connection to server 1
        connection1 = cf1.createConnection();
        // Step 8. We create a JMS Session on server 0
        Session session0 = connection0.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // Step 9. We create a JMS Session on server 1
        Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // Step 10. We start the connections to ensure delivery occurs on them
        connection0.start();
        connection1.start();
        // Step 11. We create JMS MessageConsumer objects on server 0 and server 1
        MessageConsumer consumer0 = session0.createConsumer(queue);
        MessageConsumer consumer1 = session1.createConsumer(queue);
        Thread.sleep(1000);
        // Step 12. We create a JMS MessageProducer object on server 0
        MessageProducer producer = session0.createProducer(queue);
        // Step 13. We send some messages to server 0
        final int numMessages = 10;
        for (int i = 0; i < numMessages; i++) {
            TextMessage message = session0.createTextMessage("This is text message " + i);
            producer.send(message);
            System.out.println("Sent message: " + message.getText());
        }
        for (int i = 0; i < numMessages; i += 2) {
            TextMessage message0 = (TextMessage) consumer0.receive(5000);
            System.out.println("Got message: " + message0.getText() + " from node 0");
            TextMessage message1 = (TextMessage) consumer1.receive(5000);
            System.out.println("Got message: " + message1.getText() + " from node 1");
        }
    } finally {
        if (connection0 != null) {
            connection0.close();
        }
        if (connection1 != null) {
            connection1.close();
        }
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) MessageConsumer(javax.jms.MessageConsumer) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 97 with ActiveMQConnectionFactory

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

the class PagingTest method testPurge.

@Test
public void testPurge() throws Exception {
    clearDataRecreateServerDirs();
    Configuration config = createDefaultNettyConfig().setJournalSyncNonTransactional(false);
    server = createServer(true, config, PagingTest.PAGE_SIZE, PagingTest.PAGE_MAX);
    server.start();
    String queue = "purgeQueue";
    SimpleString ssQueue = new SimpleString(queue);
    server.addAddressInfo(new AddressInfo(ssQueue, RoutingType.ANYCAST));
    QueueImpl purgeQueue = (QueueImpl) server.createQueue(ssQueue, RoutingType.ANYCAST, ssQueue, null, true, false, 1, true, false);
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory();
    Connection connection = cf.createConnection();
    Session session = connection.createSession(true, Session.SESSION_TRANSACTED);
    javax.jms.Queue jmsQueue = session.createQueue(queue);
    MessageProducer producer = session.createProducer(jmsQueue);
    for (int i = 0; i < 100; i++) {
        producer.send(session.createTextMessage("hello" + i));
    }
    session.commit();
    Wait.assertEquals(0, purgeQueue::getMessageCount);
    Assert.assertEquals(0, purgeQueue.getPageSubscription().getPagingStore().getAddressSize());
    MessageConsumer consumer = session.createConsumer(jmsQueue);
    for (int i = 0; i < 100; i++) {
        producer.send(session.createTextMessage("hello" + i));
        if (i == 10) {
            purgeQueue.getPageSubscription().getPagingStore().startPaging();
        }
    }
    session.commit();
    consumer.close();
    Wait.assertEquals(0, purgeQueue::getMessageCount);
    Wait.assertFalse(purgeQueue.getPageSubscription()::isPaging);
    Wait.assertEquals(0, purgeQueue.getPageSubscription().getPagingStore()::getAddressSize);
    purgeQueue.getPageSubscription().getPagingStore().startPaging();
    Wait.assertTrue(purgeQueue.getPageSubscription()::isPaging);
    consumer = session.createConsumer(jmsQueue);
    for (int i = 0; i < 100; i++) {
        purgeQueue.getPageSubscription().getPagingStore().startPaging();
        Assert.assertTrue(purgeQueue.getPageSubscription().isPaging());
        producer.send(session.createTextMessage("hello" + i));
        if (i % 2 == 0) {
            session.commit();
        }
    }
    session.commit();
    connection.start();
    server.getStorageManager().getMessageJournal().scheduleCompactAndBlock(50000);
    Assert.assertNotNull(consumer.receive(5000));
    session.commit();
    consumer.close();
    Wait.assertEquals(0, purgeQueue::getMessageCount);
    Wait.assertEquals(0, purgeQueue.getPageSubscription().getPagingStore()::getAddressSize);
    Wait.assertFalse(purgeQueue.getPageSubscription()::isPaging);
    StorageManager sm = server.getStorageManager();
    for (int i = 0; i < 1000; i++) {
        long tx = sm.generateID();
        PageTransactionInfoImpl txinfo = new PageTransactionInfoImpl(tx);
        sm.storePageTransaction(tx, txinfo);
        sm.commit(tx);
        tx = sm.generateID();
        sm.updatePageTransaction(tx, txinfo, 1);
        sm.commit(tx);
    }
    server.stop();
    server.start();
    Assert.assertEquals(0, server.getPagingManager().getTransactions().size());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) DivertConfiguration(org.apache.activemq.artemis.core.config.DivertConfiguration) StoreConfiguration(org.apache.activemq.artemis.core.config.StoreConfiguration) Configuration(org.apache.activemq.artemis.core.config.Configuration) DatabaseStorageConfiguration(org.apache.activemq.artemis.core.config.storage.DatabaseStorageConfiguration) PageTransactionInfoImpl(org.apache.activemq.artemis.core.paging.impl.PageTransactionInfoImpl) Connection(javax.jms.Connection) StorageManager(org.apache.activemq.artemis.core.persistence.StorageManager) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) QueueImpl(org.apache.activemq.artemis.core.server.impl.QueueImpl) AddressInfo(org.apache.activemq.artemis.core.server.impl.AddressInfo) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) MessageProducer(javax.jms.MessageProducer) Session(javax.jms.Session) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Test(org.junit.Test)

Example 98 with ActiveMQConnectionFactory

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

the class ActiveMQClusteredTest method testOutboundLoadBalancing.

@Test
public void testOutboundLoadBalancing() throws Exception {
    final int CONNECTION_COUNT = 100;
    ActiveMQResourceAdapter qResourceAdapter = newResourceAdapter();
    List<Session> sessions = new ArrayList<>();
    List<ActiveMQRAManagedConnection> managedConnections = new ArrayList<>();
    try {
        MyBootstrapContext ctx = new MyBootstrapContext();
        qResourceAdapter.start(ctx);
        ActiveMQRAConnectionManager qraConnectionManager = new ActiveMQRAConnectionManager();
        ActiveMQRAManagedConnectionFactory mcf = new ActiveMQRAManagedConnectionFactory();
        mcf.setResourceAdapter(qResourceAdapter);
        ActiveMQRAConnectionFactory qraConnectionFactory = new ActiveMQRAConnectionFactoryImpl(mcf, qraConnectionManager);
        QueueConnection queueConnection = qraConnectionFactory.createQueueConnection();
        Session s = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        sessions.add(s);
        ActiveMQRAManagedConnection mc = (ActiveMQRAManagedConnection) ((ActiveMQRASession) s).getManagedConnection();
        managedConnections.add(mc);
        ActiveMQConnectionFactory cf1 = mc.getConnectionFactory();
        while (!((ServerLocatorImpl) cf1.getServerLocator()).isReceivedTopology()) {
            Thread.sleep(50);
        }
        for (int i = 0; i < CONNECTION_COUNT; i++) {
            queueConnection = qraConnectionFactory.createQueueConnection();
            s = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            sessions.add(s);
            mc = (ActiveMQRAManagedConnection) ((ActiveMQRASession) s).getManagedConnection();
            managedConnections.add(mc);
        }
        assertTrue(server.getConnectionCount() >= (CONNECTION_COUNT / 2));
        assertTrue(secondaryServer.getConnectionCount() >= (CONNECTION_COUNT / 2));
    } finally {
        for (Session s : sessions) {
            s.close();
        }
        for (ActiveMQRAManagedConnection mc : managedConnections) {
            mc.destroy();
        }
    }
}
Also used : ActiveMQRAConnectionManager(org.apache.activemq.artemis.ra.ActiveMQRAConnectionManager) ActiveMQRAManagedConnection(org.apache.activemq.artemis.ra.ActiveMQRAManagedConnection) ArrayList(java.util.ArrayList) ActiveMQRAConnectionFactory(org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactory) ActiveMQRASession(org.apache.activemq.artemis.ra.ActiveMQRASession) ActiveMQRAConnectionFactoryImpl(org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl) ActiveMQRAManagedConnectionFactory(org.apache.activemq.artemis.ra.ActiveMQRAManagedConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) QueueConnection(javax.jms.QueueConnection) ActiveMQResourceAdapter(org.apache.activemq.artemis.ra.ActiveMQResourceAdapter) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Session(javax.jms.Session) ActiveMQRASession(org.apache.activemq.artemis.ra.ActiveMQRASession) Test(org.junit.Test)

Example 99 with ActiveMQConnectionFactory

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

the class OutgoingConnectionTest method testSharedActiveMQConnectionFactory.

@Test
public void testSharedActiveMQConnectionFactory() throws Exception {
    Session s = null;
    Session s2 = null;
    ActiveMQRAManagedConnection mc = null;
    ActiveMQRAManagedConnection mc2 = null;
    try {
        resourceAdapter = new ActiveMQResourceAdapter();
        resourceAdapter.setConnectorClassName(InVMConnectorFactory.class.getName());
        MyBootstrapContext ctx = new MyBootstrapContext();
        resourceAdapter.start(ctx);
        ActiveMQRAConnectionManager qraConnectionManager = new ActiveMQRAConnectionManager();
        ActiveMQRAManagedConnectionFactory mcf = new ActiveMQRAManagedConnectionFactory();
        mcf.setResourceAdapter(resourceAdapter);
        ActiveMQRAConnectionFactory qraConnectionFactory = new ActiveMQRAConnectionFactoryImpl(mcf, qraConnectionManager);
        QueueConnection queueConnection = qraConnectionFactory.createQueueConnection();
        s = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        mc = (ActiveMQRAManagedConnection) ((ActiveMQRASession) s).getManagedConnection();
        ActiveMQConnectionFactory cf1 = mc.getConnectionFactory();
        QueueConnection queueConnection2 = qraConnectionFactory.createQueueConnection();
        s2 = queueConnection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
        mc2 = (ActiveMQRAManagedConnection) ((ActiveMQRASession) s2).getManagedConnection();
        ActiveMQConnectionFactory cf2 = mc2.getConnectionFactory();
        // we're not testing equality so don't use equals(); we're testing if they are actually the *same* object
        assertTrue(cf1 == cf2);
    } finally {
        if (s != null) {
            s.close();
        }
        if (mc != null) {
            mc.destroy();
        }
        if (s2 != null) {
            s2.close();
        }
        if (mc2 != null) {
            mc2.destroy();
        }
    }
}
Also used : ActiveMQRAManagedConnectionFactory(org.apache.activemq.artemis.ra.ActiveMQRAManagedConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQRAConnectionManager(org.apache.activemq.artemis.ra.ActiveMQRAConnectionManager) XAQueueConnection(javax.jms.XAQueueConnection) QueueConnection(javax.jms.QueueConnection) ActiveMQRAManagedConnection(org.apache.activemq.artemis.ra.ActiveMQRAManagedConnection) ActiveMQResourceAdapter(org.apache.activemq.artemis.ra.ActiveMQResourceAdapter) InVMConnectorFactory(org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory) ActiveMQRAConnectionFactory(org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactory) ActiveMQRASession(org.apache.activemq.artemis.ra.ActiveMQRASession) XASession(javax.jms.XASession) Session(javax.jms.Session) QueueSession(javax.jms.QueueSession) ActiveMQRASession(org.apache.activemq.artemis.ra.ActiveMQRASession) ActiveMQRAConnectionFactoryImpl(org.apache.activemq.artemis.ra.ActiveMQRAConnectionFactoryImpl) Test(org.junit.Test)

Example 100 with ActiveMQConnectionFactory

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

the class ResourceAdapterTest method testResourceAdapterSetupReconnectAttemptsOverride.

@Test
public void testResourceAdapterSetupReconnectAttemptsOverride() throws Exception {
    ActiveMQResourceAdapter qResourceAdapter = new ActiveMQResourceAdapter();
    qResourceAdapter.setConnectorClassName(INVM_CONNECTOR_FACTORY);
    qResourceAdapter.setConnectionParameters("server-id=0");
    ActiveMQRATestBase.MyBootstrapContext ctx = new ActiveMQRATestBase.MyBootstrapContext();
    qResourceAdapter.start(ctx);
    ActiveMQActivationSpec spec = new ActiveMQActivationSpec();
    spec.setResourceAdapter(qResourceAdapter);
    spec.setUseJNDI(false);
    spec.setDestinationType("javax.jms.Queue");
    spec.setDestination(MDBQUEUE);
    spec.setReconnectAttempts(100);
    ActiveMQConnectionFactory fac = qResourceAdapter.getConnectionFactory(spec);
    assertEquals(100, fac.getReconnectAttempts());
    qResourceAdapter.stop();
    assertTrue(spec.isHasBeenUpdated());
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQResourceAdapter(org.apache.activemq.artemis.ra.ActiveMQResourceAdapter) ActiveMQActivationSpec(org.apache.activemq.artemis.ra.inflow.ActiveMQActivationSpec) Test(org.junit.Test)

Aggregations

ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)221 Test (org.junit.Test)141 Connection (javax.jms.Connection)84 Session (javax.jms.Session)84 MessageProducer (javax.jms.MessageProducer)63 MessageConsumer (javax.jms.MessageConsumer)60 TextMessage (javax.jms.TextMessage)49 Queue (javax.jms.Queue)48 ConnectionFactory (javax.jms.ConnectionFactory)35 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)27 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)26 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)24 ActiveMQResourceAdapter (org.apache.activemq.artemis.ra.ActiveMQResourceAdapter)24 URI (java.net.URI)22 JMSException (javax.jms.JMSException)20 Message (javax.jms.Message)19 DiscoveryGroupConfiguration (org.apache.activemq.artemis.api.core.DiscoveryGroupConfiguration)19 InitialContext (javax.naming.InitialContext)16 Context (javax.naming.Context)15 Hashtable (java.util.Hashtable)14