Search in sources :

Example 1 with ActiveMQConnection

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

the class JMSFailoverListenerTest method testAutomaticFailover.

// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
@Test
public void testAutomaticFailover() throws Exception {
    ActiveMQConnectionFactory jbcf = ActiveMQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, livetc);
    jbcf.setReconnectAttempts(-1);
    jbcf.setBlockOnDurableSend(true);
    jbcf.setBlockOnNonDurableSend(true);
    // Note we set consumer window size to a value so we can verify that consumer credit re-sending
    // works properly on failover
    // The value is small enough that credits will have to be resent several time
    final int numMessages = 10;
    final int bodySize = 1000;
    jbcf.setConsumerWindowSize(numMessages * bodySize / 10);
    ActiveMQConnection conn = JMSUtil.createConnectionAndWaitForTopology(jbcf, 2, 5);
    MyFailoverListener listener = new MyFailoverListener();
    conn.setFailoverListener(listener);
    Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ClientSession coreSession = ((ActiveMQSession) sess).getCoreSession();
    SimpleString jmsQueueName = new SimpleString("myqueue");
    coreSession.createQueue(jmsQueueName, RoutingType.ANYCAST, jmsQueueName, null, true);
    Queue queue = sess.createQueue("myqueue");
    MessageProducer producer = sess.createProducer(queue);
    producer.setDeliveryMode(DeliveryMode.PERSISTENT);
    MessageConsumer consumer = sess.createConsumer(queue);
    byte[] body = RandomUtil.randomBytes(bodySize);
    for (int i = 0; i < numMessages; i++) {
        BytesMessage bm = sess.createBytesMessage();
        bm.writeBytes(body);
        producer.send(bm);
    }
    conn.start();
    JMSFailoverListenerTest.log.info("sent messages and started connection");
    Thread.sleep(2000);
    JMSUtil.crash(liveServer, ((ActiveMQSession) sess).getCoreSession());
    Assert.assertEquals(FailoverEventType.FAILURE_DETECTED, listener.get(0));
    for (int i = 0; i < numMessages; i++) {
        JMSFailoverListenerTest.log.info("got message " + i);
        BytesMessage bm = (BytesMessage) consumer.receive(1000);
        Assert.assertNotNull(bm);
        Assert.assertEquals(body.length, bm.getBodyLength());
    }
    TextMessage tm = (TextMessage) consumer.receiveNoWait();
    Assert.assertNull(tm);
    Assert.assertEquals(FailoverEventType.FAILOVER_COMPLETED, listener.get(1));
    conn.close();
    Assert.assertEquals("Expected 2 FailoverEvents to be triggered", 2, listener.size());
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) BytesMessage(javax.jms.BytesMessage) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Test(org.junit.Test)

Example 2 with ActiveMQConnection

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

the class JMSFailoverListenerTest method testManualFailover.

@Test
public void testManualFailover() throws Exception {
    ActiveMQConnectionFactory jbcfLive = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY));
    jbcfLive.setBlockOnNonDurableSend(true);
    jbcfLive.setBlockOnDurableSend(true);
    ActiveMQConnectionFactory jbcfBackup = ActiveMQJMSClient.createConnectionFactoryWithoutHA(JMSFactoryType.CF, new TransportConfiguration(INVM_CONNECTOR_FACTORY, backupParams));
    jbcfBackup.setBlockOnNonDurableSend(true);
    jbcfBackup.setBlockOnDurableSend(true);
    jbcfBackup.setInitialConnectAttempts(-1);
    jbcfBackup.setReconnectAttempts(-1);
    ActiveMQConnection connLive = (ActiveMQConnection) jbcfLive.createConnection();
    MyFailoverListener listener = new MyFailoverListener();
    connLive.setFailoverListener(listener);
    Session sessLive = connLive.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ClientSession coreSessionLive = ((ActiveMQSession) sessLive).getCoreSession();
    SimpleString jmsQueueName = new SimpleString("myqueue");
    coreSessionLive.createQueue(jmsQueueName, RoutingType.ANYCAST, jmsQueueName, null, true);
    Queue queue = sessLive.createQueue("myqueue");
    final int numMessages = 1000;
    MessageProducer producerLive = sessLive.createProducer(queue);
    for (int i = 0; i < numMessages; i++) {
        TextMessage tm = sessLive.createTextMessage("message" + i);
        producerLive.send(tm);
    }
    // Note we block on P send to make sure all messages get to server before failover
    JMSUtil.crash(liveServer, coreSessionLive);
    Assert.assertEquals(FailoverEventType.FAILURE_DETECTED, listener.get(0));
    connLive.close();
    // Now recreate on backup
    Connection connBackup = jbcfBackup.createConnection();
    Session sessBackup = connBackup.createSession(false, Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumerBackup = sessBackup.createConsumer(queue);
    connBackup.start();
    for (int i = 0; i < numMessages; i++) {
        TextMessage tm = (TextMessage) consumerBackup.receive(1000);
        Assert.assertNotNull(tm);
        Assert.assertEquals("message" + i, tm.getText());
    }
    TextMessage tm = (TextMessage) consumerBackup.receiveNoWait();
    Assert.assertEquals(FailoverEventType.FAILOVER_FAILED, listener.get(1));
    Assert.assertEquals("Expected 2 FailoverEvents to be triggered", 2, listener.size());
    Assert.assertNull(tm);
    connBackup.close();
}
Also used : MessageConsumer(javax.jms.MessageConsumer) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) Connection(javax.jms.Connection) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) ActiveMQSession(org.apache.activemq.artemis.jms.client.ActiveMQSession) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Test(org.junit.Test)

Example 3 with ActiveMQConnection

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

the class ServerUtil method getServer.

public static int getServer(Connection connection) {
    ClientSession session = ((ActiveMQConnection) connection).getInitialSession();
    TransportConfiguration transportConfiguration = session.getSessionFactory().getConnectorConfiguration();
    String port = (String) transportConfiguration.getParams().get("port");
    return Integer.valueOf(port) - 61616;
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration)

Example 4 with ActiveMQConnection

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

the class ServerUtil method getServerConnection.

public static Connection getServerConnection(int server, Connection... connections) {
    for (Connection connection : connections) {
        ClientSession session = ((ActiveMQConnection) connection).getInitialSession();
        TransportConfiguration transportConfiguration = session.getSessionFactory().getConnectorConfiguration();
        String port = (String) transportConfiguration.getParams().get("port");
        if (Integer.valueOf(port) == server + 61616) {
            return connection;
        }
    }
    return null;
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) Connection(javax.jms.Connection) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration)

Example 5 with ActiveMQConnection

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

the class JMSBridgeImpl method createConnection.

private Connection createConnection(final String username, final String password, final ConnectionFactoryFactory cff, final String clientID, final boolean isXA, boolean isSource) throws Exception {
    Connection conn = null;
    try {
        Object cf = cff.createConnectionFactory();
        if (cf instanceof ActiveMQConnectionFactory && registry != null) {
            registry.register(XARecoveryConfig.newConfig((ActiveMQConnectionFactory) cf, username, password, null));
        }
        if (qualityOfServiceMode == QualityOfServiceMode.ONCE_AND_ONLY_ONCE && !(cf instanceof XAConnectionFactory)) {
            throw new IllegalArgumentException("Connection factory must be XAConnectionFactory");
        }
        if (username == null) {
            if (isXA) {
                if (JMSBridgeImpl.trace) {
                    ActiveMQJMSBridgeLogger.LOGGER.trace("Creating an XA connection");
                }
                conn = ((XAConnectionFactory) cf).createXAConnection();
            } else {
                if (JMSBridgeImpl.trace) {
                    ActiveMQJMSBridgeLogger.LOGGER.trace("Creating a non XA connection");
                }
                conn = ((ConnectionFactory) cf).createConnection();
            }
        } else {
            if (isXA) {
                if (JMSBridgeImpl.trace) {
                    ActiveMQJMSBridgeLogger.LOGGER.trace("Creating an XA connection");
                }
                conn = ((XAConnectionFactory) cf).createXAConnection(username, password);
            } else {
                if (JMSBridgeImpl.trace) {
                    ActiveMQJMSBridgeLogger.LOGGER.trace("Creating a non XA connection");
                }
                conn = ((ConnectionFactory) cf).createConnection(username, password);
            }
        }
        if (clientID != null) {
            conn.setClientID(clientID);
        }
        boolean ha = false;
        BridgeFailoverListener failoverListener = null;
        if (conn instanceof ActiveMQConnection) {
            ActiveMQConnectionFactory activeMQCF = (ActiveMQConnectionFactory) cf;
            ha = activeMQCF.isHA();
            if (ha) {
                ActiveMQConnection activeMQConn = (ActiveMQConnection) conn;
                failoverListener = new BridgeFailoverListener(isSource);
                activeMQConn.setFailoverListener(failoverListener);
            }
        }
        conn.setExceptionListener(new BridgeExceptionListener(ha, failoverListener, isSource));
        return conn;
    } catch (JMSException e) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Throwable ignored) {
        }
        throw e;
    }
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) XAConnection(javax.jms.XAConnection) Connection(javax.jms.Connection) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) ActiveMQConnection(org.apache.activemq.artemis.jms.client.ActiveMQConnection) XAConnectionFactory(javax.jms.XAConnectionFactory) JMSException(javax.jms.JMSException)

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