I want to fetch messages in bulk from azure service bus queues using node js azure package, Now i am able to fetch message using set-interval.is there any other way to fetch messages in bulk.
I want to fetch messages in bulk from azure service bus queues using node js azure package, Now i am able to fetch message using set-interval.is there any other way to fetch messages in bulk.
Share Improve this question asked May 31, 2018 at 11:40 Prakash RPrakash R 4092 silver badges14 bronze badges4 Answers
Reset to default 2 +50The Service Bus REST API does not currently support receiving batches, so there is not a valid implementation in this library. Part of the reason is that the REST implementation is not particularly performant, and it is much better to use AMQP. If you are looking for something with better performance, I would take a look at this library:
https://github./noodlefrenzy/node-amqp10
Related issue on github
Also you can try this out
.
It is possible to peek specified number of messages from a ServiceBus queue in bulk. Messages can be received from a queue using the receiveQueueMessage method( which supports receiving only one message at a time) on ServiceBusService in Node.js Azure package. By default messages will be deleted from the queue as they are read, however when received in peeklock mode by setting the optional parameter isPeekLock
to true can prevent deletion
Pre- Requisites to use Azure
ServiceBus:
Node.js
Azure
package, which can be installed using mand,npm install azure
- An environment variable
AZURE_SERVICEBUS_CONNECTION_STRING
, with information required by the Azure module to connect to theServiceBus
queue
Below is a sample code to receive 10 messages,
//Import the module
var azure = require('azure');
//Create ServiceBusService object
var serviceBusService = azure.createServiceBusService();
var receivedMessages = [];
while(i<10){
serviceBusService.receiveQueueMessage('myqueue', { isPeekLock: true },
function(error, lockedMessage){
if(!error){
// Message received and locked
receivedMessages.push(lockedMessage)
}
}
});
Console.log(receivedMessages);
You can use https://www.npmjs./package/@azure/service-bus
Here's a sample code for you create a receiver object and then get multiple messages:
let receivingMode = ReceiveMode.peekLock;//Or receiveAndDelete
let sbClient = ServiceBusClient.createFromConnectionString(connectionString);
let subscriptionClient = this.sbClient.createSubscriptionClient(topicName, subscriptionName);
let receiver = this.subscriptionClient.createReceiver(receivingMode);
let messages = await this.receiver.receiveMessages(maxMessageCount, maxWaitTimeInSeconds);
For example if maxMessageCount = 10 and maxWaitTimeInSeconds = 60 it means the function will return 10 messages as soon there are 10 and if it waited for 60 seconds it will return anyway.
Try the @azure/service-bus package which has an api to receive messages in batches. This takes the max batch size and max time to wait as input. You will either get the number of messages you asked for or the number of messages that were fetched within the provided time. Checkout the sample that receives messages in a loop.
const sbClient = new ServiceBusClient(connectionString);
const queueReceiver = sbClient.createReceiver(queueName);
const messages = await queueReceiver.receiveMessages(10);