-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the sample code and address the feedback.
- Loading branch information
Showing
68 changed files
with
335 additions
and
147 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
storage/client/src/Samples/java/com/azure/storage/queue/AsyncSamples.java
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,42 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.storage.queue; | ||
import java.util.UUID; | ||
|
||
/* | ||
* This example mimics some arbitrary number of clients continuously sending messages up to a queue in a parallel and | ||
* a server dequeuing the messages and processing them. | ||
*/ | ||
public class AsyncSamples { | ||
private static final String accountName = System.getenv("AZURE_STORAGE_ACCOUNT_NAME"); | ||
private static final String sasToken = System.getenv("PRIMARY_SAS_TOKEN"); | ||
private static final String queueName = generateRandomName("async-call", 16); | ||
|
||
public static void main(String[] args) { | ||
// Create an async queue client. | ||
String queueURL = String.format("https://%s.queue.core.windows.net/%s%s", accountName, queueName, sasToken); | ||
QueueAsyncClient queueAsyncClient = new QueueAsyncClientBuilder().endpoint(queueURL).build(); | ||
|
||
// Create a queue, enqueue two messages. | ||
queueAsyncClient.create() | ||
.flatMap(response -> queueAsyncClient.enqueueMessage("This is message 1")) | ||
.flatMap(response -> queueAsyncClient.enqueueMessage("This is message 2")) | ||
.subscribe( | ||
response -> { | ||
System.out.println("Message successfully equeueed by queueAsyncClient. Message id:" + response.value().messageId()); | ||
}, | ||
err -> { | ||
System.out.println("Error thrown when enqueue the message. Error message: " + err.getMessage()); | ||
}, | ||
() -> { | ||
System.out.println("The enqueue has been completed."); | ||
} | ||
); | ||
} | ||
|
||
private static String generateRandomName(String prefix, int length) { | ||
int len = length > prefix.length() ? length - prefix.length() : 0; | ||
return prefix + UUID.randomUUID().toString().substring(0, len); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
storage/client/src/Samples/java/com/azure/storage/queue/MessageSample.java
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,74 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.storage.queue; | ||
|
||
import com.azure.core.http.rest.Response; | ||
import com.azure.storage.queue.QueueClient; | ||
import com.azure.storage.queue.QueueServiceClient; | ||
import com.azure.storage.queue.models.DequeuedMessage; | ||
|
||
import java.time.Duration; | ||
import java.util.UUID; | ||
|
||
public class MessageSample { | ||
private static final String accountName = System.getenv("AZURE_STORAGE_ACCOUNT_NAME"); | ||
private static final String sasToken = System.getenv("PRIMARY_SAS_TOKEN"); | ||
|
||
public static void main(String[] args) throws Exception { | ||
// Build Queue Client using SAS Token | ||
String queueServiceURL = String.format("https://%s.queue.core.windows.net/%s", accountName, sasToken); | ||
QueueServiceClient queueServiceClient = QueueServiceClient.builder().endpoint(queueServiceURL).build(); | ||
|
||
// Create a queue client | ||
Response<QueueClient> queueClientResponse = queueServiceClient.createQueue(generateRandomName("enqueue", 16)); | ||
QueueClient queueClient = queueClientResponse.value(); | ||
// Using queue client to enqueue several "Hello World" messages into queue. | ||
for (int i = 0; i < 3; i++) { | ||
queueClient.enqueueMessage("Hello World"); | ||
} | ||
|
||
// Enqueue json file into message. | ||
// TODO | ||
|
||
// Get the total count of msg in the queue | ||
int count = queueClient.getProperties().value().getApproximateMessagesCount(); | ||
|
||
// Peek all messages in queue. It is supposed to print "Hello World" 3 times. | ||
queueClient.peekMessages(count).forEach( | ||
peekedMessage -> { | ||
System.out.println("Here is the msg: " + peekedMessage.messageText()); | ||
} | ||
); | ||
|
||
// Dequeue all messages in queue and update the message "Hello World" to Hello, world!" | ||
queueClient.dequeueMessages(count, Duration.ZERO).forEach( | ||
queueMessage -> { | ||
String msgToReplace = String.format("Hello, world!"); | ||
queueClient.updateMessage(queueMessage.messageId(), msgToReplace, queueMessage.popReceipt(), Duration.ZERO); | ||
} | ||
); | ||
|
||
// Delete the first available msg. | ||
// Since there is no invisible time for above dequeue, the following if condition should be true. | ||
if (queueClient.dequeueMessages().iterator().hasNext()) { | ||
DequeuedMessage queueMessage = queueClient.dequeueMessages().iterator().next(); | ||
queueClient.deleteMessage(queueMessage.messageId(), queueMessage.popReceipt()); | ||
} else { | ||
System.out.println("OOps, the messages disappear!"); | ||
} | ||
|
||
// Clear all messages in the queue | ||
// Sleep to guarantee we skip the default invisible time. | ||
Thread.sleep(500); | ||
queueClient.clearMessages(); | ||
|
||
// Finally, we delete the queue. | ||
queueClient.delete(); | ||
} | ||
|
||
private static String generateRandomName(String prefix, int length) { | ||
int len = length > prefix.length() ? length - prefix.length() : 0; | ||
return prefix + UUID.randomUUID().toString().substring(0, len); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
storage/client/src/Samples/java/com/azure/storage/queue/QueueExceptionSamples.java
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,68 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.storage.queue; | ||
|
||
import com.azure.core.http.rest.Response; | ||
import com.azure.storage.queue.models.StorageErrorCode; | ||
import com.azure.storage.queue.models.StorageErrorException; | ||
|
||
import java.util.UUID; | ||
|
||
public class QueueExceptionSamples { | ||
private static final String accountName = System.getenv("AZURE_STORAGE_ACCOUNT_NAME"); | ||
private static final String sasToken = System.getenv("PRIMARY_SAS_TOKEN"); | ||
|
||
public static void main(String[] args) { | ||
// Create a queue service client. | ||
String queueServiceURL = String.format("https://%s.queue.core.windows.net/%s", accountName, sasToken); | ||
QueueServiceClient queueServiceClient = QueueServiceClient.builder().endpoint(queueServiceURL).build(); | ||
|
||
// Create queue client. | ||
Response<QueueClient> queueClientResponse = null; | ||
try { | ||
queueClientResponse = queueServiceClient.createQueue(generateRandomName("delete-not-exist", 16)); | ||
System.out.println("Successfully create the queue! Status code: " + String.valueOf(queueClientResponse.statusCode())); | ||
} catch (StorageErrorException e) { | ||
System.out.println(String.format("Error creating a queue. Error message: %s", e.value().message())); | ||
} | ||
QueueClient queueClient = queueClientResponse.value(); | ||
queueClient.enqueueMessage("Hello, message 1!"); | ||
queueClient.enqueueMessage("Hello, message 2!"); | ||
|
||
// Delete message with wrong message id. | ||
try { | ||
queueClientResponse.value().dequeueMessages().forEach( | ||
msg -> { | ||
queueClient.deleteMessage("wrong id", msg.popReceipt()); | ||
} | ||
); | ||
} catch (StorageErrorException e) { | ||
if (e.getMessage().contains(StorageErrorCode.MESSAGE_NOT_FOUND.toString())) { | ||
System.out.println("This is the error expected to throw"); | ||
} else { | ||
System.out.println("This is not the error we expect!"); | ||
} | ||
} | ||
|
||
// Delete message with wrong pop receipt. | ||
try { | ||
queueClient.dequeueMessages().forEach( | ||
msg -> { | ||
queueClient.deleteMessage(msg.messageId(), "Wrong Pop Receipt"); | ||
} | ||
); | ||
} catch (StorageErrorException e) { | ||
if (e.getMessage().contains(StorageErrorCode.INVALID_QUERY_PARAMETER_VALUE.toString())) { | ||
System.out.println("This is the error expected to throw"); | ||
} else { | ||
System.out.println("This is not the error we expect!"); | ||
} | ||
} | ||
} | ||
|
||
private static String generateRandomName(String prefix, int length) { | ||
int len = length > prefix.length() ? length - prefix.length() : 0; | ||
return prefix + UUID.randomUUID().toString().substring(0, len); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
storage/client/src/Samples/java/com/azure/storage/queue/QueueServiceSample.java
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,35 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.storage.queue; | ||
|
||
import com.azure.storage.queue.QueueServiceClient; | ||
|
||
import java.util.UUID; | ||
|
||
public class QueueServiceSample { | ||
private static final String accountName = System.getenv("AZURE_STORAGE_ACCOUNT_NAME"); | ||
private static final String sasToken = System.getenv("PRIMARY_SAS_TOKEN"); | ||
|
||
public static void main(String[] args) { | ||
// Build Queue Service Client using SAS Token | ||
String queueServiceURL = String.format("https://%s.queue.core.windows.net/%s", accountName, sasToken); | ||
QueueServiceClient queueServiceClient = QueueServiceClient.builder().endpoint(queueServiceURL).build(); | ||
queueServiceClient.createQueue(generateRandomName("create-queue", 16)); | ||
|
||
// Create another queue and list all queues, print the name and then delete the queue. | ||
queueServiceClient.createQueue(generateRandomName("create-extra" , 16)); | ||
queueServiceClient.listQueues().forEach( | ||
queueItem -> { | ||
System.out.println("The queue name is: " + queueItem.name()); | ||
queueServiceClient.deleteQueue(queueItem.name()); | ||
} | ||
); | ||
} | ||
|
||
private static String generateRandomName(String prefix, int length) { | ||
int len = length > prefix.length() ? length - prefix.length() : 0; | ||
return prefix + UUID.randomUUID().toString().substring(0, len); | ||
} | ||
|
||
} |
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.