implement rabbitmq to todo service
This commit is contained in:
parent
6253d0360c
commit
44feb4d01c
6 changed files with 55 additions and 6 deletions
|
@ -7,7 +7,7 @@
|
|||
"moduleResolution": "node",
|
||||
"sourceMap": true,
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"noImplicitAny": true,
|
||||
"rootDir": "./src"
|
||||
// "noImplicitAny": true,
|
||||
}
|
||||
}
|
|
@ -9,7 +9,7 @@ Content-Type: application/json
|
|||
{
|
||||
"title": "Go to the gym",
|
||||
"description": "Go to the gym at 8pm",
|
||||
"due_date": "2023-07-08 01:21:00"
|
||||
"due_date": "2023-07-08 10:55:00"
|
||||
}
|
||||
|
||||
### update request
|
||||
|
|
2
todo-service/package-lock.json
generated
2
todo-service/package-lock.json
generated
|
@ -10,12 +10,14 @@
|
|||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@aws-sdk/types": "^3.357.0",
|
||||
"amqplib": "^0.10.3",
|
||||
"aws-sdk": "^2.1413.0",
|
||||
"dotenv": "^16.3.1",
|
||||
"express": "^4.18.2",
|
||||
"mongoose": "^7.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/amqplib": "^0.10.1",
|
||||
"@types/express": "^4.17.17"
|
||||
}
|
||||
},
|
||||
|
|
|
@ -11,12 +11,14 @@
|
|||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@aws-sdk/types": "^3.357.0",
|
||||
"amqplib": "^0.10.3",
|
||||
"aws-sdk": "^2.1413.0",
|
||||
"dotenv": "^16.3.1",
|
||||
"express": "^4.18.2",
|
||||
"mongoose": "^7.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/amqplib": "^0.10.1",
|
||||
"@types/express": "^4.17.17"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,17 +2,18 @@ import { NextFunction, Request, Response } from 'express';
|
|||
import { ApiError } from '../utils/ApiError';
|
||||
import { ITodo } from '../schemas/todoSchema';
|
||||
import { TodoModel } from '../models/todoModel';
|
||||
import { Sqs } from '../aws/Sqs';
|
||||
// import { Sqs } from '../aws/Sqs';
|
||||
import { RabbitMQ } from '../rabbitmq/RabbitMQ';
|
||||
|
||||
const env = require('dotenv').config().parsed;
|
||||
|
||||
export class TodoController {
|
||||
private todoModel: TodoModel;
|
||||
queue: Sqs;
|
||||
queue: RabbitMQ;
|
||||
|
||||
constructor() {
|
||||
this.todoModel = new TodoModel();
|
||||
this.queue = new Sqs();
|
||||
this.queue = new RabbitMQ();
|
||||
}
|
||||
|
||||
public getAll = async (req: Request, res: Response, next: NextFunction) => {
|
||||
|
|
44
todo-service/src/rabbitmq/RabbitMQ.ts
Normal file
44
todo-service/src/rabbitmq/RabbitMQ.ts
Normal file
|
@ -0,0 +1,44 @@
|
|||
import amqp from 'amqplib';
|
||||
import { ITodo } from '../schemas/todoSchema';
|
||||
|
||||
const env = require('dotenv').config().parsed;
|
||||
|
||||
export class RabbitMQ {
|
||||
channel: amqp.Channel;
|
||||
queueName: string;
|
||||
|
||||
constructor() {
|
||||
this.queueName = env.RABBITMQ_QUEUE_NAME;
|
||||
|
||||
this.connect().then(() => {
|
||||
console.log('RabbitMQ connected');
|
||||
}).catch((error) => {
|
||||
console.error('Error connecting to RabbitMQ:', error);
|
||||
});
|
||||
}
|
||||
|
||||
async connect() {
|
||||
const connection = await amqp.connect({
|
||||
protocol: 'amqp',
|
||||
hostname: env.RABBITMQ_HOST,
|
||||
port: parseInt(env.RABBITMQ_PORT),
|
||||
username: env.RABBITMQ_USERNAME,
|
||||
password: env.RABBITMQ_PASSWORD,
|
||||
});
|
||||
this.channel = await connection.createChannel();
|
||||
await this.channel.assertQueue(this.queueName, { durable: true });
|
||||
}
|
||||
|
||||
async create(payload: ITodo, delayTimeForQueue: number) {
|
||||
const message = JSON.stringify({ payload, delayTimeForQueue });
|
||||
const options = { persistent: true, expiration: delayTimeForQueue.toString() };
|
||||
|
||||
try {
|
||||
await this.channel.sendToQueue(this.queueName, Buffer.from(message), options);
|
||||
console.log('Message sent to the queue');
|
||||
} catch (error) {
|
||||
console.error('Error sending message to RabbitMQ:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue