Spring AMQP在队列Bean上设置prefetchCount

问题描述 投票:0回答:1

如何为此队列使用者设置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;
}

documentation注意,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;
    }
java spring spring-amqp spring-rabbitmq spring-rabbit
1个回答
0
投票

Prefetch不是队列属性,它是使用者属性。

您的听众是什么样的?

您为容器工厂使用了非标准名称。

[您需要在containerFactory中添加@RabbitListener属性,或者需要将bean重命名为rabbitListenerContainerFactory(覆盖Boot的定义工厂@Bean)。

您还可以只在application.properties/yaml文件中设置预取(如果使用的是Spring Boot)。

© www.soinside.com 2019 - 2024. All rights reserved.