簡體   English   中英

Spring AMQP 在隊列 Bean 上設置 prefetchCount

[英]Spring AMQP Set prefetchCount on Queue Bean

如何為此隊列使用者設置prefetchCount

@Bean
public Queue eventQueue(AmqpAdmin amqpAdmin) {
    Queue queue = QueueBuilder.durable(EVENT_QUEUE_NAME)
            ...
            .build();

    TopicExchange topicExchange = new TopicExchange(TOPIC_EXCHANGE, true, false);

    amqpAdmin.declareBinding(BindingBuilder
            .bind(queue)
            .to(topicExchange)
            .with(EVENT_ROUTING_KEY));

    return queue;
}

文檔指出prefetchCount這是一個容器配置,但是在我的工廠 bean 上設置它不起作用,並且值默認為250

    @Bean
    public SimpleRabbitListenerContainerFactory containerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPrefetchCount(10); // doesn't work; defaults to 250
        return factory;
    }

更新

Per @GaryRussell's comment below, I did test out the default rabbitListenerContainerFactory and also validated my Spring Boot configuration spring.rabbitmq.listener.simple.prefetch was being consumed in AbstractRabbitListenerContainerFactoryConfigurer . 但是,當我查看 RabbitMQ 中的隊列消費者時,我可以看到我使用默認容器設置定義的隊列的prefetchCount仍然為 250:

rabbit admin 顯示消費者預取 250

我使用 RabbitMQ 管理面板作為事實來源。 我不認為這是在撒謊,因為我有一堆用自定義容器實例化的動態隊列,而且它們確實有非默認(正確)的prefetchCount 我還在 Spring 容器啟動中驗證了只有一個(預期的) rabbitListenerContainerFactory bean。

預取不是隊列屬性,而是消費者屬性。

你的聽眾長什么樣?

您正在為容器工廠使用非標准名稱。

您要么需要將containerFactory屬性添加到@RabbitListener ,要么需要將 bean 重命名為rabbitListenerContainerFactory (覆蓋 Boot 定義的工廠@Bean )。

amqpAdmin.declareBinding(BindingBuilder

您不應該在 bean 定義中與代理交談 - 現在還為時過早。

只需將隊列、交換和綁定添加為@Bean ,您也可以在 application.properties/yaml 文件中設置預取(如果您使用的是 Spring 引導)。 管理員將在首次打開連接時找到它們並聲明它們。

編輯

還有其他事情正在發生...

@SpringBootApplication
public class So62049769Application {

    public static void main(String[] args) {
        SpringApplication.run(So62049769Application.class, args);
    }

    @Bean
    public Queue queue() {
        return new Queue("so62049769");
    }

    @RabbitListener(queues = "so62049769")
    public void listen(String in) {
        System.out.println(in);
    }

}
spring.rabbitmq.listener.simple.prefetch=42

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM