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();
}
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());
}
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());
}
Aggregations