You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -5,7 +5,7 @@ description: Learn to use the Azure Queue Storage to create and delete queues. L
5
5
author: normesta
6
6
ms.author: normesta
7
7
ms.reviewer: dineshm
8
-
ms.date: 12/21/2020
8
+
ms.date: 01/20/2023
9
9
ms.topic: how-to
10
10
ms.service: storage
11
11
ms.subservice: queues
@@ -39,150 +39,70 @@ The [Azure Storage client library for JavaScript](https://github.com/Azure/azure
39
39
40
40
1. Use a command-line interface such as PowerShell (Windows), Terminal (Mac), or Bash (Unix), navigate to the folder where you created your sample application.
41
41
42
-
# [JavaScript v12 SDK](#tab/javascript)
43
-
44
42
1. Type `npm install @azure/storage-queue` in the command window.
45
43
46
44
1. Verify that a `node_modules` folder was created. Inside that folder you'll find the `@azure/storage-queue` package, which contains the client library you need to access storage.
47
45
48
-
# [JavaScript v2](#tab/javascript2)
49
-
50
-
1. Type `npm install azure-storage` in the command window.
51
-
52
-
1. Verify that a `node_modules` folder was created. Inside that folder, you'll find the `azure-storage` package, which contains the libraries you need to access storage.
53
-
54
-
---
55
-
56
46
### Import the package
57
47
58
48
Using your code editor, add the following to the top the JavaScript file where you intend to use queues.
The following code gets the value of an environment variable called `AZURE_STORAGE_CONNECTION_STRING` and uses it to create a [`QueueServiceClient`](/javascript/api/@azure/storage-queue/queueserviceclient) object. This object is then used to create a [`QueueClient`](/javascript/api/@azure/storage-queue/queueclient) object that allows you to work with a specific queue.
If the queue already exists, an exception is thrown.
81
61
82
-
#[JavaScript v2](#tab/javascript2)
62
+
## How to format the message
83
63
84
-
The Azure module will read the environment variables `AZURE_STORAGE_ACCOUNT` and `AZURE_STORAGE_ACCESS_KEY`, or `AZURE_STORAGE_CONNECTION_STRING` for information required to connect to your Azure Storage account. If these environment variables aren't set, you must specify the account information when calling `createQueueService`.
64
+
The message type is a string. All messages are treated as strings. If you need to send a different data type, you need to serialize that datatype into a string when sending the message and deserialize the string format when reading the message.
85
65
86
-
The following code creates a `QueueService` object, which enables you to work with queues.
66
+
To convert **JSON** to a string format and back again in Node.js, use the following helper functions:
87
67
88
68
```javascript
89
-
var queueSvc =azure.createQueueService();
90
-
```
91
-
92
-
Call the `createQueueIfNotExists` method to create a new queue with the specified name or return the queue if it already exists.
To add a message to a queue, call the [`sendMessage`](/javascript/api/@azure/storage-queue/queueclient#sendmessage-string--queuesendmessageoptions-) method.
You can peek at messages in the queue without removing them from the queue by calling the `peekMessages` method.
131
90
132
-
# [JavaScript v12 SDK](#tab/javascript)
133
91
134
92
By default, [`peekMessages`](/javascript/api/@azure/storage-queue/queueclient#peekmessages-queuepeekmessagesoptions-) peeks at a single message. The following example peeks at the first five messages in the queue. If fewer than five messages are visible, just the visible messages are returned.
Calling `peekMessages` when there are no messages in the queue won't return an error. However, no messages are returned.
155
97
156
98
## How to change the contents of a queued message
157
99
158
100
The following example updates the text of a message.
159
101
160
-
# [JavaScript v12 SDK](#tab/javascript)
161
-
162
102
Change the contents of a message in-place in the queue by calling [`updateMessage`](/javascript/api/@azure/storage-queue/queueclient#updatemessage-string--string--string--number--queueupdatemessageoptions-).
@@ -193,8 +113,6 @@ Dequeueing a message is a two-stage process:
193
113
194
114
The following example gets a message, then deletes it.
195
115
196
-
# [JavaScript v12 SDK](#tab/javascript)
197
-
198
116
To get a message, call the [`receiveMessages`](/javascript/api/@azure/storage-queue/queueclient#receivemessages-queuereceivemessageoptions-) method. This call makes the messages invisible in the queue, so no other clients can process them. Once your application has processed a message, call [`deleteMessage`](/javascript/api/@azure/storage-queue/queueclient#deletemessage-string--string--queuedeletemessageoptions-) to delete it from the queue.
@@ -203,34 +121,8 @@ By default, a message is only hidden for 30 seconds. After 30 seconds it's visib
203
121
204
122
Calling `receiveMessages` when there are no messages in the queue won't return an error. However, no messages will be returned.
205
123
206
-
# [JavaScript v2](#tab/javascript2)
207
-
208
-
To get a message, call the `getMessages` method. This call makes the messages invisible in the queue, so no other clients can process them. Once your application has processed a message, call `deleteMessage` to delete it from the queue.
By default, a message is only hidden for 30 seconds. After 30 seconds it's visible to other clients. You can specify a different value by using `options.visibilityTimeout` with `getMessages`.
225
-
226
-
Using `getMessages` when there are no messages in the queue won't return an error. However, no messages will be returned.
227
-
228
-
---
229
-
230
124
## Additional options for dequeuing messages
231
125
232
-
# [JavaScript v12 SDK](#tab/javascript)
233
-
234
126
There are two ways you can customize message retrieval from a queue:
235
127
236
128
-[`options.numberOfMessages`](/javascript/api/@azure/storage-queue/queuereceivemessageoptions#numberofmessages): Retrieve a batch of messages (up to 32).
@@ -240,106 +132,27 @@ The following example uses the `receiveMessages` method to get five messages in
There are two ways you can customize message retrieval from a queue:
246
-
247
-
-`options.numOfMessages`: Retrieve a batch of messages (up to 32).
248
-
-`options.visibilityTimeout`: Set a longer or shorter invisibility timeout.
249
-
250
-
The following example uses the `getMessages` method to get 15 messages in one call. Then it processes each message using a `for` loop. It also sets the invisibility timeout to five minutes for all messages returned by this method.
The [`getProperties`](/javascript/api/@azure/storage-queue/queueclient#getproperties-queuegetpropertiesoptions-) method returns metadata about the queue, including the approximate number of messages waiting in the queue.
// Queue length is available in results.approximateMessageCount
287
-
}
288
-
});
289
-
```
290
-
291
-
---
292
-
293
142
## How to list queues
294
143
295
-
# [JavaScript v12 SDK](#tab/javascript)
296
-
297
144
To retrieve a list of queues, call [`QueueServiceClient.listQueues`](/javascript/api/@azure/storage-queue/servicelistqueuesoptions#prefix). To retrieve a list filtered by a specific prefix, set [options.prefix](/javascript/api/@azure/storage-queue/servicelistqueuesoptions#prefix) in your call to `listQueues`.
If all queues can't be returned, pass `result.continuationToken` as the first parameter of `listQueuesSegmented` or the second parameter of `listQueuesSegmentedWithPrefix` to retrieve more results.
314
-
315
-
---
316
-
317
148
## How to delete a queue
318
149
319
-
# [JavaScript v12 SDK](#tab/javascript)
320
-
321
150
To delete a queue and all the messages contained in it, call the [`DeleteQueue`](/javascript/api/@azure/storage-queue/queueclient#delete-queuedeleteoptions-) method on the `QueueClient` object.
To clear all messages from a queue without deleting it, call [`ClearMessages`](/javascript/api/@azure/storage-queue/queueclient#clearmessages-queueclearmessagesoptions-).
326
155
327
-
# [JavaScript v2](#tab/javascript2)
328
-
329
-
To delete a queue and all the messages contained in it, call the `deleteQueue` method on the queue object.
0 commit comments