universal-queues
Version:
Factory universal para mensageria (RabbitMQ, Kafka, SQS) para sistemas distribuĂdos.
90 lines • 3.83 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RabbitMQClient = void 0;
const amqplib_1 = __importDefault(require("amqplib"));
const retry_1 = require("./decorators/retry");
class RabbitMQClient {
connection;
channel;
async connect(uri) {
this.connection = await amqplib_1.default.connect(uri);
this.channel = await this.connection.createChannel();
}
async publishEvent(exchange, routingKey, message, headers = {}) {
await this.channel.assertExchange(exchange, 'topic', { durable: true });
this.channel.publish(exchange, routingKey, Buffer.from(JSON.stringify(message)), { headers, persistent: true });
}
async subscribeToEvent(exchange, queue, routingKey, handler) {
await this.channel.assertExchange(exchange, 'topic', { durable: true });
await this.channel.assertQueue(queue, {
durable: true,
deadLetterExchange: `${exchange}.dlq`
});
await this.channel.bindQueue(queue, exchange, routingKey);
this.channel.consume(queue, (msg) => {
if (msg) {
try {
const content = JSON.parse(msg.content.toString());
handler(content);
this.ackMessage(msg);
}
catch (err) {
this.nackMessage(msg);
}
}
});
}
async publishToFanout(exchange, message) {
await this.channel.assertExchange(exchange, 'fanout', { durable: true });
this.channel.publish(exchange, '', Buffer.from(JSON.stringify(message)));
}
async subscribeToFanout(exchange, handler) {
await this.channel.assertExchange(exchange, 'fanout', { durable: true });
const q = await this.channel.assertQueue('', { exclusive: true });
await this.channel.bindQueue(q.queue, exchange, '');
this.channel.consume(q.queue, (msg) => {
if (msg) {
handler(JSON.parse(msg.content.toString()));
this.ackMessage(msg);
}
});
}
async publishToOutbox(event) {
console.log('[OUTBOX]', event);
}
async handleDeadLetter(dlqExchange, dlqQueue, handler) {
await this.channel.assertExchange(dlqExchange, 'fanout', { durable: true });
await this.channel.assertQueue(dlqQueue, { durable: true });
await this.channel.bindQueue(dlqQueue, dlqExchange, '');
this.channel.consume(dlqQueue, (msg) => {
if (msg) {
handler(JSON.parse(msg.content.toString()));
this.ackMessage(msg);
}
});
}
ackMessage(msg) {
this.channel.ack(msg);
}
nackMessage(msg, requeue = false) {
this.channel.nack(msg, false, requeue);
}
async close() {
await this.channel.close();
await this.connection.close();
}
}
exports.RabbitMQClient = RabbitMQClient;
__decorate([
(0, retry_1.Retry)(3, 1000)
], RabbitMQClient.prototype, "publishEvent", null);
//# sourceMappingURL=rabbitmq.js.map