@Test public void testAutomaticFailover() throws Exception { HornetQConnectionFactory jbcf = HornetQJMSClient.createConnectionFactoryWithHA(JMSFactoryType.CF, livetc); jbcf.setReconnectAttempts(-1); jbcf.setBlockOnDurableSend(true); jbcf.setBlockOnNonDurableSend(true); // Note we set consumer window size to a value so we can verify that consumer credit re-sending // works properly on failover // The value is small enough that credits will have to be resent several time final int numMessages = 10; final int bodySize = 1000; jbcf.setConsumerWindowSize(numMessages * bodySize / 10); HornetQConnection conn = JMSUtil.createConnectionAndWaitForTopology(jbcf, 2, 5); MyFailoverListener listener = new MyFailoverListener(); conn.setFailoverListener(listener); Session sess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE); ClientSession coreSession = ((HornetQSession) sess).getCoreSession(); SimpleString jmsQueueName = new SimpleString(HornetQDestination.JMS_QUEUE_ADDRESS_PREFIX + "myqueue"); coreSession.createQueue(jmsQueueName, jmsQueueName, null, true); Queue queue = sess.createQueue("myqueue"); MessageProducer producer = sess.createProducer(queue); producer.setDeliveryMode(DeliveryMode.PERSISTENT); MessageConsumer consumer = sess.createConsumer(queue); byte[] body = RandomUtil.randomBytes(bodySize); for (int i = 0; i < numMessages; i++) { BytesMessage bm = sess.createBytesMessage(); bm.writeBytes(body); producer.send(bm); } conn.start(); JMSFailoverListenerTest.log.info("sent messages and started connection"); Thread.sleep(2000); JMSUtil.crash(liveService, ((HornetQSession) sess).getCoreSession()); Assert.assertEquals(FailoverEventType.FAILURE_DETECTED, listener.getEventTypeList().get(0)); for (int i = 0; i < numMessages; i++) { JMSFailoverListenerTest.log.info("got message " + i); BytesMessage bm = (BytesMessage) consumer.receive(1000); Assert.assertNotNull(bm); Assert.assertEquals(body.length, bm.getBodyLength()); } TextMessage tm = (TextMessage) consumer.receiveNoWait(); Assert.assertNull(tm); Assert.assertEquals(FailoverEventType.FAILOVER_COMPLETED, listener.getEventTypeList().get(1)); conn.close(); Assert.assertEquals( "Expected 2 FailoverEvents to be triggered", 2, listener.getEventTypeList().size()); }