RabbitMQ Java 客户端:当在消费者的 handleDelivery() 方法中抛出 RuntimeException 时会发生什么?
RabbitMQ Java client: What happens when a RuntimeException is thrown inside a consumer's handleDelivery() method?
RabbitMQ Java 客户端的 Consumer
接口似乎没有任何东西,例如 handleException()
方法。
那么 RuntimeException
被扔进 Consumer.handleDelivery()
里面会有什么后果?
人们可能会期望以某种方式记录异常,并且消费者会继续为将来的交付工作,但我不确定。
如果您不处理异常,您的 channel
将被关闭。
autoAck = false
消息将重新排队。
在消费过程中处理错误始终是一个好习惯。
顺便说一句,它存在一个您可以使用的异常处理程序:
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"));
错误将被重定向到处理程序并且通道不会关闭。
就我而言,您应该始终在事件内部处理错误。
RabbitMQ Java 客户端的 Consumer
接口似乎没有任何东西,例如 handleException()
方法。
那么 RuntimeException
被扔进 Consumer.handleDelivery()
里面会有什么后果?
人们可能会期望以某种方式记录异常,并且消费者会继续为将来的交付工作,但我不确定。
如果您不处理异常,您的 channel
将被关闭。
autoAck = false
消息将重新排队。
在消费过程中处理错误始终是一个好习惯。
顺便说一句,它存在一个您可以使用的异常处理程序:
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"));
错误将被重定向到处理程序并且通道不会关闭。
就我而言,您应该始终在事件内部处理错误。