use of org.apache.activemq.artemis.api.core.client.SessionFailureListener in project activemq-artemis by apache.
the class FailoverTest method testTimeoutOnFailoverTransactionCommit.
// https://issues.jboss.org/browse/HORNETQ-685
@Test(timeout = 120000)
public void testTimeoutOnFailoverTransactionCommit() throws Exception {
locator.setCallTimeout(5000).setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setAckBatchSize(0).setReconnectAttempts(300).setRetryInterval(100);
if (nodeManager instanceof InVMNodeManager) {
((InVMNodeManager) nodeManager).failoverPause = 5000L;
}
ClientSessionFactoryInternal sf1 = (ClientSessionFactoryInternal) createSessionFactory(locator);
final ClientSession session = createSession(sf1, true, 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);
Xid xid = new XidImpl("uhuhuhu".getBytes(), 126512, "auhsduashd".getBytes());
session.start(xid, XAResource.TMNOFLAGS);
for (int i = 0; i < 500; i++) {
ClientMessage message = session.createMessage(true);
message.putIntProperty("counter", i);
producer.send(message);
}
session.end(xid, XAResource.TMSUCCESS);
session.prepare(xid);
crash(true, session);
try {
session.commit(xid, false);
} catch (XAException e) {
// there is still an edge condition that we must deal with
Assert.assertTrue(connectionFailed.await(10, TimeUnit.SECONDS));
session.commit(xid, false);
}
ClientConsumer consumer = session.createConsumer(FailoverTestBase.ADDRESS);
session.start();
for (int i = 0; i < 500; i++) {
ClientMessage m = consumer.receive(1000);
Assert.assertNotNull(m);
Assert.assertEquals(i, m.getIntProperty("counter").intValue());
}
}
use of org.apache.activemq.artemis.api.core.client.SessionFailureListener in project activemq-artemis by apache.
the class ReattachTest method testAsyncFailureWhileReattaching.
// Test an async (e.g. pinger) failure coming in while a connection manager is already reconnecting
@Test
public void testAsyncFailureWhileReattaching() throws Exception {
final long retryInterval = 50;
final double retryMultiplier = 1d;
final int reconnectAttempts = 60;
final long asyncFailDelay = 200;
locator.setRetryInterval(retryInterval).setRetryIntervalMultiplier(retryMultiplier).setReconnectAttempts(reconnectAttempts).setConfirmationWindowSize(1024 * 1024);
ClientSessionFactoryInternal sf = (ClientSessionFactoryInternal) createSessionFactory(locator);
ClientSession session = sf.createSession(false, true, true);
ClientSession session2 = sf.createSession(false, true, true);
class MyFailureListener implements SessionFailureListener {
volatile boolean failed;
@Override
public void connectionFailed(final ActiveMQException me, boolean failedOver) {
failed = true;
}
@Override
public void connectionFailed(final ActiveMQException me, boolean failedOver, String scaleDownTargetNodeID) {
connectionFailed(me, failedOver);
}
@Override
public void beforeReconnect(final ActiveMQException exception) {
}
}
MyFailureListener listener = new MyFailureListener();
session2.addFailureListener(listener);
session.createQueue(ReattachTest.ADDRESS, ReattachTest.ADDRESS, null, false);
ClientProducer producer = session.createProducer(ReattachTest.ADDRESS);
final int numMessages = 1000;
for (int i = 0; i < numMessages; i++) {
ClientMessage message = session.createMessage(ActiveMQTextMessage.TYPE, false, 0, System.currentTimeMillis(), (byte) 1);
message.putIntProperty(new SimpleString("count"), i);
message.getBodyBuffer().writeString("aardvarks");
producer.send(message);
}
ClientConsumer consumer = session.createConsumer(ReattachTest.ADDRESS);
InVMConnector.numberOfFailures = 10;
InVMConnector.failOnCreateConnection = true;
final RemotingConnection conn = ((ClientSessionInternal) session).getConnection();
final RemotingConnection conn2 = ((ClientSessionInternal) session2).getConnection();
Thread t = new Thread() {
@Override
public void run() {
try {
Thread.sleep(asyncFailDelay);
} catch (InterruptedException ignore) {
}
conn2.fail(new ActiveMQNotConnectedException("Did not receive pong from server"));
}
};
t.start();
conn.fail(new ActiveMQNotConnectedException());
Assert.assertTrue(listener.failed);
session.start();
for (int i = 0; i < numMessages; i++) {
ClientMessage message = consumer.receive(500);
Assert.assertNotNull(message);
Assert.assertEquals("aardvarks", message.getBodyBuffer().readString());
Assert.assertEquals(i, message.getIntProperty("count").intValue());
message.acknowledge();
}
ClientMessage message = consumer.receiveImmediate();
Assert.assertNull(message);
session.close();
session2.close();
sf.close();
t.join();
}
use of org.apache.activemq.artemis.api.core.client.SessionFailureListener in project activemq-artemis by apache.
the class ReconnectTest method internalTestInterruptReconnect.
public void internalTestInterruptReconnect(final boolean isNetty, final boolean interruptMainThread) throws Exception {
final int pingPeriod = 1000;
ActiveMQServer server = createServer(false, isNetty);
server.start();
try {
ServerLocator locator = createFactory(isNetty).setClientFailureCheckPeriod(pingPeriod).setRetryInterval(500).setRetryIntervalMultiplier(1d).setReconnectAttempts(-1).setConfirmationWindowSize(1024 * 1024);
ClientSessionFactoryInternal factory = (ClientSessionFactoryInternal) locator.createSessionFactory();
// One for beforeReconnecto from the Factory, and one for the commit about to be done
final CountDownLatch latchCommit = new CountDownLatch(2);
final ArrayList<Thread> threadToBeInterrupted = new ArrayList<>();
factory.addFailureListener(new SessionFailureListener() {
@Override
public void connectionFailed(ActiveMQException exception, boolean failedOver) {
}
@Override
public void connectionFailed(final ActiveMQException me, boolean failedOver, String scaleDownTargetNodeID) {
connectionFailed(me, failedOver);
}
@Override
public void beforeReconnect(ActiveMQException exception) {
threadToBeInterrupted.add(Thread.currentThread());
System.out.println("Thread " + Thread.currentThread() + " reconnecting now");
latchCommit.countDown();
}
});
final ClientSessionInternal 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 tcommitt = new Thread() {
@Override
public void run() {
latchCommit.countDown();
try {
session.commit();
} catch (ActiveMQException e) {
e.printStackTrace();
}
}
};
tcommitt.start();
assertTrue(latchCommit.await(10, TimeUnit.SECONDS));
// There should be only one thread
assertEquals(1, threadToBeInterrupted.size());
if (interruptMainThread) {
tcommitt.interrupt();
} else {
for (Thread tint : threadToBeInterrupted) {
tint.interrupt();
}
}
tcommitt.join(5000);
assertFalse(tcommitt.isAlive());
locator.close();
} finally {
}
}
use of org.apache.activemq.artemis.api.core.client.SessionFailureListener in project activemq-artemis by apache.
the class ActiveMQMessageHandlerTest method testServerShutdownAndReconnect.
@Test
public void testServerShutdownAndReconnect() throws Exception {
ActiveMQResourceAdapter qResourceAdapter = newResourceAdapter();
qResourceAdapter.setReconnectAttempts(-1);
qResourceAdapter.setCallTimeout(500L);
qResourceAdapter.setRetryInterval(500L);
MyBootstrapContext ctx = new MyBootstrapContext();
qResourceAdapter.start(ctx);
// This is just to register a listener
final CountDownLatch failedLatch = new CountDownLatch(1);
ClientSessionFactoryInternal factoryListener = (ClientSessionFactoryInternal) qResourceAdapter.getDefaultActiveMQConnectionFactory().getServerLocator().createSessionFactory();
factoryListener.addFailureListener(new SessionFailureListener() {
@Override
public void connectionFailed(ActiveMQException exception, boolean failedOver) {
}
@Override
public void connectionFailed(ActiveMQException exception, boolean failedOver, String scaleDownTargetNodeID) {
connectionFailed(exception, failedOver);
}
@Override
public void beforeReconnect(ActiveMQException exception) {
failedLatch.countDown();
}
});
ActiveMQActivationSpec spec = new ActiveMQActivationSpec();
spec.setResourceAdapter(qResourceAdapter);
spec.setUseJNDI(false);
spec.setDestinationType("javax.jms.Queue");
spec.setDestination(MDBQUEUE);
CountDownLatch latch = new CountDownLatch(1);
DummyMessageEndpoint endpoint = new DummyMessageEndpoint(latch);
DummyMessageEndpointFactory endpointFactory = new DummyMessageEndpointFactory(endpoint, false);
qResourceAdapter.endpointActivation(endpointFactory, spec);
ClientSession session = locator.createSessionFactory().createSession();
ClientProducer clientProducer = session.createProducer(MDBQUEUEPREFIXED);
ClientMessage message = session.createMessage(true);
message.getBodyBuffer().writeString("teststring");
clientProducer.send(message);
session.close();
latch.await(5, TimeUnit.SECONDS);
assertNotNull(endpoint.lastMessage);
assertEquals(endpoint.lastMessage.getCoreMessage().getBodyBuffer().readString(), "teststring");
server.stop();
assertTrue(failedLatch.await(5, TimeUnit.SECONDS));
qResourceAdapter.endpointDeactivation(endpointFactory, spec);
qResourceAdapter.stop();
}
Aggregations