[英]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.