提问者:小点点

RabbitMQ推迟接收


在spring-AMQP中,有可能以某种方式推迟监听来自某个特定队列的消息吗?

在我的用例中,我有一个服务,它必须侦听两个RabbitMQ队列上的消息。第一个专用于此服务,第二个用于my service的多个实例(运行在不同的机器上)的负载平衡作业。

我的statup服务通过第一个队列接收配置并自我配置。只有在配置之后,才允许处理第二个队列中的“标准”作业--而不是在配置之前。

我怎样才能做到这一点呢?使用@RabbitListener(queues={queue1,queue2})立即开始监听。

我还查看了rabbitmq_delayed_message_exchange,但这不是我想要的,因为它延迟了消息的处理。我不想延迟处理(其他已经配置的使用者可以处理工作)。

谢谢你的帮助。


共1个答案

匿名用户

@RabbitListener具有AutoStartup选项:

/**
 * Set to true or false, to override the default setting in the container factory.
 * @return true to auto start, false to not auto start.
 * @since 2.0
 */
String autoStartup() default "";

我想您最好有两个单独的@rabbitlistener:一个用于配置队列,另一个用于not-autostartup'ed。配置就绪后,您需要从RabbitListenerEndpointRegistry.getListenerContainer()中为第二个容器获取一个容器,并调用它的start()。您还可以在第二个@RabbitListener上配置ID:

/**
 * The unique identifier of the container managing for this endpoint.
 * <p>If none is specified an auto-generated one is provided.
 * @return the {@code id} for the container managing for this endpoint.
 * @see org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistry#getListenerContainer(String)
 */
String id() default "";

有关更多信息,请参阅文档:https://docs.spring.io/spring-amqp/docs/2.1.7.release/reference/html/#async-annotation-driven