inceptum
Version:
hipages take on the foundational library for enterprise-grade apps written in NodeJS
42 lines • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const RabbitmqConfig_1 = require("./RabbitmqConfig");
const RabbitmqClient_1 = require("./RabbitmqClient");
class RabbitmqProducer extends RabbitmqClient_1.RabbitmqClient {
constructor(clientConfig, name, producerConfig) {
super(clientConfig, name);
this.producerConfig = Object.assign({}, producerConfig);
this.producerConfig.backPressureStrategy = producerConfig.backPressureStrategy || RabbitmqConfig_1.BackPressureStrategy.ERROR;
}
/**
* publish msg with routing key
* @param msg
* @param routingKey
*/
async publish(msg, routingKey, optionsPublish = {}) {
optionsPublish.headers = {
retriesCount: 0,
};
while (!this.channel.publish(this.producerConfig.exchangeName, routingKey, new Buffer(msg), optionsPublish)) {
if (this.producerConfig.backPressureStrategy === RabbitmqConfig_1.BackPressureStrategy.ERROR) {
throw new Error('Failed to publish message');
}
this.logger.error(`publish failed. ====> ${msg}`);
await new Promise((resolve, reject) => {
this.channel.once('drain', function () {
this.logger.info(`drain event received. ====> ${msg}`);
resolve();
});
});
}
return Promise.resolve(true);
}
async init() {
await super.init();
}
async close() {
await super.close();
}
}
exports.RabbitmqProducer = RabbitmqProducer;
//# sourceMappingURL=RabbitmqProducer.js.map