forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ScheduledMessages sample (Azure#79)
- Loading branch information
1 parent
cac1155
commit 34802c0
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { ReceiveMode, Namespace, generateUuid, QueueClient } from "../../lib"; | ||
import * as dotenv from "dotenv"; | ||
dotenv.config(); | ||
|
||
const str = process.env.SERVICEBUS_CONNECTION_STRING || ""; | ||
const path = process.env.QUEUE_NAME || ""; | ||
console.log("str: ", str); | ||
console.log("path: ", path); | ||
|
||
let ns: Namespace; | ||
|
||
async function sendMessages(queueClient: QueueClient, data: any[]): Promise<void> { | ||
for (let index = 0; index < data.length; index++) { | ||
const element = data[index]; | ||
const message = { | ||
body: `${element.firstName} ${element.name}`, | ||
label: "Scientist", | ||
timeToLive: 2 * 60 * 1000, // 2 minutes | ||
messageId: generateUuid() | ||
}; | ||
const scheduledEnqueueTimeUtc = new Date(Date.now() + 30000 + index * 1000); // scheduling message to be sent (30 + index) seconds from now | ||
console.log( | ||
`>>>> Sending message:\t ${message.body}, scheduled for UTC: ${scheduledEnqueueTimeUtc}` | ||
); | ||
await queueClient.scheduleMessage(message, scheduledEnqueueTimeUtc); | ||
} | ||
} | ||
|
||
async function main(): Promise<void> { | ||
ns = Namespace.createFromConnectionString(str); | ||
const client = ns.createQueueClient(path, { receiveMode: ReceiveMode.peekLock }); | ||
const data = [ | ||
{ 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" } | ||
]; | ||
|
||
// schedule messages to appear on Queue at a later time. | ||
await sendMessages(client, data); | ||
|
||
// retrieve all the messages that were sent to the queue | ||
for (let index = 0; index < data.length; index++) { | ||
// retrieve one message at a time. | ||
const msg = await client.receiveBatch(1, 30 + index); // (30 + index) is the maximum wait time in seconds for which the Receiver will wait to receive the message. | ||
|
||
console.log(`Retrieved: ${msg[0].body} - ${msg[0].label}`); | ||
} | ||
|
||
await client.close(); | ||
} | ||
|
||
main() | ||
.then(() => { | ||
console.log(">>>> Calling close...."); | ||
return ns.close(); | ||
}) | ||
.catch((err) => { | ||
console.log("error: ", err); | ||
}); |