标题交换amqplib或其他js库

时间:2015-10-19 17:53:56

标签: rabbitmq amqp meanjs

我正在编写一个Web应用程序来与基于RabbitMQ的软件系统进行交互。我是网络应用程序的新手,但是我已经使用MEAN堆栈编写了相当数量的应用程序并且对此感到满意。我找到了这个链接:

amqp vs amqplib - which Node.js amqp client library is better?

它提出了几个很好的替代库,比如bramqp和amqp.node,但是我还没有找到以下问题的答案。这些库中的哪些(如果有的话)允许我与基于标题的交换进行交互?请注意,我很乐意将此作为上述原始链接的后续内容发布,但我无法弄清楚如何。

霍华德

1 个答案:

答案 0 :(得分:0)

我写了一个教程,演示下面的标题交换。很快就会有像bramqp这样的高级教程。这应该只打印其中一条消息。

var bramqp = require('bramqp');
var async = require('async');
var net = require('net');

var socket = net.connect({
    port : 5672
});
bramqp.initialize(socket, 'rabbitmq/full/amqp0-9-1.stripped.extended', function(error, handle) {
    async.series([ function(seriesCallback) {
        handle.openAMQPCommunication('guest', 'guest', true, seriesCallback);
    }, function(seriesCallback) {
        handle.queue.declare(1, 'header_test_queue');
        handle.once('1:queue.declare-ok', function(channel, method, data) {
            seriesCallback();
        });
    }, function(seriesCallback) {
        handle.basic.consume(1, 'header_test_queue', null, false, true, false, false, {});
        handle.once('1:basic.consume-ok', function(channel, method, data) {
            handle.on('1:basic.deliver', function(channel, method, data) {
                handle.once('content', function(channel, className, properties, content) {
                    console.log('got a message:');
                    console.log(content.toString());
                });
            });
            seriesCallback();
        });
    }, function(seriesCallback) {
        handle.exchange.declare(1, 'header_test_exchange', 'headers', function() {
            seriesCallback();
        });
    }, function(seriesCallback) {
        exchange_arguments = {
            'x-match': {
                type: 'Long string',
                data: 'all'
            },
            wizard : {
                type: 'Long string',
                data: 'magic'
            }
        };
        handle.queue.bind(1, 'header_test_queue', 'header_test_exchange', null, false, exchange_arguments, function() {
            seriesCallback();
        });
    }, function(seriesCallback) {
        handle.basic.publish(1, 'header_test_exchange', null, false, false, function() {
            properties = {
                headers: {
                    wizard : {
                        type: 'Long string',
                        data: 'magic'
                    }
                }
            };
            handle.content(1, 'basic', properties, 'Hello World! with magic', seriesCallback);
        });
    }, function(seriesCallback) {
        handle.basic.publish(1, 'header_test_exchange', null, false, false, function() {
            properties = {
                headers: {
                    wizard : {
                        type: 'Long string',
                        data: 'not magic'
                    }
                }
            };
            handle.content(1, 'basic', properties, 'Hello World! without magic', seriesCallback);
        });
    }, function(seriesCallback) {
        setTimeout(function() {
            handle.closeAMQPCommunication(seriesCallback);
        }, 10 * 1000);
    }, function(seriesCallback) {
        handle.socket.end();
        setImmediate(seriesCallback);
    } ], function() {
        console.log('all done');
    });
});