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