1
1
const amqp = require ( "amqplib" ) ;
2
2
3
+ const matching_exchange_name = "matching_exchange" ;
4
+ const dead_letter_exchange_name = "dead_letter_exchange" ;
5
+ const dead_letter_queue_name = "dead_letter_queue" ;
6
+ const cancel_queue_name = "cancel_queue" ;
7
+ const queueNames = [
8
+ 'easy.python' ,
9
+ 'easy.java' ,
10
+ 'easy.cplusplus' ,
11
+ 'medium.python' ,
12
+ 'medium.java' ,
13
+ 'medium.cplusplus' ,
14
+ 'hard.python' ,
15
+ 'hard.java' ,
16
+ 'hard.cplusplus' ,
17
+ ] ;
18
+
3
19
async function setupRabbitMQ ( ) {
4
20
try {
5
- const connection = await amqp . connect ( process . env . RABBITMQ_URL )
6
- . catch ( ( error ) => {
7
- console . error ( "Error connecting to RabbitMQ:" , error ) ;
8
- return null ;
9
- } ) ;
21
+ const connection = await amqp . connect ( process . env . RABBITMQ_URL ) ;
10
22
11
23
if ( ! connection ) {
12
24
return ;
13
25
}
14
26
15
27
const channel = await connection . createChannel ( ) ;
16
28
17
- // Declare matching exchange to be bind to queues
18
- const matching_exchange_name = "matching_exchange" ;
19
- await channel . assertExchange ( matching_exchange_name , "topic" , { durable : false } ) ;
29
+ // Declare the matching exchange (topic)
30
+ await channel . assertExchange ( matching_exchange_name , "topic" , { durable : true } ) ;
20
31
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 } ) ;
32
+ // Declare the dead-letter exchange (fanout)
33
+ await channel . assertExchange ( dead_letter_exchange_name , "fanout" , { durable : true } ) ;
24
34
25
- const queueNames = [
26
- 'easy.python' ,
27
- 'easy.java' ,
28
- 'easy.cplusplus' ,
29
- 'medium.python' ,
30
- 'medium.java' ,
31
- 'medium.cplusplus' ,
32
- 'hard.python' ,
33
- 'hard.java' ,
34
- 'hard.cplusplus'
35
- ]
35
+ // Declare and bind all main queues with TTL and DLQ bindings
36
+ for ( let queueName of queueNames ) {
37
+ await channel . deleteQueue ( queueName ) ; // Ensure we start fresh for each setup
36
38
37
- // Create and bind queues to exchange with the routing keys
38
- for ( let name of queueNames ) {
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
- } ) ;
39
+ await channel . assertQueue ( queueName , {
40
+ durable : true ,
41
+ arguments : {
42
+ 'x-message-ttl' : 10000 , // 60 seconds TTL
43
+ 'x-dead-letter-exchange' : dead_letter_exchange_name // Bind to dead-letter exchange
44
+ }
45
+ } ) ;
53
46
54
- await channel . bindQueue ( name , matching_exchange_name , name ) ; // e.g. messages with routing key easy.python goes to easy.python queue
47
+ await channel . bindQueue ( queueName , matching_exchange_name , queueName ) ; // Bind to exchange
55
48
}
56
49
57
- // Create and bind queue to exchange (if we want only 1 queue)
58
- // await channel.assertQueue(name, { durable: false })
59
- // await channel.bindQueue(name, matching_exchange_name, '#') // all messages go to this queue because of a wildcard pattern
50
+ // Delete DLQ before asserting it
51
+ await channel . deleteQueue ( dead_letter_queue_name ) ;
60
52
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
53
+ // Declare the dead-letter queue and bind it to the dead-letter exchange
54
+ await channel . assertQueue ( dead_letter_queue_name , { durable : true } ) ;
55
+ await channel . bindQueue ( dead_letter_queue_name , dead_letter_exchange_name , '' ) ; // Bind with no routing key
65
56
57
+ // Declare and bind the cancel queue
58
+ await channel . deleteQueue ( cancel_queue_name ) ; // Delete any existing cancel queue
59
+ await channel . assertQueue ( cancel_queue_name , { durable : true } ) ; // Declare the cancel queue
60
+ await channel . bindQueue ( cancel_queue_name , matching_exchange_name , 'cancel' ) ; // Bind with the "cancel" routing key
66
61
67
- console . log ( "RabbitMQ setup complete with queues and bindings." )
62
+ console . log ( "RabbitMQ setup complete with queues, DLQ, and bindings." ) ;
68
63
69
64
await channel . close ( ) ;
70
65
await connection . close ( ) ;
71
66
} catch ( error ) {
72
- console . log ( ' Error setting up RabbitMQ:' , error ) ;
67
+ console . error ( " Error setting up RabbitMQ:" , error ) ;
73
68
}
74
69
}
75
70
76
- module . exports = { setupRabbitMQ } ;
77
-
78
- setupRabbitMQ ( )
71
+ module . exports = { setupRabbitMQ, matching_exchange_name, queueNames, dead_letter_queue_name , cancel_queue_name} ;
0 commit comments