简体   繁体   English

RabbitMQ 中如何将消息传递给消费者

[英]How Messages Are Delivered to Consumer in RabbitMQ

I am currently developing an application which uses RabbitMQ Message broker.我目前正在开发一个使用 RabbitMQ 消息代理的应用程序。 I have a confusion on how the message delivery work and I already went through their official docs but could not clarify my concern.我对消息传递的工作方式感到困惑,我已经阅读了他们的官方文档,但无法澄清我的担忧。

Say my application can take messages from two queues, For the queue A, producer directly push messages to this queue and the queue B gets message via an exchange.假设我的应用程序可以从两个队列中获取消息,对于队列 A,生产者直接将消息推送到此队列,队列 B 通过交换获取消息。 My question is if there are messages coming from queue A and queue B at the same time to the consumer, how would the consumer consume these messages?我的问题是,如果队列 A 和队列 B 同时向消费者发送消息,消费者将如何消费这些消息? Sequentially or by creating multiple parallel threads?.顺序还是通过创建多个并行线程?

And if my main thread is responsible for reading the message, will making the main thread go to sleep stops consuming messages from the queue?如果我的主线程负责读取消息,那么让主线程 go 进入睡眠状态会停止从队列中消耗消息吗? I mean will rabbitMQ stop pushing messages to the consumer as long as the main thread is a sleep?我的意思是只要主线程处于睡眠状态,rabbitMQ 就会停止向消费者推送消息吗? I am not using the consumer acks as of now and trying to see if message consuming can be stopped by making the main thread go to sleep for a desired amount of time.我现在没有使用消费者确认,并试图通过使主线程 go 休眠一段所需的时间来查看是否可以停止消息消费。

A RabbitMQ consumer can only consume messages from a single queue. RabbitMQ 消费者只能使用来自单个队列的消息。 There are two mechanisms by which a RabbitMQ consumer consumes a message from the queue RabbitMQ 消费者通过两种机制消费队列中的消息

  1. Push mechanism - In this type, the rabbitmq-server takes the responsibility of pushing the message to the consumer, whenever there is a message in the queue.推送机制——在这种类型中,当队列中有消息时, rabbitmq-server负责将消息推送给消费者。 The number of messages to be sent to the consumer can be controlled using the basic_qos configuration.可以使用basic_qos配置控制要发送给消费者的消息数量。 The message can be automatically acknowledged or can be acknowledged by the consumer.消息可以自动确认,也可以由消费者确认。
  2. Pull mechanism - In this type, it is the job of the consumer to request the rabbitmq-server for the message.机制 - 在这种类型中,消费者的工作是向rabbitmq-server请求消息。 Only one message can be requested at a time.一次只能请求一条消息。

When a consumer connects using the push mechanism, the consumer runs an IO loop, so when you run a long-running job, there are chances that the long-running job can hinder the consumer heartbeat to the rabbitmq server.当消费者使用push机制连接时,消费者会运行 IO 循环,因此当您运行长时间运行的作业时,长时间运行的作业可能会阻碍消费者对 rabbitmq 服务器的心跳。 One way of handling this is to run the long-running job on a separate thread.处理此问题的一种方法是在单独的线程上运行长时间运行的作业。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM