Search in sources :

Example 6 with SessionFailureListener

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

the class FailoverTest method testTimeoutOnFailoverTransactionCommit.

// https://issues.jboss.org/browse/HORNETQ-685
@Test(timeout = 120000)
public void testTimeoutOnFailoverTransactionCommit() throws Exception {
    locator.setCallTimeout(5000).setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setAckBatchSize(0).setReconnectAttempts(300).setRetryInterval(100);
    if (nodeManager instanceof InVMNodeManager) {
        ((InVMNodeManager) nodeManager).failoverPause = 5000L;
    }
    ClientSessionFactoryInternal sf1 = (ClientSessionFactoryInternal) createSessionFactory(locator);
    final ClientSession session = createSession(sf1, true, false, false);
    session.createQueue(FailoverTestBase.ADDRESS, RoutingType.MULTICAST, FailoverTestBase.ADDRESS, null, true);
    final CountDownLatch connectionFailed = new CountDownLatch(1);
    session.addFailureListener(new SessionFailureListener() {

        @Override
        public void beforeReconnect(ActiveMQException exception) {
        }

        @Override
        public void connectionFailed(ActiveMQException exception, boolean failedOver) {
        }

        @Override
        public void connectionFailed(ActiveMQException exception, boolean failedOver, String scaleDownTargetNodeID) {
            connectionFailed.countDown();
        }
    });
    final ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
    Xid xid = new XidImpl("uhuhuhu".getBytes(), 126512, "auhsduashd".getBytes());
    session.start(xid, XAResource.TMNOFLAGS);
    for (int i = 0; i < 500; i++) {
        ClientMessage message = session.createMessage(true);
        message.putIntProperty("counter", i);
        producer.send(message);
    }
    session.end(xid, XAResource.TMSUCCESS);
    session.prepare(xid);
    crash(true, session);
    try {
        session.commit(xid, false);
    } catch (XAException e) {
        // there is still an edge condition that we must deal with
        Assert.assertTrue(connectionFailed.await(10, TimeUnit.SECONDS));
        session.commit(xid, false);
    }
    ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);
    session.start();
    for (int i = 0; i < 500; i++) {
        ClientMessage m = consumer.receive(1000);
        Assert.assertNotNull(m);
        Assert.assertEquals(i, m.getIntProperty("counter").intValue());
    }
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) XAException(javax.transaction.xa.XAException) InVMNodeManager(org.apache.activemq.artemis.core.server.impl.InVMNodeManager) XidImpl(org.apache.activemq.artemis.core.transaction.impl.XidImpl) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) SessionFailureListener(org.apache.activemq.artemis.api.core.client.SessionFailureListener) CountDownSessionFailureListener(org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener) Xid(javax.transaction.xa.Xid) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) 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 SessionFailureListener

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

the class ReattachTest method testAsyncFailureWhileReattaching.

// Test an async (e.g. pinger) failure coming in while a connection manager is already reconnecting
@Test
public void testAsyncFailureWhileReattaching() throws Exception {
    final long retryInterval = 50;
    final double retryMultiplier = 1d;
    final int reconnectAttempts = 60;
    final long asyncFailDelay = 200;
    locator.setRetryInterval(retryInterval).setRetryIntervalMultiplier(retryMultiplier).setReconnectAttempts(reconnectAttempts).setConfirmationWindowSize(1024 * 1024);
    ClientSessionFactoryInternal sf = (ClientSessionFactoryInternal) createSessionFactory(locator);
    ClientSession session = sf.createSession(false, true, true);
    ClientSession session2 = sf.createSession(false, true, true);
    class MyFailureListener implements SessionFailureListener {

        volatile boolean failed;

        @Override
        public void connectionFailed(final ActiveMQException me, boolean failedOver) {
            failed = true;
        }

        @Override
        public void connectionFailed(final ActiveMQException me, boolean failedOver, String scaleDownTargetNodeID) {
            connectionFailed(me, failedOver);
        }

        @Override
        public void beforeReconnect(final ActiveMQException exception) {
        }
    }
    MyFailureListener listener = new MyFailureListener();
    session2.addFailureListener(listener);
    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.numberOfFailures = 10;
    InVMConnector.failOnCreateConnection = true;
    final RemotingConnection conn = ((ClientSessionInternal) session).getConnection();
    final RemotingConnection conn2 = ((ClientSessionInternal) session2).getConnection();
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                Thread.sleep(asyncFailDelay);
            } catch (InterruptedException ignore) {
            }
            conn2.fail(new ActiveMQNotConnectedException("Did not receive pong from server"));
        }
    };
    t.start();
    conn.fail(new ActiveMQNotConnectedException());
    Assert.assertTrue(listener.failed);
    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();
    session2.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) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) SessionFailureListener(org.apache.activemq.artemis.api.core.client.SessionFailureListener) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) 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 SessionFailureListener

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

the class ReconnectTest method internalTestInterruptReconnect.

public void internalTestInterruptReconnect(final boolean isNetty, final boolean interruptMainThread) throws Exception {
    final int pingPeriod = 1000;
    ActiveMQServer server = createServer(false, isNetty);
    server.start();
    try {
        ServerLocator locator = createFactory(isNetty).setClientFailureCheckPeriod(pingPeriod).setRetryInterval(500).setRetryIntervalMultiplier(1d).setReconnectAttempts(-1).setConfirmationWindowSize(1024 * 1024);
        ClientSessionFactoryInternal factory = (ClientSessionFactoryInternal) locator.createSessionFactory();
        // One for beforeReconnecto from the Factory, and one for the commit about to be done
        final CountDownLatch latchCommit = new CountDownLatch(2);
        final ArrayList<Thread> threadToBeInterrupted = new ArrayList<>();
        factory.addFailureListener(new SessionFailureListener() {

            @Override
            public void connectionFailed(ActiveMQException exception, boolean failedOver) {
            }

            @Override
            public void connectionFailed(final ActiveMQException me, boolean failedOver, String scaleDownTargetNodeID) {
                connectionFailed(me, failedOver);
            }

            @Override
            public void beforeReconnect(ActiveMQException exception) {
                threadToBeInterrupted.add(Thread.currentThread());
                System.out.println("Thread " + Thread.currentThread() + " reconnecting now");
                latchCommit.countDown();
            }
        });
        final ClientSessionInternal session = (ClientSessionInternal) factory.createSession();
        final AtomicInteger count = new AtomicInteger(0);
        final CountDownLatch latch = new CountDownLatch(1);
        session.addFailureListener(new SessionFailureListener() {

            @Override
            public void connectionFailed(final ActiveMQException me, boolean failedOver) {
                count.incrementAndGet();
                latch.countDown();
            }

            @Override
            public void connectionFailed(final ActiveMQException me, boolean failedOver, String scaleDownTargetNodeID) {
                connectionFailed(me, failedOver);
            }

            @Override
            public void beforeReconnect(final ActiveMQException exception) {
            }
        });
        server.stop();
        Thread tcommitt = new Thread() {

            @Override
            public void run() {
                latchCommit.countDown();
                try {
                    session.commit();
                } catch (ActiveMQException e) {
                    e.printStackTrace();
                }
            }
        };
        tcommitt.start();
        assertTrue(latchCommit.await(10, TimeUnit.SECONDS));
        // There should be only one thread
        assertEquals(1, threadToBeInterrupted.size());
        if (interruptMainThread) {
            tcommitt.interrupt();
        } else {
            for (Thread tint : threadToBeInterrupted) {
                tint.interrupt();
            }
        }
        tcommitt.join(5000);
        assertFalse(tcommitt.isAlive());
        locator.close();
    } finally {
    }
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) SessionFailureListener(org.apache.activemq.artemis.api.core.client.SessionFailureListener) ActiveMQServer(org.apache.activemq.artemis.core.server.ActiveMQServer) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator)

Example 9 with SessionFailureListener

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

the class ActiveMQMessageHandlerTest method testServerShutdownAndReconnect.

@Test
public void testServerShutdownAndReconnect() throws Exception {
    ActiveMQResourceAdapter qResourceAdapter = newResourceAdapter();
    qResourceAdapter.setReconnectAttempts(-1);
    qResourceAdapter.setCallTimeout(500L);
    qResourceAdapter.setRetryInterval(500L);
    MyBootstrapContext ctx = new MyBootstrapContext();
    qResourceAdapter.start(ctx);
    // This is just to register a listener
    final CountDownLatch failedLatch = new CountDownLatch(1);
    ClientSessionFactoryInternal factoryListener = (ClientSessionFactoryInternal) qResourceAdapter.getDefaultActiveMQConnectionFactory().getServerLocator().createSessionFactory();
    factoryListener.addFailureListener(new SessionFailureListener() {

        @Override
        public void connectionFailed(ActiveMQException exception, boolean failedOver) {
        }

        @Override
        public void connectionFailed(ActiveMQException exception, boolean failedOver, String scaleDownTargetNodeID) {
            connectionFailed(exception, failedOver);
        }

        @Override
        public void beforeReconnect(ActiveMQException exception) {
            failedLatch.countDown();
        }
    });
    ActiveMQActivationSpec spec = new ActiveMQActivationSpec();
    spec.setResourceAdapter(qResourceAdapter);
    spec.setUseJNDI(false);
    spec.setDestinationType("javax.jms.Queue");
    spec.setDestination(MDBQUEUE);
    CountDownLatch latch = new CountDownLatch(1);
    DummyMessageEndpoint endpoint = new DummyMessageEndpoint(latch);
    DummyMessageEndpointFactory endpointFactory = new DummyMessageEndpointFactory(endpoint, false);
    qResourceAdapter.endpointActivation(endpointFactory, spec);
    ClientSession session = locator.createSessionFactory().createSession();
    ClientProducer clientProducer = session.createProducer(MDBQUEUEPREFIXED);
    ClientMessage message = session.createMessage(true);
    message.getBodyBuffer().writeString("teststring");
    clientProducer.send(message);
    session.close();
    latch.await(5, TimeUnit.SECONDS);
    assertNotNull(endpoint.lastMessage);
    assertEquals(endpoint.lastMessage.getCoreMessage().getBodyBuffer().readString(), "teststring");
    server.stop();
    assertTrue(failedLatch.await(5, TimeUnit.SECONDS));
    qResourceAdapter.endpointDeactivation(endpointFactory, spec);
    qResourceAdapter.stop();
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) SessionFailureListener(org.apache.activemq.artemis.api.core.client.SessionFailureListener) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQResourceAdapter(org.apache.activemq.artemis.ra.ActiveMQResourceAdapter) ActiveMQActivationSpec(org.apache.activemq.artemis.ra.inflow.ActiveMQActivationSpec) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Aggregations

ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)9 SessionFailureListener (org.apache.activemq.artemis.api.core.client.SessionFailureListener)9 CountDownLatch (java.util.concurrent.CountDownLatch)7 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)7 ClientSessionFactoryInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)6 Test (org.junit.Test)6 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)5 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)4 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)4 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)3 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)3 ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)3 CountDownSessionFailureListener (org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 XAException (javax.transaction.xa.XAException)2 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)2 ActiveMQServer (org.apache.activemq.artemis.core.server.ActiveMQServer)2 InVMNodeManager (org.apache.activemq.artemis.core.server.impl.InVMNodeManager)2 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)2 ArrayList (java.util.ArrayList)1