Untitled

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

const QUEUE_A = 'QueueA';
const QUEUE_B = 'QueueB';
const NUM_JOBS = 100;

function createJobs(channel, queue, num) {
  for (let i = 1; i <= num; i++) {
    const msg = `Job ${i}`;
    channel.sendToQueue(queue, Buffer.from(msg));
    console.log(`Sent ${msg} to ${queue}`);
  }
}

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

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

    await new Promise((resolve) => setTimeout(resolve, 2000))

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

    createJobs(channel, QUEUE_A, NUM_JOBS);
    createJobs(channel, QUEUE_B, NUM_JOBS);

    setTimeout(() => {
      connection.close();
      process.exit(0);
    }, 500);
  });
});
Leave a Comment