Search in sources :

Example 11 with CountDownSessionFailureListener

use of org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener in project activemq-artemis by apache.

the class AsynchronousFailoverTest method doTestNonTransactional.

private void doTestNonTransactional(final TestRunner runner) throws Exception {
    while (!runner.isFailed()) {
        AsynchronousFailoverTest.log.info("looping");
        ClientSession session = sf.createSession(true, true, 0);
        listener = new CountDownSessionFailureListener(session);
        session.addFailureListener(listener);
        ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
        final int numMessages = 1000;
        for (int i = 0; i < numMessages; i++) {
            boolean retry = false;
            do {
                try {
                    ClientMessage message = session.createMessage(true);
                    message.getBodyBuffer().writeString("message" + i);
                    message.putIntProperty("counter", i);
                    addPayload(message);
                    producer.send(message);
                    retry = false;
                } catch (ActiveMQUnBlockedException ube) {
                    AsynchronousFailoverTest.log.info("exception when sending message with counter " + i);
                    ube.printStackTrace();
                    retry = true;
                } catch (ActiveMQException e) {
                    fail("Invalid Exception type:" + e.getType());
                }
            } while (retry);
        }
        // create the consumer with retry if failover occurs during createConsumer call
        ClientConsumer consumer = null;
        boolean retry = false;
        do {
            try {
                consumer = session.createConsumer(FailoverTestBase.ADDRESS);
                retry = false;
            } catch (ActiveMQUnBlockedException ube) {
                AsynchronousFailoverTest.log.info("exception when creating consumer");
                retry = true;
            } catch (ActiveMQException e) {
                fail("Invalid Exception type:" + e.getType());
            }
        } while (retry);
        session.start();
        List<Integer> counts = new ArrayList<>(1000);
        int lastCount = -1;
        boolean counterGap = false;
        while (true) {
            ClientMessage message = consumer.receive(500);
            if (message == null) {
                break;
            }
            // messages must remain ordered but there could be a "jump" if messages
            // are missing or duplicated
            int count = message.getIntProperty("counter");
            counts.add(count);
            if (count != lastCount + 1) {
                if (counterGap) {
                    Assert.fail("got another counter gap at " + count + ": " + counts);
                } else {
                    if (lastCount != -1) {
                        AsynchronousFailoverTest.log.info("got first counter gap at " + count);
                        counterGap = true;
                    }
                }
            }
            lastCount = count;
            message.acknowledge();
        }
        session.close();
        this.listener = null;
    }
}
Also used : ActiveMQUnBlockedException(org.apache.activemq.artemis.api.core.ActiveMQUnBlockedException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ArrayList(java.util.ArrayList) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) CountDownSessionFailureListener(org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer)

Example 12 with CountDownSessionFailureListener

use of org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener in project activemq-artemis by apache.

the class FailBackAutoTest method testAutoFailbackThenFailover.

@Test
public void testAutoFailbackThenFailover() throws Exception {
    createSessionFactory();
    ClientSession session = sendAndConsume(sf, true);
    CountDownSessionFailureListener listener = new CountDownSessionFailureListener(session);
    session.addFailureListener(listener);
    IntegrationTestLogger.LOGGER.info("Crashing live server...");
    liveServer.crash(session);
    ClientProducer producer = session.createProducer(ADDRESS);
    ClientMessage message = session.createMessage(true);
    setBody(0, message);
    producer.send(message);
    session.removeFailureListener(listener);
    listener = new CountDownSessionFailureListener(session);
    session.addFailureListener(listener);
    log.info("restarting live node now");
    liveServer.start();
    assertTrue("expected a session failure 1", listener.getLatch().await(5, TimeUnit.SECONDS));
    message = session.createMessage(true);
    setBody(1, message);
    producer.send(message);
    session.removeFailureListener(listener);
    listener = new CountDownSessionFailureListener(session);
    session.addFailureListener(listener);
    waitForBackup(sf, 10);
    IntegrationTestLogger.LOGGER.info("Crashing live server again...");
    liveServer.crash();
    assertTrue("expected a session failure 2", listener.getLatch().await(5, TimeUnit.SECONDS));
    session.close();
    wrapUpSessionFactory();
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownSessionFailureListener(org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 13 with CountDownSessionFailureListener

use of org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener in project activemq-artemis by apache.

the class FailoverTest method testLiveAndBackupLiveComesBackNewFactory.

@Test(timeout = 120000)
public void testLiveAndBackupLiveComesBackNewFactory() throws Exception {
    locator.setFailoverOnInitialConnection(true);
    createSessionFactory();
    final CountDownLatch latch = new CountDownLatch(1);
    ClientSession session = sendAndConsume(sf, true);
    session.addFailureListener(new CountDownSessionFailureListener(latch, session));
    backupServer.stop();
    liveServer.crash();
    // To reload security or other settings that are read during startup
    beforeRestart(liveServer);
    liveServer.start();
    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
    ClientMessage message = session.createMessage(true);
    setBody(0, message);
    producer.send(message);
    session.close();
    sf.close();
    createClientSessionFactory();
    session = createSession(sf);
    ClientConsumer cc = session.createConsumer(FailoverTestBase.ADDRESS);
    session.start();
    ClientMessage cm = cc.receive(5000);
    Assert.assertNotNull(cm);
    Assert.assertEquals("message0", cm.getBodyBuffer().readString());
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) CountDownSessionFailureListener(org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Aggregations

CountDownSessionFailureListener (org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener)13 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)11 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)10 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)9 Test (org.junit.Test)9 CountDownLatch (java.util.concurrent.CountDownLatch)5 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)5 SharedStoreSlavePolicy (org.apache.activemq.artemis.core.server.cluster.ha.SharedStoreSlavePolicy)3 ArrayList (java.util.ArrayList)2 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)2 ActiveMQUnBlockedException (org.apache.activemq.artemis.api.core.ActiveMQUnBlockedException)2 ActiveMQDuplicateIdException (org.apache.activemq.artemis.api.core.ActiveMQDuplicateIdException)1 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)1 ActiveMQTransactionOutcomeUnknownException (org.apache.activemq.artemis.api.core.ActiveMQTransactionOutcomeUnknownException)1 ActiveMQTransactionRolledBackException (org.apache.activemq.artemis.api.core.ActiveMQTransactionRolledBackException)1 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)1 ClientSessionFactoryInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)1 ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)1 ClusterManager (org.apache.activemq.artemis.core.server.cluster.ClusterManager)1 SharedStoreMasterPolicy (org.apache.activemq.artemis.core.server.cluster.ha.SharedStoreMasterPolicy)1