Search in sources :

Example 6 with ActiveMQConnection

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

the class JMSUtil method createConnectionAndWaitForTopology.

// Constructors --------------------------------------------------
// Public --------------------------------------------------------
// Package protected ---------------------------------------------
// Protected -----------------------------------------------------
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
public static ActiveMQConnection createConnectionAndWaitForTopology(ActiveMQConnectionFactory factory, int topologyMembers, int timeout) throws Exception {
    ActiveMQConnection conn;
    CountDownLatch countDownLatch = new CountDownLatch(topologyMembers);
    ServerLocator locator = factory.getServerLocator();
    locator.addClusterTopologyListener(new FailoverTestBase.LatchClusterTopologyListener(countDownLatch));
    conn = (ActiveMQConnection) factory.createConnection();
    boolean ok = countDownLatch.await(timeout, TimeUnit.SECONDS);
    if (!ok) {
        throw new IllegalStateException("timed out waiting for topology");
    }
    return conn;
}
Also used : FailoverTestBase(org.apache.activemq.artemis.tests.integration.cluster.failover.FailoverTestBase) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) CountDownLatch(java.util.concurrent.CountDownLatch) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator)

Example 7 with ActiveMQConnection

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

the class ClientSideFailoverListerExample method main.

public static void main(final String[] args) throws Exception {
    InitialContext initialContext = null;
    Connection connectionA = null;
    try {
        server0 = ServerUtil.startServer(args[0], ClientSideFailoverListerExample.class.getSimpleName() + "0", 0, 5000);
        server1 = ServerUtil.startServer(args[1], ClientSideFailoverListerExample.class.getSimpleName() + "1", 1, 0);
        // Step 1. Get an initial context for looking up JNDI from server 0
        initialContext = new InitialContext();
        // Step 2. Look-up the JMS Queue object from JNDI
        Queue queue = (Queue) initialContext.lookup("queue/exampleQueue");
        // Step 3. Look-up a JMS Connection Factory object from JNDI on server 0
        ConnectionFactory connectionFactory = (ConnectionFactory) initialContext.lookup("ConnectionFactory");
        // Step 4. We create 1 JMS connections from the same connection factory.
        // Wait a little while to make sure broadcasts from all nodes have reached the client
        Thread.sleep(5000);
        connectionA = connectionFactory.createConnection();
        ((ActiveMQConnection) connectionA).setFailoverListener(new FailoverListenerImpl());
        // Step 5. We create JMS Sessions
        Session sessionA = connectionA.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // Step 6. We create JMS MessageProducer objects on the sessions
        MessageProducer producerA = sessionA.createProducer(queue);
        // Step 7. We send some messages on each producer
        final int numMessages = 10;
        for (int i = 0; i < numMessages; i++) {
            TextMessage messageA = sessionA.createTextMessage("A:This is text message " + i);
            producerA.send(messageA);
            System.out.println("Sent message: " + messageA.getText());
        }
        // Step 8. We start the connection to consume messages
        connectionA.start();
        // Step 9. We consume messages from the session A, one at a time.
        // We reached message no 5 the first server will crash
        consume(sessionA, queue, numMessages, "A");
    } finally {
        if (connectionA != null) {
            connectionA.close();
        }
        if (initialContext != null) {
            initialContext.close();
        }
        ServerUtil.killServer(server0);
        ServerUtil.killServer(server1);
    }
}
Also used : ConnectionFactory(javax.jms.ConnectionFactory) Connection(javax.jms.Connection) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) InitialContext(javax.naming.InitialContext) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Example 8 with ActiveMQConnection

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

the class HQFailoverTest method failoverTest.

@Test
public void failoverTest() throws Throwable {
    String textBody = "a rapadura e doce mas nao e mole nao";
    String queueName = "queue";
    ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("tcp://localhost:5445?ha=true&reconnectAttempts=10&protocolManagerFactoryStr=org.apache.activemq.artemis.core.protocol.hornetq.client.HornetQClientProtocolManagerFactory&confirmationWindowSize=1048576&blockOnDurableSend=false");
    ActiveMQConnection conn = (ActiveMQConnection) cf.createConnection();
    Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Queue queue = session.createQueue(queueName);
    MessageProducer producer = session.createProducer(queue);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    for (int i = 0; i < 10; i++) {
        producer.send(session.createTextMessage(textBody + i));
    }
    CountDownLatch latch = new CountDownLatch(1);
    conn.setFailoverListener(eventType -> {
        if (eventType == FailoverEventType.FAILOVER_COMPLETED) {
            latch.countDown();
        }
    });
    execute(serverClassloader, "liveServer.stop(true)");
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    // This test is to validate stuff can still work well after failover against hornetq
    for (int i = 0; i < 10; i++) {
        producer.send(session.createTextMessage(textBody + i));
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) MessageProducer(javax.jms.MessageProducer) CountDownLatch(java.util.concurrent.CountDownLatch) Queue(javax.jms.Queue) Session(javax.jms.Session) Test(org.junit.Test)

Example 9 with ActiveMQConnection

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

the class ExceptionListenerTest method testListenerCalledForOneConnectionAndSessions.

@Test
public void testListenerCalledForOneConnectionAndSessions() throws Exception {
    Connection conn = cf.createConnection();
    CountDownLatch latch = new CountDownLatch(1);
    MyExceptionListener listener = new MyExceptionListener(latch);
    conn.setExceptionListener(listener);
    Session sess1 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Session sess2 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Session sess3 = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ClientSessionInternal coreSession0 = (ClientSessionInternal) ((ActiveMQConnection) conn).getInitialSession();
    ClientSessionInternal coreSession1 = (ClientSessionInternal) ((ActiveMQSession) sess1).getCoreSession();
    ClientSessionInternal coreSession2 = (ClientSessionInternal) ((ActiveMQSession) sess2).getCoreSession();
    ClientSessionInternal coreSession3 = (ClientSessionInternal) ((ActiveMQSession) sess3).getCoreSession();
    coreSession0.getConnection().fail(new ActiveMQInternalErrorException("blah"));
    coreSession1.getConnection().fail(new ActiveMQInternalErrorException("blah"));
    coreSession2.getConnection().fail(new ActiveMQInternalErrorException("blah"));
    coreSession3.getConnection().fail(new ActiveMQInternalErrorException("blah"));
    latch.await(5, TimeUnit.SECONDS);
    // Listener should only be called once even if all sessions connections die
    Assert.assertEquals(1, listener.numCalls);
    conn.close();
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) Connection(javax.jms.Connection) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) ActiveMQInternalErrorException(org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException) CountDownLatch(java.util.concurrent.CountDownLatch) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) Test(org.junit.Test)

Example 10 with ActiveMQConnection

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

the class ExceptionListenerTest method testListenerCalledForOneConnection.

@Test
public void testListenerCalledForOneConnection() throws Exception {
    Connection conn = cf.createConnection();
    CountDownLatch latch = new CountDownLatch(1);
    MyExceptionListener listener = new MyExceptionListener(latch);
    conn.setExceptionListener(listener);
    ClientSessionInternal coreSession = (ClientSessionInternal) ((ActiveMQConnection) conn).getInitialSession();
    coreSession.getConnection().fail(new ActiveMQInternalErrorException("blah"));
    latch.await(5, TimeUnit.SECONDS);
    Assert.assertEquals(1, listener.numCalls);
    conn.close();
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) Connection(javax.jms.Connection) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) ActiveMQInternalErrorException(org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

ActiveMQConnection (org.apache.activemq.artemis.jms.client.ActiveMQConnection)10 Connection (javax.jms.Connection)6 Session (javax.jms.Session)5 Test (org.junit.Test)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 MessageProducer (javax.jms.MessageProducer)4 Queue (javax.jms.Queue)4 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)4 ActiveMQConnectionFactory (org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory)4 TextMessage (javax.jms.TextMessage)3 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)3 ActiveMQSession (org.apache.activemq.artemis.jms.client.ActiveMQSession)3 MessageConsumer (javax.jms.MessageConsumer)2 ActiveMQInternalErrorException (org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException)2 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)2 ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)2 BytesMessage (javax.jms.BytesMessage)1 ConnectionFactory (javax.jms.ConnectionFactory)1 JMSException (javax.jms.JMSException)1 XAConnection (javax.jms.XAConnection)1