RabbitMQ基础学习02(工作队列)

1. 主要概念:生产者  队列  消费者
2. 虽然Message通过RabbitMQ在不同的App之间传递,但是Message始终只能存储在队列中


[url]http://www.rabbitmq.com/tutorials/tutorial-two-python.html [/url]


Work queues [ Task queues ]


Themain idea behind Work Queues (aka: Task Queues)is to avoid doing a resource-intensive task immediately and having to wait forit to complete. Instead we schedule the task to be done later. We encapsulate a task asa message and send it to the queue. A worker process running in the backgroundwill pop the tasks and eventually execute the job. When you run many workersthe tasks will be shared between them.

Thisconcept is especially useful in web applications where it's impossible tohandle a complex task during a short HTTP request window.


在处理大并发的任务时,我们通常会有多个线程同时执行任务(多个C),所以我们需要RabbitMQ根据相应的规则把队列中的任务推送到不同的C。

假设P发送了10条消息,3个C同时接收



round-robin   dispatching

轮询分发:这种分发机制,RabbitMQ会把消息平均分发给不同的C 


message   acknowledgment

消息确认:消息确认主要是为了防止消息丢失。RabbitMQ在把消息发送给C之后就会把消息删除掉,但是如果此时某个C被kill或者出现其它问题,就会导致消息丢失。加入消息确认机制之后,RabbitMQ只有在收到C返回的确认消息之后才会把消息删除,如果某个C的链接断掉,那么RabbitMQ会把发送给这个C的消息分发给其它的C。注意:这里没有时间限制,如果一个C很长时间没有返回确认消息,RabbitMQ仍然不会把这个消息分发给其它C,只有在某个C的链接断开之后,才会重新分发。 


message   durability

消息持久化:消息确认机制可以保证在某个C出问题时,消息不会丢失。但是如果RabbitMQ服务器在运行中出现问题,那么消息同样会丢失。为了确保在服务器出问题时消息不丢失,我们需要进行2个操作:1. 声明队列持久化 2.声明消息持久化 


fair   dispatch

公平分发:公平分发是为了处理以下场景:假设有2个C在同时处理任务,RabbitMQ采用轮询分发的机制向不同的C分发任务,但是分发到A的任务都需要大量时间来处理,而分发到B的任务可以瞬间处理,那么就会导致A一直在处理而B一直处于空闲中,这是明显不合理的。

In order to defeat that we can use the basic.qos method withthe prefetch_count=1 setting. Thistells RabbitMQ not to give more than one message to a worker at a time. Or, inother words, don't dispatch a new message to a worker until it has processedand acknowledged the previous one. Instead, it will dispatch it to the nextworker that is not still busy. 

公平分发的原理是一次只给C一个任务来处理,等一个任务处理完成之后再分发另外一个任务。

猜你喜欢

转载自lijiejava.iteye.com/blog/2245861