Search in sources :

Example 1 with CloseListener

use of org.apache.activemq.artemis.core.remoting.CloseListener in project activemq-artemis by apache.

the class JMSTopicConsumerTest method testTemporarySubscriptionDeleted.

@Test(timeout = 60000)
public void testTemporarySubscriptionDeleted() throws Exception {
    Connection connection = createConnection();
    try {
        TopicSession session = (TopicSession) connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Topic topic = session.createTopic(getTopicName());
        TopicSubscriber myNonDurSub = session.createSubscriber(topic);
        assertNotNull(myNonDurSub);
        Bindings bindingsForAddress = server.getPostOffice().getBindingsForAddress(new SimpleString(getTopicName()));
        Assert.assertEquals(2, bindingsForAddress.getBindings().size());
        session.close();
        final CountDownLatch latch = new CountDownLatch(1);
        server.getRemotingService().getConnections().iterator().next().addCloseListener(new CloseListener() {

            @Override
            public void connectionClosed() {
                latch.countDown();
            }
        });
        connection.close();
        latch.await(5, TimeUnit.SECONDS);
        bindingsForAddress = server.getPostOffice().getBindingsForAddress(new SimpleString(getTopicName()));
        Assert.assertEquals(1, bindingsForAddress.getBindings().size());
    } finally {
        connection.close();
    }
}
Also used : TopicSubscriber(javax.jms.TopicSubscriber) TopicSession(javax.jms.TopicSession) CloseListener(org.apache.activemq.artemis.core.remoting.CloseListener) Connection(javax.jms.Connection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) Topic(javax.jms.Topic) CountDownLatch(java.util.concurrent.CountDownLatch) Bindings(org.apache.activemq.artemis.core.postoffice.Bindings) Test(org.junit.Test)

Example 2 with CloseListener

use of org.apache.activemq.artemis.core.remoting.CloseListener in project activemq-artemis by apache.

the class CloseConnectionOnGCTest method testCloseOneConnectionOnGC.

@Test
public void testCloseOneConnectionOnGC() throws Exception {
    // Debug - don't remove this until intermittent failure with this test is fixed
    int initialConns = server.getRemotingService().getConnections().size();
    Assert.assertEquals(0, initialConns);
    Connection conn = cf.createConnection();
    WeakReference<Connection> wr = new WeakReference<>(conn);
    Assert.assertEquals(1, server.getRemotingService().getConnections().size());
    final CountDownLatch latch = new CountDownLatch(1);
    Iterator<RemotingConnection> connectionIterator = server.getRemotingService().getConnections().iterator();
    connectionIterator.next().addCloseListener(new CloseListener() {

        @Override
        public void connectionClosed() {
            latch.countDown();
        }
    });
    conn = null;
    ActiveMQTestBase.checkWeakReferences(wr);
    latch.await(5000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(0, server.getRemotingService().getConnections().size());
}
Also used : CloseListener(org.apache.activemq.artemis.core.remoting.CloseListener) WeakReference(java.lang.ref.WeakReference) Connection(javax.jms.Connection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with CloseListener

use of org.apache.activemq.artemis.core.remoting.CloseListener 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 4 with CloseListener

use of org.apache.activemq.artemis.core.remoting.CloseListener in project activemq-artemis by apache.

the class ServerSessionPacketHandler method internaltransferConnection.

private int internaltransferConnection(final CoreRemotingConnection newConnection, final int lastReceivedCommandID) {
    // We need to disable delivery on all the consumers while the transfer is occurring- otherwise packets might get
    // delivered
    // after the channel has transferred but *before* packets have been replayed - this will give the client the wrong
    // sequence of packets.
    // It is not sufficient to just stop the session, since right after stopping the session, another session start
    // might be executed
    // before we have transferred the connection, leaving it in a started state
    session.setTransferring(true);
    List<CloseListener> closeListeners = remotingConnection.removeCloseListeners();
    List<FailureListener> failureListeners = remotingConnection.removeFailureListeners();
    // Note. We do not destroy the replicating connection here. In the case the live server has really crashed
    // then the connection will get cleaned up anyway when the server ping timeout kicks in.
    // In the case the live server is really still up, i.e. a split brain situation (or in tests), then closing
    // the replicating connection will cause the outstanding responses to be be replayed on the live server,
    // if these reach the client who then subsequently fails over, on reconnection to backup, it will have
    // received responses that the backup did not know about.
    channel.transferConnection(newConnection);
    newConnection.syncIDGeneratorSequence(remotingConnection.getIDGeneratorSequence());
    Connection oldTransportConnection = remotingConnection.getTransportConnection();
    remotingConnection = newConnection;
    remotingConnection.setCloseListeners(closeListeners);
    remotingConnection.setFailureListeners(failureListeners);
    int serverLastReceivedCommandID = channel.getLastConfirmedCommandID();
    channel.replayCommands(lastReceivedCommandID);
    channel.setTransferring(false);
    session.setTransferring(false);
    // We do this because the old connection could be out of credits on netty
    // this will force anything to resume after the reattach through the ReadyListener callbacks
    oldTransportConnection.fireReady(true);
    return serverLastReceivedCommandID;
}
Also used : CloseListener(org.apache.activemq.artemis.core.remoting.CloseListener) FailureListener(org.apache.activemq.artemis.core.remoting.FailureListener) NettyConnection(org.apache.activemq.artemis.core.remoting.impl.netty.NettyConnection) Connection(org.apache.activemq.artemis.spi.core.remoting.Connection)

Example 5 with CloseListener

use of org.apache.activemq.artemis.core.remoting.CloseListener in project activemq-artemis by apache.

the class TemporaryQueueTest method testDeleteTemporaryQueueAfterConnectionIsClosed.

@Test
public void testDeleteTemporaryQueueAfterConnectionIsClosed() throws Exception {
    SimpleString queue = RandomUtil.randomSimpleString();
    SimpleString address = RandomUtil.randomSimpleString();
    session.createTemporaryQueue(address, queue);
    RemotingConnectionImpl conn = (RemotingConnectionImpl) server.getRemotingService().getConnections().iterator().next();
    final CountDownLatch latch = new CountDownLatch(1);
    conn.addCloseListener(new CloseListener() {

        @Override
        public void connectionClosed() {
            latch.countDown();
        }
    });
    session.close();
    sf.close();
    // wait for the closing listeners to be fired
    assertTrue("connection close listeners not fired", latch.await(2 * TemporaryQueueTest.CONNECTION_TTL, TimeUnit.MILLISECONDS));
    sf = addSessionFactory(createSessionFactory(locator));
    session = sf.createSession(false, true, true);
    session.start();
    try {
        session.createConsumer(queue);
        fail("temp queue must not exist after the remoting connection is closed");
    } catch (ActiveMQNonExistentQueueException neqe) {
    // ol
    } catch (ActiveMQException e) {
        fail("Invalid Exception type:" + e.getType());
    }
    session.close();
}
Also used : ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) CloseListener(org.apache.activemq.artemis.core.remoting.CloseListener) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) RemotingConnectionImpl(org.apache.activemq.artemis.core.protocol.core.impl.RemotingConnectionImpl) CountDownLatch(java.util.concurrent.CountDownLatch) ActiveMQNonExistentQueueException(org.apache.activemq.artemis.api.core.ActiveMQNonExistentQueueException) Test(org.junit.Test)

Aggregations

CloseListener (org.apache.activemq.artemis.core.remoting.CloseListener)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 Test (org.junit.Test)7 RemotingConnection (org.apache.activemq.artemis.spi.core.protocol.RemotingConnection)5 Connection (javax.jms.Connection)4 WeakReference (java.lang.ref.WeakReference)3 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)3 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)3 Interceptor (org.apache.activemq.artemis.api.core.Interceptor)2 ServerLocator (org.apache.activemq.artemis.api.core.client.ServerLocator)2 Packet (org.apache.activemq.artemis.core.protocol.core.Packet)2 Session (javax.jms.Session)1 Topic (javax.jms.Topic)1 TopicSession (javax.jms.TopicSession)1 TopicSubscriber (javax.jms.TopicSubscriber)1 ActiveMQInternalErrorException (org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException)1 ActiveMQNonExistentQueueException (org.apache.activemq.artemis.api.core.ActiveMQNonExistentQueueException)1 TransportConfiguration (org.apache.activemq.artemis.api.core.TransportConfiguration)1 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)1 ClientSessionFactory (org.apache.activemq.artemis.api.core.client.ClientSessionFactory)1