猿问

将多个 @RabbitListener bean 添加到 ContainerFactory

这是我的@Configuration


   @Bean

    public AmqpAdmin amqpAdmin()

    {

        RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory());


        DirectExchange dirExchange = new DirectExchange("evtExchange", true,

                false);


        rabbitAdmin.declareExchange(dirExchange);

        rabbitAdmin.declareQueue(processQueue);

        Binding processBinding = BindingBuilder.bind(processQueue)

                .to(dirExchange).with("rkey.process");

        rabbitAdmin.declareBinding(processBinding);


        return rabbitAdmin;

    }


    @Bean

    public RabbitTemplate rabbitTemplate()

    {

        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());

        return rabbitTemplate;

    }


    @Bean

    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory()

    {

        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();

        factory.setConnectionFactory(connectionFactory());

        SimpleMessageListenerContainer container = factory

                .createListenerContainer();

        factory.setConcurrentConsumers(50);

        factory.setMaxConcurrentConsumers(100);

        container.setStartConsumerMinInterval(3000);

        container.setQueues(processQueue);

        factory.setAdviceChain(retryInterceptor());

        return factory;

    }


    @Bean

    public RetryOperationsInterceptor retryInterceptor()

    {

        return RetryInterceptorBuilder.stateless().maxAttempts(5)

                .backOffOptions(1000, 2.0, 10000).recoverer(new RejectAndDontRequeueRecoverer()).build();

    }


    @Bean

    public ProcessQueueListener processListener()

    {

        return new ProcessQueueListener();

    }



只有当我单独实例化时processListener(),processListener2()我才processListener3()开始在 RabbitMQ Admin 中看到进程队列的多个消费者,并且每个侦听器都在处理消息,否则我只看到一个消费者,尽管指定了setConcurrentConsumers()


有没有一种优雅的方式来按需声明多个监听器,根据需要增加和减少。或者声明多个@Beans 是唯一的选择?还是我做错了什么?


翻阅古今
浏览 480回答 1
1回答

达令说

你用的是什么版本?我刚刚复制了你的容器工厂,它对我来说很好(2.1.3)......顺便说一句,从 2.0 版开始,您可以添加concurrency到@RabbitListener,它将覆盖容器工厂中的任何值。/**&nbsp;* Set the concurrency of the listener container for this listener. Overrides the&nbsp;* default set by the listener container factory. Maps to the concurrency setting of&nbsp;* the container type.&nbsp;* <p>For a&nbsp;* {@link org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer&nbsp;* SimpleMessageListenerContainer} if this value is a simple integer, it sets a fixed&nbsp;* number of consumers in the {@code concurrentConsumers} property. If it is a string&nbsp;* with the form {@code "m-n"}, the {@code concurrentConsumers} is set to {@code m}&nbsp;* and the {@code maxConcurrentConsumers} is set to {@code n}.&nbsp;* <p>For a&nbsp;* {@link org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer&nbsp;* DirectMessageListenerContainer} it sets the {@code consumersPerQueue} property.&nbsp;* @return the concurrency.&nbsp;* @since 2.0&nbsp;*/String concurrency() default "";此外,不相关,但您不应该rabbitAdmin.declareExchange(dirExchange)在 bean 声明中这样做 - 在应用程序上下文生命周期中连接到 RabbitMQ 还为时过早。将交换、队列和绑定添加为@Beans,管理员会自动找到并声明它们。
随时随地看视频慕课网APP

相关分类

Java
我要回答