rabbitmq-enterprise-toolkit
Version:
🚀 Enterprise-grade RabbitMQ wrapper for Node.js & TypeScript - High-throughput messaging with automatic retry, DLQ, batch processing & graceful shutdown. Production-ready AMQP client with zero message loss guarantee.
60 lines • 2.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MessageProcessor = void 0;
const retry_handler_1 = require("./retry-handler");
const helpers_1 = require("../utils/helpers");
class MessageProcessor {
constructor(getChannel) {
this.getChannel = getChannel;
this.retryHandler = new retry_handler_1.RetryHandler(getChannel);
}
async processMessage(msg, options) {
let content;
try {
content = JSON.parse(msg.content.toString());
const processedMessage = this.createProcessedMessage(msg, content);
// Execute business logic
await options.processor(processedMessage.content);
// Acknowledge successful processing
await processedMessage.ack();
}
catch (error) {
await this.handleProcessingError(msg, options, error, content);
}
}
async handleProcessingError(msg, options, error, content) {
// Call error callback if provided
if (options.errCallback) {
try {
const errorObj = error instanceof Error ? error : new Error((0, helpers_1.formatError)(error));
await options.errCallback(errorObj, content);
}
catch (callbackError) {
console.error('Error callback failed:', callbackError);
}
}
// Handle retry logic
await this.retryHandler.handleRetry(msg, options, error);
}
createProcessedMessage(msg, content) {
const channel = this.getChannel();
const headers = msg.properties.headers || {};
return {
id: content.id,
content: content.content,
headers,
timestamp: content.timestamp,
ack: async () => {
channel.ack(msg);
},
nack: async (requeue = false) => {
channel.nack(msg, false, requeue);
},
reject: async (requeue = false) => {
channel.reject(msg, requeue);
}
};
}
}
exports.MessageProcessor = MessageProcessor;
//# sourceMappingURL=message-processor.js.map