Search in sources :

Example 6 with CloseListener

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

the class TemporaryQueueTest method testDeleteTemporaryQueueWhenClientCrash.

@Test
public void testDeleteTemporaryQueueWhenClientCrash() throws Exception {
    session.close();
    sf.close();
    final SimpleString queue = RandomUtil.randomSimpleString();
    SimpleString address = RandomUtil.randomSimpleString();
    // 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(1);
    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;
        }
    });
    ServerLocator locator = createInVMNonHALocator();
    locator.setConnectionTTL(TemporaryQueueTest.CONNECTION_TTL);
    sf = addSessionFactory(createSessionFactory(locator));
    session = sf.createSession(false, true, true);
    session.createTemporaryQueue(address, queue);
    assertTrue("server has not received any ping from the client", pingOnServerLatch.await(2 * server.getConfiguration().getConnectionTtlCheckInterval(), TimeUnit.MILLISECONDS));
    assertEquals(1, server.getConnectionCount());
    RemotingConnection remotingConnection = server.getRemotingService().getConnections().iterator().next();
    final CountDownLatch serverCloseLatch = new CountDownLatch(1);
    remotingConnection.addCloseListener(new CloseListener() {

        @Override
        public void connectionClosed() {
            serverCloseLatch.countDown();
        }
    });
    ((ClientSessionInternal) session).getConnection().fail(new ActiveMQInternalErrorException("simulate a client failure"));
    // let some time for the server to clean the connections
    assertTrue("server has not closed the connection", serverCloseLatch.await(2 * server.getConfiguration().getConnectionTtlCheckInterval() + 2 * TemporaryQueueTest.CONNECTION_TTL, TimeUnit.MILLISECONDS));
    // The next getCount will be asynchronously done at the end of failure. We will wait some time until it has reached there.
    for (long timeout = System.currentTimeMillis() + 5000; timeout > System.currentTimeMillis() && server.getConnectionCount() > 0; ) {
        Thread.sleep(1);
    }
    assertEquals(0, server.getConnectionCount());
    session.close();
    sf.close();
    ServerLocator locator2 = createInVMNonHALocator();
    sf = addSessionFactory(createSessionFactory(locator2));
    session = sf.createSession(false, true, true);
    session.start();
    ActiveMQAction activeMQAction = new ActiveMQAction() {

        @Override
        public void run() throws ActiveMQException {
            session.createConsumer(queue);
        }
    };
    ActiveMQTestBase.expectActiveMQException("temp queue must not exist after the server detected the client crash", ActiveMQExceptionType.QUEUE_DOES_NOT_EXIST, activeMQAction);
    session.close();
    locator2.close();
}
Also used : Packet(org.apache.activemq.artemis.core.protocol.core.Packet) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) CloseListener(org.apache.activemq.artemis.core.remoting.CloseListener) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ActiveMQInternalErrorException(org.apache.activemq.artemis.api.core.ActiveMQInternalErrorException) CountDownLatch(java.util.concurrent.CountDownLatch) Interceptor(org.apache.activemq.artemis.api.core.Interceptor) ServerLocator(org.apache.activemq.artemis.api.core.client.ServerLocator) Test(org.junit.Test)

Example 7 with CloseListener

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

the class CloseConnectionOnGCTest method testCloseSeveralConnectionsWithSessionsOnGC.

@Test
public void testCloseSeveralConnectionsWithSessionsOnGC() throws Exception {
    Connection conn1 = cf.createConnection();
    Connection conn2 = cf.createConnection();
    Connection conn3 = cf.createConnection();
    WeakReference<Connection> wr1 = new WeakReference<>(conn1);
    WeakReference<Connection> wr2 = new WeakReference<>(conn2);
    WeakReference<Connection> wr3 = new WeakReference<>(conn3);
    Session sess1 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Session sess2 = conn1.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Session sess3 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Session sess4 = conn2.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Session sess5 = conn3.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Session sess6 = conn3.createSession(false, Session.AUTO_ACKNOWLEDGE);
    Session sess7 = conn3.createSession(false, Session.AUTO_ACKNOWLEDGE);
    final CountDownLatch latch = new CountDownLatch(3);
    Iterator<RemotingConnection> connectionIterator = server.getRemotingService().getConnections().iterator();
    while (connectionIterator.hasNext()) {
        RemotingConnection remotingConnection = connectionIterator.next();
        remotingConnection.addCloseListener(new CloseListener() {

            @Override
            public void connectionClosed() {
                latch.countDown();
            }
        });
    }
    sess1 = sess2 = sess3 = sess4 = sess5 = sess6 = sess7 = null;
    conn1 = null;
    conn2 = null;
    conn3 = null;
    ActiveMQTestBase.checkWeakReferences(wr1, wr2, wr3);
    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) Session(javax.jms.Session) Test(org.junit.Test)

Example 8 with CloseListener

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

the class CloseConnectionOnGCTest method testCloseSeveralConnectionOnGC.

@Test
public void testCloseSeveralConnectionOnGC() throws Exception {
    Connection conn1 = cf.createConnection();
    Connection conn2 = cf.createConnection();
    Connection conn3 = cf.createConnection();
    WeakReference<Connection> wr1 = new WeakReference<>(conn1);
    WeakReference<Connection> wr2 = new WeakReference<>(conn2);
    WeakReference<Connection> wr3 = new WeakReference<>(conn3);
    Assert.assertEquals(3, server.getRemotingService().getConnections().size());
    final CountDownLatch latch = new CountDownLatch(3);
    Iterator<RemotingConnection> connectionIterator = server.getRemotingService().getConnections().iterator();
    while (connectionIterator.hasNext()) {
        RemotingConnection remotingConnection = connectionIterator.next();
        remotingConnection.addCloseListener(new CloseListener() {

            @Override
            public void connectionClosed() {
                latch.countDown();
            }
        });
    }
    conn1 = null;
    conn2 = null;
    conn3 = null;
    ActiveMQTestBase.checkWeakReferences(wr1, wr2, wr3);
    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)

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