简体   繁体   English

spring rabbitmq - 同时消费多条消息

[英]spring rabbitmq - consume multiple messages at the same time

I'm using RabbitMQ in my spring boot application in this way:我以这种方式在我的 spring 启动应用程序中使用RabbitMQ

Sender:发件人:

rabbitTemplate.convertAndSend("exchange", "routingKey", "Message Text");

Listener:听众:

@RabbitListener(queues = "queueName")
public void receive(String message) {
    System.out.println("start");  
    //send an http request that takes for example 4 seconds 
    System.out.println("end");  
}

With above codes, when application executes sender part, receive method invoked.使用上述代码,当应用程序执行发送方部分时,会调用receive方法。 My problem is while receive method is processing a message, if sender part put another message into queue, the method does not proccess new message and so second start word wont be printed until end word of previous message.我的问题是,当receive方法正在处理一条消息时,如果发送方将另一条消息放入队列,该方法不会处理新消息,因此直到前一条消息的end字才会打印第二个start字。 In the other words, I want to know, how a message listener can proccess multiple messages at a time I don't know what is the problem.换句话说,我想知道消息监听器如何一次处理多条消息,我不知道有什么问题。

From the problem you are stating, it looks like your listener is configured for single thread.从您陈述的问题来看,您的侦听器似乎配置为单线程。 Refer to the container listener configuration docs here and here especially the concurrency settings.请参阅此处此处的容器侦听器配置文档,尤其是并发设置。 The concurrency settings control how many threads process messages on the queue at same time.并发设置控制有多少线程同时处理队列上的消息。

If you are using spring boot, just add this configuration to the application properties:如果您使用 spring 启动,只需将此配置添加到应用程序属性中:

# Minimum number of listener invoker threads
spring.rabbitmq.listener.simple.concurrency=5

And your listener will start accepting messages in parallel (multiple threads).并且您的侦听器将开始并行接受消息(多线程)。 There are other configurations that you can check too.您还可以检查其他配置。 Like the max number of listener invoker threads (check spring boot doc for more info).就像侦听器调用程序线程的最大数量(查看 spring 引导文档以获取更多信息)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 RabbitMq:使用同一个队列消费不同的消息 - RabbitMq: Consume different messages using the same queue Spring 启动应用程序有多个实例,但只有一个实例应该使用来自 RabbitMQ 的消息 - Spring boot application has multiple instances but only one instance should consume messages from RabbitMQ RabbitMQ(或Spring云流)可以独占消息吗? - Can RabbitMQ (or spring cloud stream) consume messages exclusively? 使用顺序和上下文消费RabbitMQ消息 - Consume RabbitMQ messages with order and context 如何配置侦听器以处理Rabbitmq Spring中来自同一队列的多个交换的消息 - How to configure a listener to handle messages from multiple exchanges for the same queue in rabbitmq spring 使用 spring 云 stream 消费来自 RabbitMQ 队列的消息 3.0+ - Consume messages from RabbitMQ queue using spring cloud stream 3.0+ 有没有更好的方法使用RabbitMQ来消耗多线程消息? - Is there a better way to use rabbitMQ to consume multithreaded messages? 如何使用Micronaut和RabbitMQ消费和发布消息? - How consume and publish messages with Micronaut and RabbitMQ? Spring Kafka多个使用者针对单个主题消耗不同的消息 - Spring Kafka multiple consumer for single topic consume different messages 在同一频道上使用和发布消息 - Consume and publish messages on the same channel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM