Search in sources :

Example 1 with SessionFailureListener

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

the class JMSUtil method crash.

public static void crash(ActiveMQServer server, ClientSession... sessions) throws Exception {
    final CountDownLatch latch = new CountDownLatch(sessions.length);
    class MyListener implements SessionFailureListener {

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

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

        @Override
        public void beforeReconnect(ActiveMQException exception) {
            System.out.println("MyListener.beforeReconnect");
        }
    }
    for (ClientSession session : sessions) {
        session.addFailureListener(new MyListener());
    }
    ClusterManager clusterManager = server.getClusterManager();
    clusterManager.clear();
    server.fail(true);
    // Wait to be informed of failure
    boolean ok = latch.await(10000, TimeUnit.MILLISECONDS);
    Assert.assertTrue(ok);
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) CountDownLatch(java.util.concurrent.CountDownLatch) SessionFailureListener(org.apache.activemq.artemis.api.core.client.SessionFailureListener) ClusterManager(org.apache.activemq.artemis.core.server.cluster.ClusterManager)

Example 2 with SessionFailureListener

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

the class SessionTest method testFailureListenerRemoved.

@Test
public void testFailureListenerRemoved() throws Exception {
    cf = createSessionFactory(locator);
    try {
        ClientSession clientSession = cf.createSession(false, true, true);
        class MyFailureListener implements SessionFailureListener {

            boolean called = false;

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

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

            @Override
            public void beforeReconnect(final ActiveMQException me) {
            }
        }
        MyFailureListener listener = new MyFailureListener();
        clientSession.addFailureListener(listener);
        Assert.assertTrue(clientSession.removeFailureListener(listener));
        clientSession.close();
        server.stop();
        Assert.assertFalse(listener.called);
    } finally {
        ((ClientSessionFactoryInternal) cf).causeExit();
        cf.close();
    }
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) SessionFailureListener(org.apache.activemq.artemis.api.core.client.SessionFailureListener) CountDownSessionFailureListener(org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener) Test(org.junit.Test)

Example 3 with SessionFailureListener

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

the class FailoverTest method testTimeoutOnFailoverTransactionCommitTimeoutCommunication.

/**
 * This test would fail one in three or five times,
 * where the commit would leave the session dirty after a timeout.
 */
@Test(timeout = 120000)
public void testTimeoutOnFailoverTransactionCommitTimeoutCommunication() throws Exception {
    locator.setCallTimeout(1000).setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setAckBatchSize(0).setReconnectAttempts(300).setRetryInterval(500);
    if (nodeManager instanceof InVMNodeManager) {
        ((InVMNodeManager) nodeManager).failoverPause = 6000L;
    }
    ClientSessionFactoryInternal sf1 = (ClientSessionFactoryInternal) createSessionFactory(locator);
    final ClientSession session = createSession(sf1, false, 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);
    for (int i = 0; i < 500; i++) {
        ClientMessage message = session.createMessage(true);
        message.putIntProperty("counter", i);
        producer.send(message);
    }
    session.commit();
    ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);
    session.start();
    ClientMessage m = null;
    for (int i = 0; i < 500; i++) {
        m = consumer.receive(1000);
        Assert.assertNotNull(m);
        Assert.assertEquals(i, m.getIntProperty("counter").intValue());
    }
    m.acknowledge();
    crash(false, session);
    try {
        session.commit();
        fail("Exception expected");
    } catch (Exception expected) {
        expected.printStackTrace();
    }
    Thread.sleep(2000);
    m = null;
    for (int i = 0; i < 500; i++) {
        m = consumer.receive(1000);
        Assert.assertNotNull(m);
        Assert.assertEquals(i, m.getIntProperty("counter").intValue());
    }
    m.acknowledge();
    session.commit();
}
Also used : ClientSessionFactoryInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal) InVMNodeManager(org.apache.activemq.artemis.core.server.impl.InVMNodeManager) 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) ActiveMQTransactionOutcomeUnknownException(org.apache.activemq.artemis.api.core.ActiveMQTransactionOutcomeUnknownException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQDuplicateIdException(org.apache.activemq.artemis.api.core.ActiveMQDuplicateIdException) ActiveMQTransactionRolledBackException(org.apache.activemq.artemis.api.core.ActiveMQTransactionRolledBackException) ActiveMQObjectClosedException(org.apache.activemq.artemis.api.core.ActiveMQObjectClosedException) XAException(javax.transaction.xa.XAException) 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 4 with SessionFailureListener

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

the class PingTest method testClientFailureNoServerPing.

/*
   * Test the client triggering failure due to no ping from server received in time
   */
@Test
public void testClientFailureNoServerPing() throws Exception {
    // server must received at least one ping from the client to pass
    // so that the server connection TTL is configured with the client value
    final CountDownLatch pingOnServerLatch = new CountDownLatch(2);
    server.getRemotingService().addIncomingInterceptor(new Interceptor() {

        @Override
        public boolean intercept(final Packet packet, final RemotingConnection connection) throws ActiveMQException {
            if (packet.getType() == PacketImpl.PING) {
                pingOnServerLatch.countDown();
            }
            return true;
        }
    });
    TransportConfiguration transportConfig = new TransportConfiguration(INVM_CONNECTOR_FACTORY);
    ServerLocator locator = addServerLocator(ActiveMQClient.createServerLocatorWithoutHA(transportConfig));
    locator.setClientFailureCheckPeriod(PingTest.CLIENT_FAILURE_CHECK_PERIOD);
    locator.setConnectionTTL(PingTest.CLIENT_FAILURE_CHECK_PERIOD * 2);
    ClientSessionFactory csf = createSessionFactory(locator);
    ClientSession session = csf.createSession(false, true, true);
    Assert.assertEquals(1, ((ClientSessionFactoryInternal) csf).numConnections());
    final CountDownLatch clientLatch = new CountDownLatch(1);
    SessionFailureListener clientListener = new SessionFailureListener() {

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

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

        @Override
        public void beforeReconnect(final ActiveMQException exception) {
        }
    };
    final CountDownLatch serverLatch = new CountDownLatch(1);
    CloseListener serverListener = new CloseListener() {

        @Override
        public void connectionClosed() {
            serverLatch.countDown();
        }
    };
    session.addFailureListener(clientListener);
    CoreRemotingConnection serverConn = null;
    while (serverConn == null) {
        Set<RemotingConnection> conns = server.getRemotingService().getConnections();
        if (!conns.isEmpty()) {
            serverConn = (CoreRemotingConnection) server.getRemotingService().getConnections().iterator().next();
        } else {
            // It's async so need to wait a while
            Thread.sleep(10);
        }
    }
    serverConn.addCloseListener(serverListener);
    Assert.assertTrue("server has not received any ping from the client", pingOnServerLatch.await(4000, TimeUnit.MILLISECONDS));
    // we let the server receives at least 1 ping (so that it uses the client ConnectionTTL value)
    // Setting the handler to null will prevent server sending pings back to client
    serverConn.getChannel(0, -1).setHandler(null);
    Assert.assertTrue(clientLatch.await(8 * PingTest.CLIENT_FAILURE_CHECK_PERIOD, TimeUnit.MILLISECONDS));
    // Server connection will be closed too, when client closes client side connection after failure is detected
    Assert.assertTrue(serverLatch.await(2 * server.getConfiguration().getConnectionTtlCheckInterval(), TimeUnit.MILLISECONDS));
    long start = System.currentTimeMillis();
    while (true) {
        if (!server.getRemotingService().getConnections().isEmpty() && System.currentTimeMillis() - start < 10000) {
            Thread.sleep(500);
        } else {
            break;
        }
    }
    Assert.assertTrue(server.getRemotingService().getConnections().isEmpty());
    session.close();
    csf.close();
    locator.close();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) CoreRemotingConnection(org.apache.activemq.artemis.core.protocol.core.CoreRemotingConnection) TransportConfiguration(org.apache.activemq.artemis.api.core.TransportConfiguration) CountDownLatch(java.util.concurrent.CountDownLatch) SessionFailureListener(org.apache.activemq.artemis.api.core.client.SessionFailureListener) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) CloseListener(org.apache.activemq.artemis.core.remoting.CloseListener) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) Interceptor(org.apache.activemq.artemis.api.core.Interceptor) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 5 with SessionFailureListener

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

the class ReconnectTest method internalTestReconnect.

public void internalTestReconnect(final boolean isNetty) throws Exception {
    final int pingPeriod = 1000;
    ActiveMQServer server = createServer(false, isNetty);
    server.start();
    ClientSessionInternal session = null;
    try {
        ServerLocator locator = createFactory(isNetty).setClientFailureCheckPeriod(pingPeriod).setRetryInterval(500).setRetryIntervalMultiplier(1d).setReconnectAttempts(-1).setConfirmationWindowSize(1024 * 1024);
        ClientSessionFactory factory = createSessionFactory(locator);
        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.sleep((pingPeriod * 2));
        server.start();
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
        // Some time to let possible loops to occur
        Thread.sleep(500);
        Assert.assertEquals(1, count.get());
        locator.close();
    } finally {
        try {
            session.close();
        } catch (Throwable e) {
        }
        server.stop();
    }
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) 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) ClientSessionFactory(org.apache.activemq.artemis.api.core.client.ClientSessionFactory) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator)

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