I have one simple rabbitmq publisher JAVA application that connects to HAProxy to send message to RabbitMQ cluster. Following is the code:
public static void main(String[] args) throws IOException, TimeoutException {
ConnectionFactory cf = new ConnectionFactory();
Address haAddr = new Address("192.168.11.113", 5671);
cf.setUsername("test25");
cf.setPassword("test25");
Connection con = cf.newConnection(new Address[]{haAddr});
// con.addShutdownListener(new ConnectionShutdownListener());
Channel channel = con.createChannel();
for(int index = 0; index < 500; index++){
sendMessage(channel, "m"+ index);
}
System.out.println("done");
}
private static void sendMessage(Channel channel, String msg){
try {
channel.basicPublish("", QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, msg.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
This App creates a connection with HAProxy and then keep on sending messages to the RabbitMQ queue. I tested a case where application has created a channel from Connection(Connection with HAProxy) and started sending messages. While it was sending messages, I made HAProxy service stopped. I observed that though HAProxy wasn't running channel.basicPublish was executed successfully for all subsequent messages but messages were not inserted in queue. I expect channel.basicPublish to throw some exception if HAProxy is down so that app can handle this scenario and message loss can be avoided.Is there any approach to achieve it?