JAVA :Shutdown Signal: channel error; protocol method: #method<channel.close>(reply-code=406, reply

JAVA 报错Shutdown Signal: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - unknown delivery tag 0, class-id=60, method-id=80)

Introduction:

During project development, sometimes you may encounter an error message such as "Shutdown Signal: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - unknown delivery tag 0, class-id=60, method-id=80)". This error is usually related to message queues, and may occur when using message middleware such as RabbitMQ. This blog will analyze the cause of this error and provide corresponding solutions.

Reason for error:

"Shutdown Signal: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - unknown delivery tag 0, class-id=60, method-id=80)" may be reported for the following reasons:

  • Delivery tag (delivery tag) error: This error indicates that a consumer tried to confirm an unknown delivery tag. It could be that the consumer lost the delivery tag before or after acknowledging the message, or the delivery tag was incorrectly used or reused.
  • Errors caused by channel closure: It may be that the channel where a consumer is located is closed, but the consumer still tries to use the channel for message confirmation.

Solution:

Here are some possible solutions for the above error reasons:

  • Ensure delivery tags are handled correctly: Ensure delivery tags are properly fetched and saved until the consumer acknowledges the message. Do not attempt message acknowledgments with unknown or invalid delivery tags.
  • Check channel state: In the consumer, make sure to check the state of the channel before using it to avoid trying to use it after it has been closed. Appropriate logic can be added in the consumer code to detect the state of the channel and recreate or select another available channel.
  • Check message middleware configuration: Check whether the configuration of message middleware (such as RabbitMQ) is correct. Ensure that channel and delivery tags are used as expected, avoiding possible misconfiguration or misuse.
    Sample Code:
    Here is a simple RabbitMQ consumer code sample that demonstrates proper handling of delivery tags and checking channel status:
void consumeMessage() throws IOException {
    
    
    Channel channel = connection.createChannel();
    String queueName = "my_queue";

    channel.basicConsume(queueName, false, (consumerTag, delivery) -> {
    
    
        long deliveryTag = delivery.getEnvelope().getDeliveryTag();
        try {
    
    
            // 处理消息
            processMessage(delivery);

            // 确认消息
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
    
    
            // 处理异常,根据需要进行重试或者记录日志
            handleException(e);

            // 拒绝消息并重新入队
            channel.basicReject(deliveryTag, true);
        }
    }, consumerTag -> {
    
    });

    // 检查通道状态
    if (!channel.isOpen()) {
    
    
        // 重新创建通道或者采取其他处理方法
        recreateChannel();
    }
}

in conclusion:

Shutdown Signal: channel error; protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED - unknown delivery tag 0, class-id=60, method-id=80)Errors are usually message queue related and can be caused by wrong delivery tags or channel closures. This can be resolved by properly handling delivery tags, checking channel status, and configuration of messaging middleware. This ensures that consumers process messages correctly and avoids errors.

Guess you like

Origin blog.csdn.net/weixin_45626288/article/details/131725085