Last active
February 19, 2020 14:44
-
-
Save carvalhoviniciusluiz/43120aacfe289308768938a4ce3800f3 to your computer and use it in GitHub Desktop.
Send sync queue
This file contains hidden or 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
const amqp = require('amqplib'); | |
const uuid = require('uuid'); | |
module.exports = { | |
async sendPublish (req, res) { | |
const url = ``; | |
const exchange = ''; | |
const routing_key = ''; | |
const conn = await amqp.connect(url); | |
const channel = await conn.createChannel(); | |
await channel.assertExchange(exchange, 'direct', { durable: true }); | |
const q = await channel.assertQueue('', { exclusive: true }); | |
await channel.publish( | |
exchange, | |
routing_key, | |
Buffer.from( | |
JSON.stringify({ | |
chave: 'provident', | |
cnpjCertificado: '54.117.372/0001-21', | |
idUsuario: '1', | |
}) | |
), | |
{ | |
contentType: 'application/json', | |
correlationId: uuid.v4(), | |
replyTo: q.queue, | |
} | |
); | |
let content = null; | |
await channel.consume( | |
q.queue, | |
async data => { | |
({ content } = data); | |
await channel.close(); | |
await conn.close(); | |
}, | |
{ noAck: true } | |
); | |
const result = content ? JSON.parse(content.toString()) : {}; | |
return res.status(202).json(result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment