-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06e5d9b
commit ced1d06
Showing
11 changed files
with
1,486 additions
and
1,320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const amqp = require("amqplib"); | ||
const config = require("../config/config"); | ||
const logger = require("../config/logger"); | ||
|
||
class Producer { | ||
channel; | ||
|
||
async createChannel() { | ||
const connection = await amqp.connect(config.rabbitmq.url); | ||
this.channel = await connection.createChannel(); | ||
} | ||
|
||
async publishEmailMessage(message, routingKey = "MailService") { | ||
if (!this.channel) { | ||
await this.createChannel(); | ||
} | ||
|
||
const mailServiceExchangeName = config.rabbitmq.mailServiceExchangeName; | ||
await this.channel.assertExchange(mailServiceExchangeName, "direct"); | ||
|
||
const logDetails = { | ||
logType: routingKey, | ||
message: message, | ||
dateTime: new Date(), | ||
}; | ||
|
||
await this.channel.publish( | ||
mailServiceExchangeName, | ||
routingKey, | ||
Buffer.from(JSON.stringify(logDetails)) | ||
); | ||
|
||
logger.info( | ||
`The new ${routingKey} is sent to exchange ${mailServiceExchangeName}` | ||
); | ||
} | ||
} | ||
|
||
module.exports = Producer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,38 @@ | ||
const config = require("./config.json"); | ||
const logger = require("./logger"); | ||
const amqp = require("amqplib"); | ||
|
||
const sendMail = require("./transporter"); | ||
|
||
const mailList = ["nguyenduckhai8101@gmail.com", "19521658@gm.uit.edu.vn"]; | ||
async function consumeMessages() { | ||
const connection = await amqp.connect(config.amqp.url); | ||
const channel = await connection.createChannel(); | ||
|
||
await channel.assertExchange(config.amqp.exchangeName, "direct"); | ||
|
||
const q = await channel.assertQueue(config.amqp.queueName); | ||
|
||
await channel.bindQueue( | ||
q.queue, | ||
config.amqp.exchangeName, | ||
config.amqp.bindingKey | ||
); | ||
|
||
channel.consume(q.queue, async (msg) => { | ||
const data = JSON.parse(msg.content); | ||
logger.info( | ||
`Receive message from exchange [${ | ||
config.amqp.exchangeName | ||
}], data ${JSON.stringify(data)}` | ||
); | ||
|
||
const { message: mailList } = data; | ||
console.log(mailList); | ||
|
||
await sendMail(mailList); | ||
|
||
channel.ack(msg); | ||
}); | ||
} | ||
|
||
(async () => { | ||
await sendMail(mailList); | ||
})(); | ||
consumeMessages(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.