为了账号安全,请及时绑定邮箱和手机立即绑定

将多个 @RabbitListener bean 添加到 ContainerFactory

将多个 @RabbitListener bean 添加到 ContainerFactory

翻阅古今 2022-05-25 17:13:03
这是我的@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 是唯一的选择?还是我做错了什么?
查看完整描述

1 回答

?
达令说

TA贡献1821条经验 获得超6个赞

你用的是什么版本?

我刚刚复制了你的容器工厂,它对我来说很好(2.1.3)......

//img1.sycdn.imooc.com//628df3420001094707730423.jpg

顺便说一句,从 2.0 版开始,您可以添加concurrency到@RabbitListener,它将覆盖容器工厂中的任何值。


/**

 * Set the concurrency of the listener container for this listener. Overrides the

 * default set by the listener container factory. Maps to the concurrency setting of

 * the container type.

 * <p>For a

 * {@link org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer

 * SimpleMessageListenerContainer} if this value is a simple integer, it sets a fixed

 * number of consumers in the {@code concurrentConsumers} property. If it is a string

 * with the form {@code "m-n"}, the {@code concurrentConsumers} is set to {@code m}

 * and the {@code maxConcurrentConsumers} is set to {@code n}.

 * <p>For a

 * {@link org.springframework.amqp.rabbit.listener.DirectMessageListenerContainer

 * DirectMessageListenerContainer} it sets the {@code consumersPerQueue} property.

 * @return the concurrency.

 * @since 2.0

 */

String concurrency() default "";

此外,不相关,但您不应该rabbitAdmin.declareExchange(dirExchange)在 bean 声明中这样做 - 在应用程序上下文生命周期中连接到 RabbitMQ 还为时过早。将交换、队列和绑定添加为@Beans,管理员会自动找到并声明它们。


查看完整回答
反对 回复 2022-05-25
  • 1 回答
  • 0 关注
  • 479 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信