Search in sources :

Example 11 with ClientSessionInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal in project activemq-artemis by apache.

the class ReattachTest method testCreateQueue.

@Test
public void testCreateQueue() throws Exception {
    final long retryInterval = 100;
    final double retryMultiplier = 1d;
    final int reconnectAttempts = 300;
    locator.setRetryInterval(retryInterval).setRetryIntervalMultiplier(retryMultiplier).setReconnectAttempts(reconnectAttempts).setConfirmationWindowSize(1024 * 1024);
    ClientSessionFactoryInternal sf = (ClientSessionFactoryInternal) createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, true);
    // Sleep 3 times retryInterval, so it should at least have 3 retries
    RemotingConnection conn = ((ClientSessionInternal) session).getConnection();
    InVMConnector.failOnCreateConnection = false;
    conn.fail(new ActiveMQNotConnectedException());
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(retryInterval * 3);
            } catch (InterruptedException ignore) {
            }
            InVMConnector.failOnCreateConnection = false;
        }
    };
    t.start();
    for (int i = 0; i < 10; i++) {
        session.createQueue("address", RoutingType.ANYCAST, "queue" + i);
    }
    // 
    // InVMConnector.failOnCreateConnection = true;
    // 
    // //Should throw exception since didn't reconnect
    // 
    // try
    // {
    // session.start();
    // 
    // fail("Should throw exception");
    // }
    // catch (ActiveMQException e)
    // {
    // assertEquals(ActiveMQException.OBJECT_CLOSED, e.getCode());
    // }
    session.close();
    sf.close();
    t.join();
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) Test(org.junit.Test)

Example 12 with ClientSessionInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal in project activemq-artemis by apache.

the class ReattachTest method testReattachAttemptsSucceedsInReconnecting.

@Test
public void testReattachAttemptsSucceedsInReconnecting() throws Exception {
    final long retryInterval = 50;
    final double retryMultiplier = 1d;
    final int reconnectAttempts = 10;
    locator.setRetryInterval(retryInterval).setRetryIntervalMultiplier(retryMultiplier).setReconnectAttempts(reconnectAttempts).setConfirmationWindowSize(1024 * 1024);
    ClientSessionFactoryInternal sf = (ClientSessionFactoryInternal) createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, true);
    session.createQueue(ReattachTest.ADDRESS, ReattachTest.ADDRESS, null, false);
    ClientProducer producer = session.createProducer(ReattachTest.ADDRESS);
    final int numMessages = 1000;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session.createMessage(ActiveMQTextMessage.TYPE, false, 0, System.currentTimeMillis(), (byte) 1);
        message.putIntProperty(new SimpleString("count"), i);
        message.getBodyBuffer().writeString("aardvarks");
        producer.send(message);
    }
    ClientConsumer consumer = session.createConsumer(ReattachTest.ADDRESS);
    InVMConnector.failOnCreateConnection = true;
    InVMConnector.numberOfFailures = reconnectAttempts - 1;
    RemotingConnection conn = ((ClientSessionInternal) session).getConnection();
    conn.fail(new ActiveMQNotConnectedException());
    session.start();
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = consumer.receive(500);
        Assert.assertNotNull(message);
        Assert.assertEquals("aardvarks", message.getBodyBuffer().readString());
        Assert.assertEquals(i, message.getIntProperty("count").intValue());
        message.acknowledge();
    }
    ClientMessage message = consumer.receiveImmediate();
    Assert.assertNull(message);
    session.close();
    sf.close();
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 13 with ClientSessionInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal in project activemq-artemis by apache.

the class ReattachTest method testRetryInterval.

@Test
public void testRetryInterval() throws Exception {
    final long retryInterval = 500;
    final double retryMultiplier = 1d;
    final int reconnectAttempts = 60;
    locator.setRetryInterval(retryInterval).setRetryIntervalMultiplier(retryMultiplier).setReconnectAttempts(reconnectAttempts).setConfirmationWindowSize(1024 * 1024);
    ClientSessionFactoryInternal sf = (ClientSessionFactoryInternal) createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, true);
    session.createQueue(ReattachTest.ADDRESS, ReattachTest.ADDRESS, null, false);
    ClientProducer producer = session.createProducer(ReattachTest.ADDRESS);
    final int numMessages = 1000;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session.createMessage(ActiveMQTextMessage.TYPE, false, 0, System.currentTimeMillis(), (byte) 1);
        message.putIntProperty(new SimpleString("count"), i);
        message.getBodyBuffer().writeString("aardvarks");
        producer.send(message);
    }
    ClientConsumer consumer = session.createConsumer(ReattachTest.ADDRESS);
    InVMConnector.failOnCreateConnection = true;
    RemotingConnection conn = ((ClientSessionInternal) session).getConnection();
    long start = System.currentTimeMillis();
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(retryInterval / 2);
            } catch (InterruptedException ignore) {
            }
            InVMConnector.failOnCreateConnection = false;
        }
    };
    t.start();
    conn.fail(new ActiveMQNotConnectedException());
    session.start();
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = consumer.receive(500);
        Assert.assertNotNull(message);
        Assert.assertEquals("aardvarks", message.getBodyBuffer().readString());
        Assert.assertEquals(i, message.getIntProperty("count").intValue());
        message.acknowledge();
    }
    ClientMessage message = consumer.receiveImmediate();
    Assert.assertNull(message);
    long end = System.currentTimeMillis();
    Assert.assertTrue(end - start >= retryInterval);
    session.close();
    sf.close();
    t.join();
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 14 with ClientSessionInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal in project activemq-artemis by apache.

the class ReattachTest method testDelayedReattach.

/*
    * Test failure on connection, simulate failure to create connection for a while, then
    * allow connection to be recreated
    */
@Test
public void testDelayedReattach() throws Exception {
    final long retryInterval = 50;
    final double retryMultiplier = 1d;
    final int reconnectAttempts = 60;
    locator.setRetryInterval(retryInterval).setRetryIntervalMultiplier(retryMultiplier).setReconnectAttempts(reconnectAttempts).setConfirmationWindowSize(1024 * 1024);
    ClientSessionFactoryInternal sf = (ClientSessionFactoryInternal) createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, true);
    session.createQueue(ReattachTest.ADDRESS, ReattachTest.ADDRESS, null, false);
    ClientProducer producer = session.createProducer(ReattachTest.ADDRESS);
    final int numMessages = 1000;
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = session.createMessage(ActiveMQTextMessage.TYPE, false, 0, System.currentTimeMillis(), (byte) 1);
        message.putIntProperty(new SimpleString("count"), i);
        message.getBodyBuffer().writeString("aardvarks");
        producer.send(message);
    }
    ClientConsumer consumer = session.createConsumer(ReattachTest.ADDRESS);
    InVMConnector.failOnCreateConnection = true;
    RemotingConnection conn = ((ClientSessionInternal) session).getConnection();
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(retryInterval * 3);
            } catch (InterruptedException ignore) {
            }
            InVMConnector.failOnCreateConnection = false;
        }
    };
    t.start();
    conn.fail(new ActiveMQNotConnectedException());
    session.start();
    for (int i = 0; i < numMessages; i++) {
        ClientMessage message = consumer.receive(500);
        Assert.assertNotNull(message);
        Assert.assertEquals("aardvarks", message.getBodyBuffer().readString());
        Assert.assertEquals(i, message.getIntProperty("count").intValue());
        message.acknowledge();
    }
    ClientMessage message = consumer.receiveImmediate();
    Assert.assertNull(message);
    session.close();
    sf.close();
    t.join();
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 15 with ClientSessionInternal

use of org.apache.activemq.artemis.core.client.impl.ClientSessionInternal in project activemq-artemis by apache.

the class JMSFailoverTest 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);
    Connection connLive = jbcfLive.createConnection();
    MyExceptionListener listener = new MyExceptionListener();
    connLive.setExceptionListener(listener);
    Session sessLive = connLive.createSession(false, Session.AUTO_ACKNOWLEDGE);
    ClientSession coreSessionLive = ((ActiveMQSession) sessLive).getCoreSession();
    RemotingConnection coreConnLive = ((ClientSessionInternal) coreSessionLive).getConnection();
    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);
    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.assertNull(tm);
    connBackup.close();
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) MessageConsumer(javax.jms.MessageConsumer) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) Connection(javax.jms.Connection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) 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)

Aggregations

ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)46 Test (org.junit.Test)35 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)21 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)20 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)19 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)17 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)13 Connection (javax.jms.Connection)12 Session (javax.jms.Session)12 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)11 ClientSessionFactoryInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)11 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)10 MessageConsumer (javax.jms.MessageConsumer)8 MessageProducer (javax.jms.MessageProducer)8 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 TextMessage (javax.jms.TextMessage)7 ActiveMQSession (org.apache.activemq.artemis.jms.client.ActiveMQSession)7 XAConnection (javax.jms.XAConnection)5 XASession (javax.jms.XASession)5