still testing delayed queue

This commit is contained in:
Kfir Dayan 2023-07-08 15:26:13 +03:00
parent 2333b78f8c
commit e3268b4633
3 changed files with 23 additions and 3 deletions

View file

@ -9,9 +9,16 @@ export class RabbitMQ {
channel: amqp.Channel;
queueName: string;
mongoClient: MongoDbModel;
exchange: string;
queue: string;
routingKey: string;
constructor() {
this.mongoClient = new MongoDbModel();
this.exchange = 'delayed_exchange';
this.queue = 'delayed_queue';
this.routingKey = 'delayed_routing_key'
this.queueName = env.RABBITMQ_QUEUE_NAME;
this.connect().then(() => {
@ -30,6 +37,11 @@ export class RabbitMQ {
password: env.RABBITMQ_PASSWORD,
});
this.channel = await connection.createChannel();
await this.channel.assertExchange(this.exchange, 'x-delayed-message', { durable: true, autoDelete: false, arguments: { 'x-delayed-type': 'direct' } });
await this.channel.assertQueue(this.queueName, { durable: true });
await this.channel.bindQueue(this.queueName, this.exchange, this.routingKey);
await this.channel.assertQueue(this.queueName, { durable: true });
}

View file

@ -9,7 +9,7 @@ Content-Type: application/json
{
"title": "Go to the gymNEW!!!",
"description": "Go to the gym at 8pm",
"due_date": "2023-07-08T15:05:00.000Z"
"due_date": "2023-07-08T15:24:00.000Z"
}
### update request

View file

@ -6,10 +6,17 @@ const env = require('dotenv').config().parsed;
export class RabbitMQ {
channel: amqp.Channel;
queueName: string;
exchange: string;
queue: string;
routingKey: string;
constructor() {
this.queueName = env.RABBITMQ_QUEUE_NAME;
this.exchange = 'delayed_exchange';
this.queue = 'delayed_queue';
this.routingKey = 'delayed_routing_key'
this.connect().then(() => {
console.log('RabbitMQ connected');
}).catch((error) => {
@ -28,8 +35,9 @@ export class RabbitMQ {
password: env.RABBITMQ_PASSWORD,
});
this.channel = await connection.createChannel();
await this.channel.assertExchange('delayed', 'x-delayed-message', { durable: true, autoDelete: false, arguments: { 'x-delayed-type': 'direct' } });
await this.channel.assertExchange(this.exchange, 'x-delayed-message', { durable: true, autoDelete: false, arguments: { 'x-delayed-type': 'direct' } });
await this.channel.assertQueue(this.queueName, { durable: true });
await this.channel.bindQueue(this.queueName, this.exchange, this.routingKey);
console.log('Channel and queue asserted successfully');
} catch (error) {
console.error('Error connecting to RabbitMQ:', error);
@ -43,7 +51,7 @@ export class RabbitMQ {
const message = JSON.stringify({ payload, delayTimeForQueue: delayTimeForQueue });
const options: Options.Publish = { persistent: true, headers: { 'x-delay': delayTimeForQueue } };
try {
await this.channel.sendToQueue(this.queueName, Buffer.from(message), options);
await this.channel.publish(this.exchange, this.routingKey, Buffer.from(message), options);
console.log('Message sent to the queue');
} catch (error) {
console.error('Error sending message to RabbitMQ:', error);