Skip to content

Commit

Permalink
Service Bus SDK updated
Browse files Browse the repository at this point in the history
  • Loading branch information
johnwatson484 committed Dec 19, 2020
1 parent 02d3c16 commit 5980916
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 210 deletions.
16 changes: 2 additions & 14 deletions app/messaging/message-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,11 @@ const { ServiceBusClient } = require('@azure/service-bus')
class MessageBase {
constructor (name, config) {
this.name = name
this.sbClient = ServiceBusClient.createFromConnectionString(config.connectionString)
this.entityClient = this.createEntityClient(config)
}

createEntityClient (config) {
switch (config.type) {
case 'queue':
return this.sbClient.createQueueClient(config.address)
case 'topic':
return this.sbClient.createTopicClient(config.address)
case 'subscription':
return this.sbClient.createSubscriptionClient(config.topic, config.address)
}
this.config = config
this.sbClient = new ServiceBusClient(config.connectionString)
}

async closeConnection () {
await this.entityClient.close()
await this.sbClient.close()
console.log(`${this.name} connection closed`)
}
Expand Down
3 changes: 1 addition & 2 deletions app/messaging/message-receiver.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
const { ReceiveMode } = require('@azure/service-bus')
const MessageBase = require('./message-base')

class MessageReceiver extends MessageBase {
constructor (name, config, action) {
super(name, config)
this.receiverHandler = this.receiverHandler.bind(this)
this.action = action
this.receiver = this.entityClient.createReceiver(ReceiveMode.peekLock)
this.receiver = config.type === 'subscription' ? this.sbClient.createReceiver(config.address, config.topic) : this.sbClient.createReceiver(config.address)
this.receiver.registerMessageHandler(this.receiverHandler, this.receiverError)
}

Expand Down
6 changes: 3 additions & 3 deletions app/messaging/message-sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ class MessageSender extends MessageBase {
constructor (name, config) {
super(name, config)
this.sendMessage = this.sendMessage.bind(this)
this.sender = this.sbClient.createSender(config.address)
}

async sendMessage (message) {
const sender = this.entityClient.createSender()
try {
console.log(`${this.name} sending message`)
await sender.send({ body: message })
await this.sender.sendMessages({ body: message })
} catch (error) {
console.error('failed to send message', error)
throw error
} finally {
await sender.close()
await this.sender.close()
}
return message
}
Expand Down
Loading

0 comments on commit 5980916

Please sign in to comment.