Created
September 4, 2019 10:35
-
-
Save kharandziuk/e838b71f1b6ea8482e7807ff850359c4 to your computer and use it in GitHub Desktop.
all messages from one queue to another in amqp
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 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