@@ -18,6 +18,10 @@ async function setupRabbitMQ() {
18
18
const matching_exchange_name = "matching_exchange" ;
19
19
await channel . assertExchange ( matching_exchange_name , "topic" , { durable : false } ) ;
20
20
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
+
21
25
const queueNames = [
22
26
'easy.python' ,
23
27
'easy.java' ,
@@ -32,14 +36,34 @@ async function setupRabbitMQ() {
32
36
33
37
// Create and bind queues to exchange with the routing keys
34
38
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
+
36
54
await channel . bindQueue ( name , matching_exchange_name , name ) ; // e.g. messages with routing key easy.python goes to easy.python queue
37
55
}
38
56
39
57
// Create and bind queue to exchange (if we want only 1 queue)
40
58
// await channel.assertQueue(name, { durable: false })
41
59
// await channel.bindQueue(name, matching_exchange_name, '#') // all messages go to this queue because of a wildcard pattern
42
60
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
+
43
67
console . log ( "RabbitMQ setup complete with queues and bindings." )
44
68
45
69
await channel . close ( ) ;
@@ -50,3 +74,5 @@ async function setupRabbitMQ() {
50
74
}
51
75
52
76
module . exports = { setupRabbitMQ } ;
77
+
78
+ setupRabbitMQ ( )
0 commit comments