This example demonstrates how you can listen on failover event on the client side.
In this example there are two nodes running in a cluster, both server will be running for start, but after a while the first server will crash. This will trigger an fail oever event.
To run the example, simply type mvn verify
from this directory
InitialContext initialContext = getContext(0);
Queue queue = (Queue)initialContext.lookup("/queue/exampleQueue");
ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup("/ConnectionFactory");
Thread.sleep(5000);
connectionA = connectionFactory.createConnection();
((HornetQConnection)connectionA).setFailoverListener(new FailoverListenerImpl());
Session sessionA = connectionA.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producerA = sessionA.createProducer(queue);
final int numMessages = 10;
for (int i = 0; i < numMessages; i++)
{
TextMessage messageA = sessionA.createTextMessage("A:This is text message " + i);
producerA.send(messageA);
System.out.println("Sent message: " + messageA.getText());
}
connectionA.start();
consume(sessionA, queue, numMessages, "A");
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
if (connectionA != null)
{
connectionA.close();
}
if (initialContext != null)
{
initialContext.close();
}