@nestjstools/messaging-azure-service-bus-extension
Version:
Extension to handle messages and dispatch them over Azure service bus
72 lines • 3.73 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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AzureServiceBusMessagingConsumer = void 0;
const azure_service_bus_channel_1 = require("../channel/azure-service-bus.channel");
const messaging_1 = require("@nestjstools/messaging");
const common_1 = require("@nestjs/common");
const messaging_2 = require("@nestjstools/messaging");
const const_1 = require("../const");
let AzureServiceBusMessagingConsumer = class AzureServiceBusMessagingConsumer {
channel;
async consume(dispatcher, channel) {
this.channel = channel;
await this.autoCreate();
let receiver;
if (this.channel.isQueueMode()) {
receiver = this.channel.client.createReceiver(this.channel.getQueue());
}
else {
receiver = this.channel.client.createReceiver(this.channel.getTopic(), this.channel.getSubscription());
}
receiver.subscribe({
processMessage: async (message) => {
const routingKey = message.applicationProperties?.[const_1.ROUTING_KEY_ATTRIBUTE_NAME];
if (!routingKey) {
throw new Error(`Routing header [${const_1.ROUTING_KEY_ATTRIBUTE_NAME}] not found in message attribute`);
}
await dispatcher.dispatch(new messaging_1.ConsumerMessage(message.body, routingKey));
},
processError: async (args) => {
console.error(`Error occurred with Azure service bus: ${args.error.message}`, args.error);
},
});
return Promise.resolve();
}
async autoCreate() {
if (this.channel.config.autoCreate &&
this.channel.adminClient &&
this.channel.isQueueMode()) {
const isExists = await this.channel.adminClient.queueExists(this.channel.getQueue());
if (!isExists) {
await this.channel.adminClient.createQueue(this.channel.getQueue());
}
}
if (this.channel.config.autoCreate &&
this.channel.adminClient &&
this.channel.isTopicMode()) {
const topicExists = await this.channel.adminClient.topicExists(this.channel.getTopic());
if (!topicExists) {
await this.channel.adminClient.createTopic(this.channel.getTopic());
}
const subscriptionExists = await this.channel.adminClient.subscriptionExists(this.channel.getTopic(), this.channel.getSubscription());
if (!subscriptionExists) {
await this.channel.adminClient.createSubscription(this.channel.getTopic(), this.channel.getSubscription());
}
}
}
async onError(errored, channel) {
return Promise.resolve();
}
};
exports.AzureServiceBusMessagingConsumer = AzureServiceBusMessagingConsumer;
exports.AzureServiceBusMessagingConsumer = AzureServiceBusMessagingConsumer = __decorate([
(0, common_1.Injectable)(),
(0, messaging_2.MessageConsumer)(azure_service_bus_channel_1.AzureServiceBusChannel)
], AzureServiceBusMessagingConsumer);
//# sourceMappingURL=azure-service-bus-messaging-consumer.js.map