如何将队列消耗的消息发送到快速应用程序?

时间:2018-05-08 23:52:28

标签: node.js express amqp

所以我在快速应用中有一个简单的get方法,使用amqp library

将消息发送到队列
app.get('/hello', function(req, res) {
    sendMessageToQueue('messageSent', 'producerQueue');
    res.send('messageReceivedFromQueue');
})

sendMessageToQueue的位置:

function sendMessageToQueue(msg, q) {
    amqp.connect('amqp://localhost').then(function(conn) {
        return conn.createChannel().then(function(ch) {

          var ok = ch.assertQueue(q, {durable: false});

          return ok.then(function(_qok) {
              ch.sendToQueue(q, Buffer.from(msg));
              console.log(" [-] Sent '%s' to", msg, q);
              return ch.close();
          });
        }).finally(function() { conn.close();});
    }).catch(console.warn);
}

通讯员receive功能

amqp.connect('amqp://localhost').then(function(conn) {
  process.once('SIGINT', function() { conn.close(); });
  return conn.createChannel().then(function(ch) {
      var q = 'consumerQueue';

      var ok = ch.assertQueue(q, {durable: false});

      ok = ok.then(function(_qok) {
        return ch.consume(q, consumeMessageFromQueue, {noAck: true});
      });

      return ok.then(function(_consumeOk) {
        console.log(' [*] Waiting for messages. To exit press CTRL+C');
      });
    });
  }).catch(console.warn);

consumeMessageFromQueue是:

function consumeMessageFromQueue(msg) {
    console.log(" [+] Received '%s'", msg.content.toString());
}

如何从 consumeMessageFromQueue 功能发送消息,而不是硬编码的' messageReceivedFromQueue ' app.get函数中的字符串?

0 个答案:

没有答案