SimpleFailoverListener failoverListener = new SimpleFailoverListener();
((JBossConnection)conn).registerFailoverListener(failoverListener);
ClientConnectionDelegate del = (ClientConnectionDelegate)jbc.getDelegate();
ConnectionState state = (ConnectionState)del.getState();
int initialServerID = state.getServerID();
assertEquals(1, initialServerID);
Session sess = conn.createSession(false, Session.CLIENT_ACKNOWLEDGE);
MessageProducer prod = sess.createProducer(queue[1]);
MessageConsumer cons = sess.createConsumer(queue[1]);
final int NUM_MESSAGES = 100;
for (int i = 0; i < NUM_MESSAGES; i++)
{
TextMessage tm = sess.createTextMessage("message:" + i);
prod.send(tm);
}
conn.start();
//Now consume half of the messages but don't ack them these will end up in
//client side toAck list
for (int i = 0; i < NUM_MESSAGES / 2; i++)
{
TextMessage tm = (TextMessage)cons.receive(1000);
assertNotNull(tm);
assertEquals("message:" + i, tm.getText());
}
//So now, messages should be in queue[1] on server 1
//So we now kill server 1
//Which should cause transparent failover of connection conn onto server 2
ServerManagement.kill(1);
// wait for the client-side failover to complete
while(true)
{
FailoverEvent event = failoverListener.getEvent(30000);
if (event != null && FailoverEvent.FAILOVER_COMPLETED == event.getType())
{
break;
}
if (event == null)
{
fail("Did not get expected FAILOVER_COMPLETED event");
}
}
state = (ConnectionState)del.getState();
int finalServerID = state.getServerID();
conn.start();
//Now should be able to consume the rest of the messages