-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer.js
35 lines (29 loc) · 863 Bytes
/
producer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const amqp = require('amqplib');
const message = process.argv[2] || 'Hello world!';
const jobCompletionTimeInSeconds = parseInt(process.argv[3]) || 7;
const amqpServerURL = 'amqp://localhost:5672';
const queueName = 'd2_work_queue';
const jobDataObject = {
jobMessage: message,
jobCompletionTime: jobCompletionTimeInSeconds,
};
const init = async () => {
try {
const connection = await amqp.connect(amqpServerURL);
const channel = await connection.createChannel();
await channel.assertQueue(queueName, { durable: true });
await channel.sendToQueue(
queueName,
Buffer.from(JSON.stringify(jobDataObject)),
{
persistent: true,
}
);
console.log(`${message} sent successfully :)`);
await channel.close();
await connection.close();
} catch (error) {
console.error(error);
}
};
init();