如果您不處理異常,您的channel
將被關閉。
使用autoAck = false
消息將被重新排隊。
在消費過程中處理錯誤總是一個好習慣。
BTW它的存在,你可以使用異常處理程序:
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
final ExceptionHandler eh = new DefaultExceptionHandler() {
@Override
public void handleConsumerException(Channel channel, Throwable exception, Consumer consumer, String consumerTag, String methodName) {
System.out.println(" - Error raised by: " + channel.getChannelNumber());
}
};
factory.setExceptionHandler(eh);
final Connection connection = factory.newConnection();
final Channel channel = connection.createChannel();
channel.queueDeclare("my_queue",true,false,false,null);
channel.basicConsume("my_queue", true, new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("Received...");
System.out.println("error:"+ Integer.parseInt("RAISE_AN_ERROR"));
的錯誤會被重定向到處理程序和渠道不會被關閉。
對於我而言,您應該始終處理事件中的錯誤。
我們可以設置一個'ForgivingExceptionHandler'而不是執行另一個避免關閉通道 – Jaskey