Skip to content

Commit fdd3116

Browse files
committed
Bind queues to dead letter exchange
1 parent ac0f8e4 commit fdd3116

File tree

1 file changed

+27
-1
lines changed
  • Backend/MatchingService/rabbitmq

1 file changed

+27
-1
lines changed

Backend/MatchingService/rabbitmq/setup.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ async function setupRabbitMQ() {
1818
const matching_exchange_name = "matching_exchange";
1919
await channel.assertExchange(matching_exchange_name, "topic", { durable: false });
2020

21+
// Declare dead letter exchange
22+
const dead_letter_exchange_name = "dead_letter_exchange";
23+
await channel.assertExchange(dead_letter_exchange_name, "fanout", { durable: false });
24+
2125
const queueNames = [
2226
'easy.python',
2327
'easy.java',
@@ -32,14 +36,34 @@ async function setupRabbitMQ() {
3236

3337
// Create and bind queues to exchange with the routing keys
3438
for (let name of queueNames) {
35-
await channel.assertQueue(name, { durable: false }); // durable=false ensures queue will survive broker restarts
39+
/*
40+
try {
41+
await channel.deleteQueue(name);
42+
} catch (err) {
43+
console.log(`Queue ${name} does not exist or could not be deleted: ${err.message}`);
44+
}
45+
*/
46+
await channel.assertQueue(name,
47+
{ durable: false, // durable=false ensures queue will survive broker restarts
48+
arguments: {
49+
'x-dead-letter-exchange': dead_letter_exchange_name // set dead letter exchange
50+
}
51+
52+
});
53+
3654
await channel.bindQueue(name, matching_exchange_name, name); // e.g. messages with routing key easy.python goes to easy.python queue
3755
}
3856

3957
// Create and bind queue to exchange (if we want only 1 queue)
4058
// await channel.assertQueue(name, { durable: false })
4159
// await channel.bindQueue(name, matching_exchange_name, '#') // all messages go to this queue because of a wildcard pattern
4260

61+
// Create and bind dead letter queue
62+
// const dead_letter_queue_name = "dead_letter_queue";
63+
// await channel.assertQueue(deadLetterQueueName, { durable: false });
64+
// await channel.bindQueue(deadLetterQueueName, deadLetterExchangeName, ''); // Bind all messages to this queue
65+
66+
4367
console.log("RabbitMQ setup complete with queues and bindings.")
4468

4569
await channel.close();
@@ -50,3 +74,5 @@ async function setupRabbitMQ() {
5074
}
5175

5276
module.exports = { setupRabbitMQ };
77+
78+
setupRabbitMQ()

0 commit comments

Comments
 (0)