如何在 SimpleMessageListenerContainer.java 中记录队列名称

How to log Queue name in SimpleMessageListenerContainer.java

我们遇到了其中一位消费者的问题,需要调试代码。 SimpleRabbitListenerContainerFactory 允许设置一个 ConsumerTagStrategy,它应该在日志记录期间添加标签。

@Bean
public SimpleRabbitListenerContainerFactory analyzeTransactionListenerContainerFactory(ConnectionFactory connectionFactory, AsyncTaskExecutor asyncTaskExecutor) {
    connectionFactory.getVirtualHost());
    SimpleRabbitListenerContainerFactory factory = new  SimpleRabbitListenerContainerFactory();
    factory.setConnectionFactory(connectionFactory);
    factory.setConcurrentConsumers(2);
    factory.setMaxConcurrentConsumers(4);
    factory.setTaskExecutor(asyncTaskExecutor);
    ConsumerTagStrategy consumerTagStrategy = new ConsumerTagStrategy() {
        @Override
        public String createConsumerTag(String queue) {
            return queue;
        }
     };
     factory.setConsumerTagStrategy(consumerTagStrategy);
     return factory;
}

但是,日志仍然没有标签。所以无法找到 queue/consumer 此消息的目的。

LogLevel=DEBUG; category=org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; msg=Cancelling Consumer: tags=[{}], channel=Cached Rabbit Channel: AMQChannel(amqp://guest@10.17.1.13:5672/,47), acknowledgeMode=AUTO local queue size=0; 

LogLevel=DEBUG; category=org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; msg=Idle consumer terminating: Consumer: tags=[{}], channel=Cached Rabbit Channel: AMQChannel(amqp://guest@10.17.1.13:5672/,47), acknowledgeMode=AUTO local queue size=0; 

如何向 SimpleMessageListenerContainer 日志添加一些标签?

嗯,看来你对 Consumer Key 有点误解了。来自 RabbitMQ 文档:

consumer-tag consumer-tag

Specifies the identifier for the consumer. The consumer tag is local to a channel, so two clients can use the same consumer tags. If this field is empty the server will generate a unique tag.

The client MUST NOT specify a tag that refers to an existing consumer. Error code: not-allowed

The consumer tag is valid only within the channel from which the consumer was created. I.e. a client MUST NOT create a consumer in one channel and then use it in another. Error code: not-allowed

SimpleMessageListenerContainer 在开始侦听提供的队列时填充其内部映射以获取 consumerTags。如果我们正在收听(消费),我们可以从以下位置看到它们:

public String toString() {
    return "Consumer: tags=[" + (this.consumerTags.toString()) + "], channel=" + channel
            + ", acknowledgeMode=" + acknowledgeMode + " local queue size=" + queue.size();
}

当然如你所料。

但是如果我们不再消费,例如Channel#basicCancel

或者...在闲置期间查看您的第二条日志消息。当你有一个额外的消费者,但没有消息时。

关于此事的代码如下:

boolean receivedOk = receiveAndExecute(this.consumer); // At least one message received
if (SimpleMessageListenerContainer.this.maxConcurrentConsumers != null) {
    if (receivedOk) {
.....
    }
    else {
        consecutiveMessages = 0;
        if (consecutiveIdles++ > SimpleMessageListenerContainer.this.consecutiveIdleTrigger) {
            considerStoppingAConsumer(this.consumer);
            consecutiveIdles = 0;
        }
   }
}

所以,也许你是白担心了。而且您只会看到那些额外消费者的日志。对于这种情况,您有 maxConcurrentConsumers > concurrentConsumers.