Learn to use. Looking for a half-day node. JS under the RABBITMQ Library, looks not to take the hand, until finally found the AMQP library, it looks good, according to the example, wrote the first RABBITMQ client.
First, install the NODE-AMQP library using NPM install AMQP--save, although he recommends using a global installation, but don't do it!
The following is the client code:
[JavaScript]View Plaincopyprint?
- var AMPQ = require (' amqp ');
- var util = require (' util ');
- var connection = ampq.createconnection ();
- var bstop = false;
- Connection.on (' ready ', function () {
- Connection.queue (' moneyqueue ', {durable: true, Autodelete: false}, function (queue) { /c4>
- Console.log (' Queue ' + queue.name + ' is open! ');
- Queue.subscribe (function (message, header, Deliveryinfo) {
- if (message.data) {
- var messagetext = message.data.toString ()
- Console.log (MessageText);
- if (MessageText = = = "Quit") bstop = true;
- }
- });
- });
- });
- (function keepitrunning () {
- if (!bstop)
- SetTimeout (keepitrunning, 1000);
- Else
- Connection.end ();
- })();
This is an echo client, which comes in, prints out, and waits silently for the next message. If the message is quit, then graceful exit ~
Where the code creates a persistent queue:moneyqueue, the queue is automatically bound to the default Exchange, which is provided by RMQ, so it's persistent, so there's no problem.
Because this is just a consumer, unable to work alone, to verify, you can send messages through the RMQ Administration page.
http://blog.csdn.net/puncha/article/details/8452017
RABBITMQ Learning: (12) using RABBITMQ in the node. JS Environment