mxriverlynn / rabbus

A micro-service bus with built-in messaging patterns, for NodeJS and RabbitMQ
116 stars 26 forks source link

Prioritizing messages? #30

Open markohaapala opened 8 years ago

markohaapala commented 8 years ago

Hi,

I've struggled with getting messages prioritized in RabbitMQ using Rabbus send-receive pattern. I create the exchange like this:

  1 'use strict';
  2 var util = require('../util/util');
  3 var Rabbus = require('rabbus');
  4 var rabbot = require('rabbot');
  5 
  6 function NewPaymentReceiver(limit) {
  7   var options = {
  8     exchange: 'payment.ex',
  9     routingKey: 'payment.new',
 10     queue: {
 11       name: 'payment.q',
 12       noBatch: true,
 13       limit: Number(limit), // Here I've tried values 1 and 20
 14       maxPriority: 4
 15     }
 16   };
 17   Rabbus.Receiver.call(this, rabbot, options);
 18 }
 19 
 20 util.inherits(NewPaymentReceiver, Rabbus.Receiver);
 21 module.exports = NewPaymentReceiver;

And in the Sender I set a priority in the middleware depending on how the Sender was initialized:

[...]
  5 exports.payment = function(priority) {
  6   priority = priority ? priority : 1; // LOW 0, NORMAL 1, HIGH 2, "REALTIME" 3, "REALTIME" ENRICHED 4
[...]
 14   var newPaymentSender = new NewPaymentSender();
 15 
 16   newPaymentSender.use(function(message, headers, next) {
 17     headers['x-priority'] = priority;
 18     next();
 19   });
[...]

I can see in RabbitMQ console that the queue has x-max-priority set to 4, and in the messages I see the header x-priority as 1 or 2, depending on how the Sender was initialized.

The queue will however be processed sequentially in the order that the payments are received. My guess is that I'm missing something fundamental. Other than configuring the exchange, I couldn't find any references of defining message priorities in RabbitMQ layout book, RMQ Patterns book, rabbus source code or through google, so this is my last hope to get some guidance.

EDIT: I'm using

24601 commented 7 years ago

Any luck with this? I am working to try and do the same thing.

markohaapala commented 7 years ago

This is how I got it to work. The 'arguments' and 'routingKey' were the key changes. I hope this helps.

 48   var options = {                                                   
 49     exchange: 'payment.ex',                                        
 50     routingKey: 'payment.new.' + priority,                                          
 51     queue: {
 52       name: 'payment.q',
 53       deadLetter: 'payment.dlx',                        
 54       noBatch: true,
 55       limit: Number(limit),
 56       maxPriority: 4
 57     },
 58     arguments: {
 59       'x-priority': priority
 60     }
 61   };
 62   Rabbus.Receiver.call(this, rabbot, options);