Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provided a tutorial for RabbitMq priority queue implementation #186

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/tutorials/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ and routing replies back to clients.
* [rpc_server.js](rpc_server.js)
* [rpc_client.js](rpc_client.js)

## [Tutorial seven: Priority Queue][tute-seven]

RabbitMQ has priority queue implementation in the core as of version 3.5.0.
Using RabbitMQ as a priority queue, showing how to set maximum priority value supported by the queue and setting priority of each message pushed to the queue. In case of messages having equal value of priority, they are consumed in FIFO order.

* [send_priority_queue.js](callback_api/send_priority_queue.js)
* [receive_priority_queue.js](callback_api/receive_priority_queue.js)

I depart slightly from the original tutorial code, which I think has
some needless object-orientation (in the Python code; you don't get a
choice about needless object-orientation in Java).
Expand All @@ -91,3 +99,4 @@ choice about needless object-orientation in Java).
[tute-four]: http://www.rabbitmq.com/tutorials/tutorial-four-python.html
[tute-five]: http://www.rabbitmq.com/tutorials/tutorial-five-python.html
[tute-six]: http://www.rabbitmq.com/tutorials/tutorial-six-python.html
[tute-seven]: https://www.rabbitmq.com/priority.html
30 changes: 30 additions & 0 deletions examples/tutorials/callback_api/receive_priority_queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env node

var amqp = require('amqplib/callback_api');

function bail(err, conn) {
console.error(err);
if (conn) conn.close(function() { process.exit(1); });
}

function on_connect(err, conn) {
if (err !== null) return bail(err);
process.once('SIGINT', function() { conn.close(); });

var q = 'hello';

function on_channel_open(err, ch) {
ch.assertQueue(q, {durable: false, maxPriority: 10}, function(err, ok) {
if (err !== null) return bail(err, conn);
ch.consume(q, function(msg) { // message callback
console.log(" [x] Received '%s'", msg.content.toString());
}, {noAck: true}, function(_consumeOk) { // consume callback
console.log(' [*] Waiting for messages. To exit press CTRL+C');
});
});
}

conn.createChannel(on_channel_open);
}

amqp.connect(on_connect);
36 changes: 36 additions & 0 deletions examples/tutorials/callback_api/send_priority_queue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env node

var amqp = require('amqplib/callback_api');

function bail(err, conn) {
console.error(err);
if (conn) conn.close(function() { process.exit(1); });
}

function on_connect(err, conn) {
if (err !== null) return bail(err);

var q = 'hello';
var message = '';
var priorityValue = 0;

function on_channel_open(err, ch) {
if (err !== null) return bail(err, conn);
ch.assertQueue(q, {durable: false, maxPriority: 10}, function(err, ok) {
if (err !== null) return bail(err, conn);

for(var index=1; index<=50; index++) {
priorityValue = Math.floor((Math.random() * 10));
// index refers to message number. Lower is the index value, earlier is the message pushed into the queue.
message = 'Message index = ' + index + ' and Priority Value = ' + priorityValue;
ch.sendToQueue(q, new Buffer(message), {priority: priorityValue});
console.log(" [x] Sent '%s'", message);
}
ch.close(function() { conn.close(); });
});
}

conn.createChannel(on_channel_open);
}

amqp.connect(on_connect);