Search in sources :

Example 6 with ActiveMQNotConnectedException

use of org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException 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 7 with ActiveMQNotConnectedException

use of org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException 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 8 with ActiveMQNotConnectedException

use of org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException 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 9 with ActiveMQNotConnectedException

use of org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException in project activemq-artemis by apache.

the class SessionClosedOnRemotingConnectionFailureTest method testSessionClosedOnRemotingConnectionFailure.

@Test
public void testSessionClosedOnRemotingConnectionFailure() throws Exception {
    ClientSession session = addClientSession(sf.createSession());
    session.createQueue("fooaddress", RoutingType.ANYCAST, "fooqueue");
    ClientProducer prod = session.createProducer("fooaddress");
    ClientConsumer cons = session.createConsumer("fooqueue");
    session.start();
    prod.send(session.createMessage(false));
    Assert.assertNotNull(cons.receive());
    // Now fail the underlying connection
    RemotingConnection connection = ((ClientSessionInternal) session).getConnection();
    connection.fail(new ActiveMQNotConnectedException());
    Assert.assertTrue(session.isClosed());
    Assert.assertTrue(prod.isClosed());
    Assert.assertTrue(cons.isClosed());
    try {
        prod.send(session.createMessage(false));
        Assert.fail("Should throw exception");
    } catch (ActiveMQObjectClosedException oce) {
    // ok
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
    try {
        cons.receive();
        Assert.fail("Should throw exception");
    } catch (ActiveMQObjectClosedException oce) {
    // ok
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
    session.close();
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQObjectClosedException(org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) 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 10 with ActiveMQNotConnectedException

use of org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException in project activemq-artemis by apache.

the class ReplicatedDistributionTest method fail.

/**
 * @param session
 * @throws InterruptedException
 */
private void fail(final ClientSession session) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    session.addFailureListener(new CountDownSessionFailureListener(latch, session));
    RemotingConnection conn = ((ClientSessionInternal) session).getConnection();
    // Simulate failure on connection
    conn.fail(new ActiveMQNotConnectedException());
    // Wait to be informed of failure
    boolean ok = latch.await(1000, TimeUnit.MILLISECONDS);
    Assert.assertTrue(ok);
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) CountDownLatch(java.util.concurrent.CountDownLatch) CountDownSessionFailureListener(org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener)

Aggregations

ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)44 Test (org.junit.Test)38 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)20 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)19 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)19 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)17 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)16 ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)16 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)15 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)14 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)14 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)11 ClientSessionFactoryInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)11 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)6 Connection (javax.jms.Connection)5 Session (javax.jms.Session)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 MessageConsumer (javax.jms.MessageConsumer)4 MessageProducer (javax.jms.MessageProducer)4 Message (org.apache.activemq.artemis.api.core.Message)4