RabbitMQ / AMQP: single queue, multiple consumers for same message?

前端 未结 12 2232
不思量自难忘°
不思量自难忘° 2020-11-28 00:53

I am just starting to use RabbitMQ and AMQP in general.

  • I have a queue of messages
  • I have multiple consumers, which I would like to do different thing
相关标签:
12条回答
  • 2020-11-28 01:34

    RabbitMQ / AMQP: single queue, multiple consumers for same message and page refresh.

    rabbit.on('ready', function () {    });
        sockjs_chat.on('connection', function (conn) {
    
            conn.on('data', function (message) {
                try {
                    var obj = JSON.parse(message.replace(/\r/g, '').replace(/\n/g, ''));
    
                    if (obj.header == "register") {
    
                        // Connect to RabbitMQ
                        try {
                            conn.exchange = rabbit.exchange(exchange, { type: 'topic',
                                autoDelete: false,
                                durable: false,
                                exclusive: false,
                                confirm: true
                            });
    
                            conn.q = rabbit.queue('my-queue-'+obj.agentID, {
                                durable: false,
                                autoDelete: false,
                                exclusive: false
                            }, function () {
                                conn.channel = 'my-queue-'+obj.agentID;
                                conn.q.bind(conn.exchange, conn.channel);
    
                                conn.q.subscribe(function (message) {
                                    console.log("[MSG] ---> " + JSON.stringify(message));
                                    conn.write(JSON.stringify(message) + "\n");
                                }).addCallback(function(ok) {
                                    ctag[conn.channel] = ok.consumerTag; });
                            });
                        } catch (err) {
                            console.log("Could not create connection to RabbitMQ. \nStack trace -->" + err.stack);
                        }
    
                    } else if (obj.header == "typing") {
    
                        var reply = {
                            type: 'chatMsg',
                            msg: utils.escp(obj.msga),
                            visitorNick: obj.channel,
                            customField1: '',
                            time: utils.getDateTime(),
                            channel: obj.channel
                        };
    
                        conn.exchange.publish('my-queue-'+obj.agentID, reply);
                    }
    
                } catch (err) {
                    console.log("ERROR ----> " + err.stack);
                }
            });
    
            // When the visitor closes or reloads a page we need to unbind from RabbitMQ?
            conn.on('close', function () {
                try {
    
                    // Close the socket
                    conn.close();
    
                    // Close RabbitMQ           
                   conn.q.unsubscribe(ctag[conn.channel]);
    
                } catch (er) {
                    console.log(":::::::: EXCEPTION SOCKJS (ON-CLOSE) ::::::::>>>>>>> " + er.stack);
                }
            });
        });
    
    0 讨论(0)
  • 2020-11-28 01:35

    The send pattern is a one-to-one relationship. If you want to "send" to more than one receiver you should be using the pub/sub pattern. See http://www.rabbitmq.com/tutorials/tutorial-three-python.html for more details.

    0 讨论(0)
  • 2020-11-28 01:36

    To get the behavior you want, simply have each consumer consume from its own queue. You'll have to use a non-direct exchange type (topic, header, fanout) in order to get the message to all of the queues at once.

    0 讨论(0)
  • 2020-11-28 01:36

    I think you should check sending your messages using the fan-out exchanger. That way you willl receiving the same message for differents consumers, under the table RabbitMQ is creating differents queues for each one of this new consumers/subscribers.

    This is the link for see the tutorial example in javascript https://www.rabbitmq.com/tutorials/tutorial-one-javascript.html

    0 讨论(0)
  • 2020-11-28 01:40

    Yes each consumer can receive the same messages. have a look at http://www.rabbitmq.com/tutorials/tutorial-three-python.html http://www.rabbitmq.com/tutorials/tutorial-four-python.html http://www.rabbitmq.com/tutorials/tutorial-five-python.html

    for different ways to route messages. I know they are for python and java but its good to understand the principles, decide what you are doing and then find how to do it in JS. Its sounds like you want to do a simple fanout (tutorial 3), which sends the messages to all queues connected to the exchange.

    The difference with what you are doing and what you want to do is basically that you are going to set up and exchange or type fanout. Fanout excahnges send all messages to all connected queues. Each queue will have a consumer that will have access to all the messages separately.

    Yes this is commonly done, it is one of the features of AMPQ.

    0 讨论(0)
  • 2020-11-28 01:46

    Can I have each consumer receive the same messages? Ie, both consumers get message 1, 2, 3, 4, 5, 6? What is this called in AMQP/RabbitMQ speak? How is it normally configured?

    No, not if the consumers are on the same queue. From RabbitMQ's AMQP Concepts guide:

    it is important to understand that, in AMQP 0-9-1, messages are load balanced between consumers.

    This seems to imply that round-robin behavior within a queue is a given, and not configurable. Ie, separate queues are required in order to have the same message ID be handled by multiple consumers.

    Is this commonly done? Should I just have the exchange route the message into two separate queues, with a single consumer, instead?

    No it's not, single queue/multiple consumers with each each consumer handling the same message ID isn't possible. Having the exchange route the message onto into two separate queues is indeed better.

    As I don't require too complex routing, a fanout exchange will handle this nicely. I didn't focus too much on Exchanges earlier as node-amqp has the concept of a 'default exchange' allowing you to publish messages to a connection directly, however most AMQP messages are published to a specific exchange.

    Here's my fanout exchange, both sending and receiving:

    var amqp = require('amqp');
    var connection = amqp.createConnection({ host: "localhost", port: 5672 });
    var count = 1;
    
    connection.on('ready', function () {
      connection.exchange("my_exchange", options={type:'fanout'}, function(exchange) {   
    
        var sendMessage = function(exchange, payload) {
          console.log('about to publish')
          var encoded_payload = JSON.stringify(payload);
          exchange.publish('', encoded_payload, {})
        }
    
        // Recieve messages
        connection.queue("my_queue_name", function(queue){
          console.log('Created queue')
          queue.bind(exchange, ''); 
          queue.subscribe(function (message) {
            console.log('subscribed to queue')
            var encoded_payload = unescape(message.data)
            var payload = JSON.parse(encoded_payload)
            console.log('Recieved a message:')
            console.log(payload)
          })
        })
    
        setInterval( function() {    
          var test_message = 'TEST '+count
          sendMessage(exchange, test_message)  
          count += 1;
        }, 2000) 
     })
    })
    
    0 讨论(0)
提交回复
热议问题