rabbitmq之Fair dispatch

正常情況下,rabbitmq server(broker)會盲目地,按照message數目平均地分發message給consumer,而不考慮有的message處理起來比較耗時,有的處理起來很快,

場景問題是,假設有兩個consumers,consumerA和consumerB,4條messages,evenly, message1,message3會分發給consumerA去處理,message2,message4會分發給consumerB去處理,有的時候,message1,message3處理起來很簡單,耗時很少,幾秒鐘就處理完畢,message2,message4處理比較耗時,每條message都得處理幾分鐘,則由於rabbitmq默認只會按照數目平均的分發給consumers,這樣會導致consumerA 幾秒鐘就處理完message1和3,休息,而consumerB還在處理message2中,message4還沒開始處理,導致不公平。通常情況下,rabbitmq server只會just blindly dispatches every n-th messageto the n-th consumer.

解決方法:通過channel.basicQos(1);來保證RabbitMQ not to give more thanone message to a worker at a time. Or, in other words, don't dispatcha new message to a worker until it has processed and acknowledged theprevious one. Instead, it will dispatch it to the next worker that is not still busy. 如果不設置這個參數,則rabbitmq會數目均勻的一次性平均分發給各個consumers

int prefetchCount = 1;
channel.basicQos(prefetchCount);
由於queue的size有限,當queue裏面的messages滿了,你需要增加consumers個數來處理message或者其他的strategy


Using message acknowledgments and prefetchCount you can set up awork queue. The durability options let the tasks survive even ifRabbitMQ is restarted.

reference: http://www.rabbitmq.com/tutorials/tutorial-two-java.html


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章