Skip to content

Instantly share code, notes, and snippets.

@scionwest
Created March 18, 2018 22:13
Show Gist options
  • Save scionwest/1cb843cbb0e27395c3c1900ebf381931 to your computer and use it in GitHub Desktop.
Save scionwest/1cb843cbb0e27395c3c1900ebf381931 to your computer and use it in GitHub Desktop.
module.exports = function() {
var azure = require('azure');
var topic = "foo-bar";
var connectionString = "Endpoint=sb://";
console.log("Configuring Service Bus.");
var serviceBusService = azure.createServiceBusService(connectionString);
var movies = [];
for(count = 0; count < 5000; count++) {
movies.push({id: count});
}
var promise = new Promise(function(resolve, reject) {
serviceBusService.createTopicIfNotExists(topic, function(error) {
if (error) {
console.log('failed to get the service bus topic');
reject();
}
var messages = [];
for(index = 0; index < movies.length; index++) {
var message = {
body: JSON.stringify(movies[index]),
customProperties: {
messageNumber: index
}
}
messages.push(message);
}
resolve(messages);
});
});
promise.then(messages => {
var promises = [];
while (messages.length > 0) {
for(index = 0; index < 1000; index++) {
promises.push(sendMessage(serviceBusService, topic, messages[index]));
}
messages.splice(0, 1000);
Promise.all(promises).then(() => {
promises = []
});
}
});
}
function sendMessage(serviceBusService, topic, message) {
return new Promise((resolve, reject) => {
serviceBusService.sendTopicMessage(topic, message, (error, result) => {
if (error) {
console.log(error);
reject();
}
resolve();
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment