Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
955 B
2
Indexable
Never
const amqp = require('amqplib/callback_api');

const QUEUE_A = 'QueueA';
const QUEUE_B = 'QueueB';
const PREFETCH_COUNT = 3;

amqp.connect('amqp://localhost', (error0, connection) => {
  if (error0) {
    throw error0;
  }
  connection.createChannel((error1, channel) => {
    if (error1) {
      throw error1;
    }

    channel.assertQueue(QUEUE_A, {
      durable: true
    });

    channel.assertQueue(QUEUE_B, {
      durable: true
    });

    channel.prefetch(PREFETCH_COUNT);

    const consumeMessage = (queue) => {
      return (msg) => {
        if (msg !== null) {
          console.log(`Received ${msg.content.toString()} from ${queue}`);
          setTimeout(() => {
            channel.ack(msg);
          }, 1000 * (1+Math.random()));
        }
      };
    };

    channel.consume(QUEUE_A, consumeMessage(QUEUE_A), {
      noAck: false
    });

    channel.consume(QUEUE_B, consumeMessage(QUEUE_B), {
      noAck: false
    });
  });
});
Leave a Comment