Skip to content

Commit

Permalink
Added the sample code and address the feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
sima-zhu committed Jun 21, 2019
2 parents 69e6e64 + 596fae3 commit 839cbd4
Show file tree
Hide file tree
Showing 68 changed files with 335 additions and 147 deletions.
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);
}
}
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);
}
}
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);
}
}
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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
import com.azure.storage.blob.models.BlobHTTPHeaders;
import com.azure.storage.blob.models.SourceModifiedAccessConditions;
import io.netty.buffer.ByteBuf;
import java.net.URL;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.net.URL;


/**
* Client to an append blob. It may only be instantiated through a {@link AppendBlobClientBuilder}, via
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@
package com.azure.storage.blob;

import com.azure.core.util.Context;
import com.azure.storage.blob.implementation.AzureBlobStorageBuilder;
import com.azure.storage.blob.implementation.AzureBlobStorageImpl;
import com.azure.storage.blob.models.AppendBlobsAppendBlockFromUrlResponse;
import com.azure.storage.blob.models.AppendBlobsAppendBlockResponse;
import com.azure.storage.blob.models.AppendBlobsCreateResponse;
import com.azure.storage.blob.models.BlobHTTPHeaders;
import com.azure.storage.blob.models.SourceModifiedAccessConditions;
import io.netty.buffer.ByteBuf;
import java.net.URL;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.net.URL;

import static com.azure.storage.blob.Utility.postProcessResponse;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
import com.azure.storage.blob.models.BlobHTTPHeaders;
import com.azure.storage.blob.models.SourceModifiedAccessConditions;
import io.netty.buffer.ByteBuf;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.net.URL;
import java.time.Duration;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.azure.core.util.configuration.Configuration;
import com.azure.storage.blob.implementation.AzureBlobStorageBuilder;
import com.azure.storage.blob.implementation.AzureBlobStorageImpl;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@

import com.azure.core.http.HttpPipeline;
import com.azure.core.util.Context;
import com.azure.storage.blob.implementation.AzureBlobStorageBuilder;
import com.azure.storage.blob.implementation.AzureBlobStorageImpl;
import com.azure.storage.blob.models.*;
import com.azure.storage.blob.models.AppendBlobsAppendBlockFromUrlResponse;
import com.azure.storage.blob.models.AppendBlobsAppendBlockResponse;
import com.azure.storage.blob.models.AppendBlobsCreateResponse;
import com.azure.storage.blob.models.BlobHTTPHeaders;
import com.azure.storage.blob.models.SourceModifiedAccessConditions;
import io.netty.buffer.ByteBuf;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.net.URL;
import java.nio.ByteBuffer;
import java.time.Duration;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,31 @@

import com.azure.core.util.Context;
import com.azure.storage.blob.implementation.AzureBlobStorageImpl;
import com.azure.storage.blob.models.*;
import reactor.core.publisher.Mono;

import com.azure.storage.blob.models.AccessTier;
import com.azure.storage.blob.models.BlobHTTPHeaders;
import com.azure.storage.blob.models.BlobStartCopyFromURLHeaders;
import com.azure.storage.blob.models.BlobsAbortCopyFromURLResponse;
import com.azure.storage.blob.models.BlobsAcquireLeaseResponse;
import com.azure.storage.blob.models.BlobsBreakLeaseResponse;
import com.azure.storage.blob.models.BlobsChangeLeaseResponse;
import com.azure.storage.blob.models.BlobsCopyFromURLResponse;
import com.azure.storage.blob.models.BlobsCreateSnapshotResponse;
import com.azure.storage.blob.models.BlobsDeleteResponse;
import com.azure.storage.blob.models.BlobsGetAccountInfoResponse;
import com.azure.storage.blob.models.BlobsGetPropertiesResponse;
import com.azure.storage.blob.models.BlobsReleaseLeaseResponse;
import com.azure.storage.blob.models.BlobsRenewLeaseResponse;
import com.azure.storage.blob.models.BlobsSetHTTPHeadersResponse;
import com.azure.storage.blob.models.BlobsSetMetadataResponse;
import com.azure.storage.blob.models.BlobsSetTierResponse;
import com.azure.storage.blob.models.BlobsStartCopyFromURLResponse;
import com.azure.storage.blob.models.BlobsUndeleteResponse;
import com.azure.storage.blob.models.DeleteSnapshotsOptionType;
import com.azure.storage.blob.models.LeaseAccessConditions;
import com.azure.storage.blob.models.ModifiedAccessConditions;
import com.azure.storage.blob.models.SourceModifiedAccessConditions;
import java.net.URL;
import reactor.core.publisher.Mono;

import static com.azure.storage.blob.Utility.postProcessResponse;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
import com.azure.storage.blob.models.DeleteSnapshotsOptionType;
import com.azure.storage.blob.models.LeaseAccessConditions;
import com.azure.storage.blob.models.ModifiedAccessConditions;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.time.Duration;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
* Client to a blob of any type: block, append, or page. It may only be instantiated through a {@link BlobClientBuilder} or via
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.azure.core.util.configuration.Configuration;
import com.azure.storage.blob.implementation.AzureBlobStorageBuilder;
import com.azure.storage.blob.implementation.AzureBlobStorageImpl;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package com.azure.storage.blob;

import com.azure.storage.blob.models.ListBlobsIncludeItem;

import java.util.ArrayList;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@
import com.azure.storage.blob.models.DeleteSnapshotsOptionType;
import com.azure.storage.blob.models.LeaseAccessConditions;
import com.azure.storage.blob.models.ModifiedAccessConditions;
import reactor.core.publisher.Mono;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.time.Duration;
import reactor.core.publisher.Mono;

/**
* Represents a URL to a blob of any type: block, append, or page. It may be obtained by direct construction or via the
Expand Down
Loading

0 comments on commit 839cbd4

Please sign in to comment.