final int reconnectAttempts = -1;
final long asyncFailDelay = 2000;
ClientSessionFactoryInternal sf = createFactory(false);
sf.setRetryInterval(retryInterval);
sf.setRetryIntervalMultiplier(retryMultiplier);
sf.setReconnectAttempts(reconnectAttempts);
sf.setConfirmationWindowSize(1024 * 1024);
ClientSession session = sf.createSession(false, true, true);
ClientSession session2 = sf.createSession(false, true, true);
class MyFailureListener implements SessionFailureListener
{
volatile boolean failed;
public void connectionFailed(final HornetQException me)
{
failed = true;
}
public void beforeReconnect(final HornetQException 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(HornetQTextMessage.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 HornetQException(HornetQException.NOT_CONNECTED, "Did not receive pong from server"));
}
};
t.start();
conn.fail(new HornetQException(HornetQException.NOT_CONNECTED));
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();
}