Skip to content

Instantly share code, notes, and snippets.

@kharandziuk
Created September 4, 2019 10:35
Show Gist options
  • Select an option

  • Save kharandziuk/e838b71f1b6ea8482e7807ff850359c4 to your computer and use it in GitHub Desktop.

Select an option

Save kharandziuk/e838b71f1b6ea8482e7807ff850359c4 to your computer and use it in GitHub Desktop.
all messages from one queue to another in amqp
const DLQ_NAME = 'DeadLetterQueue'
const EXCHANGE_NAME = 'MainExchange'
const delay = (ms) => new Promise((resolve) => {
setTimeout(resolve, ms)
})
const delayWithRefresh = (ms) => {
let flag = false
const promise = () => delay(ms).then(() => {
if (flag) {
return
} else {
flag = true
return promise()
}
})
const refresh = () => {
flag = false
}
return {
timeout: promise(),
refresh
}
}
require('amqplib').connect('amqp://localhost').then(async(connection) => {
const channel = await connection.createChannel()
const dwr = delayWithRefresh(1000)
channel.consume(DLQ_NAME, function(msg) {
channel.publish(EXCHANGE_NAME, '', Buffer.from(msg.content));
channel.ack(msg)
dwr.refresh()
});
dwr.timeout.then(() => process.exit(0))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment