/
sendMessages.ts
58 lines (47 loc) · 1.84 KB
/
sendMessages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT Licence.
This sample demonstrates how the send() function can be used to send messages to Service Bus
Queue/Topic.
See https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-queues-topics-subscriptions
to learn about Queues, Topics and Subscriptions.
*/
import { ServiceBusClient, SendableMessageInfo } from "@azure/service-bus";
// Define connection string and related Service Bus entity names here
const connectionString = "";
const queueName = "";
const listOfScientists = [
{ name: "Einstein", firstName: "Albert" },
{ name: "Heisenberg", firstName: "Werner" },
{ name: "Curie", firstName: "Marie" },
{ name: "Hawking", firstName: "Steven" },
{ name: "Newton", firstName: "Isaac" },
{ name: "Bohr", firstName: "Niels" },
{ name: "Faraday", firstName: "Michael" },
{ name: "Galilei", firstName: "Galileo" },
{ name: "Kepler", firstName: "Johannes" },
{ name: "Kopernikus", firstName: "Nikolaus" }
];
async function main(): Promise<void> {
const sbClient = ServiceBusClient.createFromConnectionString(connectionString);
// If sending to a Topic, use `createTopicClient` instead of `createQueueClient`
const queueClient = sbClient.createQueueClient(queueName);
const sender = queueClient.createSender();
try {
for (let index = 0; index < listOfScientists.length; index++) {
const scientist = listOfScientists[index];
const message: SendableMessageInfo = {
body: `${scientist.firstName} ${scientist.name}`,
label: "Scientist"
};
console.log(`Sending message: ${message.body} - ${message.label}`);
await sender.send(message);
}
await queueClient.close();
} finally {
await sbClient.close();
}
}
main().catch((err) => {
console.log("Error occurred: ", err);
});