Quickstart: Azure Queue Storage client library v12 for Java
Get started with the Azure Queue Storage client library v12 for Java. Azure Queue Storage is a service for storing large numbers of messages for later retrieval and processing. Follow these steps to install the package and try out example code for basic tasks.
Use the Azure Queue Storage client library v12 for Java to:
- Create a queue
- Add messages to a queue
- Peek at messages in a queue
- Update a message in a queue
- Receive and delete messages from a queue
- Delete a queue
Additional resources:
Prerequisites
- Java Development Kit (JDK) version 8 or above
- Apache Maven
- Azure subscription - create one for free
- Azure Storage account - create a storage account
Setting up
This section walks you through preparing a project to work with the Azure Queue Storage client library v12 for Java.
Create the project
Create a Java application named queues-quickstart-v12.
In a console window (such as cmd, PowerShell, or Bash), use Maven to create a new console app with the name
queues-quickstart-v12. Type the followingmvncommand to create a "hello world" Java project.mvn archetype:generate ` --define interactiveMode=n ` --define groupId=com.queues.quickstart ` --define artifactId=queues-quickstart-v12 ` --define archetypeArtifactId=maven-archetype-quickstart ` --define archetypeVersion=1.4The output from generating the project should look something like this:
[INFO] Scanning for projects... [INFO] [INFO] ------------------< org.apache.maven:standalone-pom >------------------- [INFO] Building Maven Stub Project (No POM) 1 [INFO] --------------------------------[ pom ]--------------------------------- [INFO] [INFO] >>> maven-archetype-plugin:3.1.2:generate (default-cli) > generate-sources @ standalone-pom >>> [INFO] [INFO] <<< maven-archetype-plugin:3.1.2:generate (default-cli) < generate-sources @ standalone-pom <<< [INFO] [INFO] [INFO] --- maven-archetype-plugin:3.1.2:generate (default-cli) @ standalone-pom --- [INFO] Generating project in Batch mode [INFO] ---------------------------------------------------------------------------- [INFO] Using following parameters for creating project from Archetype: maven-archetype-quickstart:1.4 [INFO] ---------------------------------------------------------------------------- [INFO] Parameter: groupId, Value: com.queues.quickstart [INFO] Parameter: artifactId, Value: queues-quickstart-v12 [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: package, Value: com.queues.quickstart [INFO] Parameter: packageInPathFormat, Value: com/queues/quickstart [INFO] Parameter: version, Value: 1.0-SNAPSHOT [INFO] Parameter: package, Value: com.queues.quickstart [INFO] Parameter: groupId, Value: com.queues.quickstart [INFO] Parameter: artifactId, Value: queues-quickstart-v12 [INFO] Project created from Archetype in dir: C:\quickstarts\queues\queues-quickstart-v12 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 6.394 s [INFO] Finished at: 2019-12-03T09:58:35-08:00 [INFO] ------------------------------------------------------------------------Switch to the newly created
queues-quickstart-v12directory.cd queues-quickstart-v12
Install the package
Open the pom.xml file in your text editor. Add the following dependency element to the group of dependencies.
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-queue</artifactId>
<version>12.0.1</version>
</dependency>
Set up the app framework
From the project directory:
- Navigate to the
/src/main/java/com/queues/quickstartdirectory - Open the
App.javafile in your editor - Delete the
System.out.println("Hello, world");statement - Add
importdirectives
Here's the code:
package com.queues.quickstart;
/**
* Azure Queue Storage client library v12 quickstart
*/
import com.azure.storage.queue.*;
import com.azure.storage.queue.models.*;
import java.io.*;
import java.time.*;
public class App
{
public static void main( String[] args ) throws IOException
{
}
}
Copy your credentials from the Azure portal
When the sample application makes a request to Azure Storage, it must be authorized. To authorize a request, add your storage account credentials to the application as a connection string. To view your storage account credentials, follow these steps:
Sign in to the Azure portal.
Locate your storage account.
In the storage account menu pane, under Security + networking, select Access keys. Here, you can view the account access keys and the complete connection string for each key.

In the Access keys pane, select Show keys.
In the key1 section, locate the Connection string value. Select the Copy to clipboard icon to copy the connection string. You will add the connection string value to an environment variable in the next section.

Configure your storage connection string
After you copy the connection string, write it to a new environment variable on the local machine running the application. To set the environment variable, open a console window, and follow the instructions for your operating system. Replace <yourconnectionstring> with your actual connection string.
Windows
setx AZURE_STORAGE_CONNECTION_STRING "<yourconnectionstring>"
After you add the environment variable in Windows, you must start a new instance of the command window.
Linux
export AZURE_STORAGE_CONNECTION_STRING="<yourconnectionstring>"
macOS
export AZURE_STORAGE_CONNECTION_STRING="<yourconnectionstring>"
Restart programs
After you add the environment variable, restart any running programs that will need to read the environment variable. For example, restart your development environment or editor before you continue.
Object model
Azure Queue Storage is a service for storing large numbers of messages. A queue message can be up to 64 KB in size. A queue may contain millions of messages, up to the total capacity limit of a storage account. Queues are commonly used to create a backlog of work to process asynchronously. Queue Storage offers three types of resources:
- The storage account
- A queue in the storage account
- Messages within the queue
The following diagram shows the relationship between these resources.

Use the following Java classes to interact with these resources:
QueueClientBuilder: TheQueueClientBuilderclass configures and instantiates aQueueClientobject.QueueServiceClient: TheQueueServiceClientallows you to manage the all queues in your storage account.QueueClient: TheQueueClientclass allows you to manage and manipulate an individual queue and its messages.QueueMessageItem: TheQueueMessageItemclass represents the individual objects returned when callingReceiveMessageson a queue.
Code examples
These example code snippets show you how to do the following actions with the Azure Queue Storage client library for Java:
- Get the connection string
- Create a queue
- Add messages to a queue
- Peek at messages in a queue
- Update a message in a queue
- Receive and delete messages from a queue
- Delete a queue
Get the connection string
The following code retrieves the connection string for the storage account. The connection string is stored the environment variable created in the Configure your storage connection string section.
Add this code inside the main method:
System.out.println("Azure Queue Storage client library v12 - Java quickstart sample\n");
// Retrieve the connection string for use with the application. The storage
// connection string is stored in an environment variable on the machine
// running the application called AZURE_STORAGE_CONNECTION_STRING. If the environment variable
// is created after the application is launched in a console or with
// Visual Studio, the shell or application needs to be closed and reloaded
// to take the environment variable into account.
String connectStr = System.getenv("AZURE_STORAGE_CONNECTION_STRING");
Create a queue
Decide on a name for the new queue. The following code appends a GUID value to the queue name to ensure that it's unique.
Important
Queue names may only contain lowercase letters, numbers, and hyphens, and must begin with a letter or a number. Each hyphen must be preceded and followed by a non-hyphen character. The name must also be between 3 and 63 characters long. For more information about naming queues, see Naming queues and metadata.
Create an instance of the QueueClient class. Then, call the Create method to create the queue in your storage account.
Add this code to the end of the main method:
// Create a unique name for the queue
String queueName = "quickstartqueues-" + java.util.UUID.randomUUID();
System.out.println("Creating queue: " + queueName);
// Instantiate a QueueClient which will be
// used to create and manipulate the queue
QueueClient queueClient = new QueueClientBuilder()
.connectionString(connectStr)
.queueName(queueName)
.buildClient();
// Create the queue
queueClient.create();
Add messages to a queue
The following code snippet adds messages to queue by calling the sendMessage method. It also saves a SendMessageResult returned from a sendMessage call. The result is used to update the message later in the program.
Add this code to the end of the main method:
System.out.println("\nAdding messages to the queue...");
// Send several messages to the queue
queueClient.sendMessage("First message");
queueClient.sendMessage("Second message");
// Save the result so we can update this message later
SendMessageResult result = queueClient.sendMessage("Third message");
Peek at messages in a queue
Peek at the messages in the queue by calling the peekMessages method. This method retrieves one or more messages from the front of the queue but doesn't alter the visibility of the message.
Add this code to the end of the main method:
System.out.println("\nPeek at the messages in the queue...");
// Peek at messages in the queue
queueClient.peekMessages(10, null, null).forEach(
peekedMessage -> System.out.println("Message: " + peekedMessage.getMessageText()));
Update a message in a queue
Update the contents of a message by calling the updateMessage method. This method can change a message's visibility timeout and contents. The message content must be a UTF-8 encoded string that is up to 64 KB in size. Along with new content for the message, pass in the message ID and pop receipt by using the SendMessageResult that was saved earlier in the code. The message ID and pop receipt identify which message to update.
System.out.println("\nUpdating the third message in the queue...");
// Update a message using the result that
// was saved when sending the message
queueClient.updateMessage(result.getMessageId(),
result.getPopReceipt(),
"Third message has been updated",
Duration.ofSeconds(1));
Receive and delete messages from a queue
Download previously added messages by calling the receiveMessages method. The example code also deletes messages from the queue after they're received and processed. In this case, processing is just displaying the message on the console.
The app pauses for user input by calling System.console().readLine(); before it receives and deletes the messages. Verify in your Azure portal that the resources were created correctly, before they're deleted. Any messages not explicitly deleted will eventually become visible in the queue again for another chance to process them.
Add this code to the end of the main method:
System.out.println("\nPress Enter key to receive messages and delete them from the queue...");
System.console().readLine();
// Get messages from the queue
queueClient.receiveMessages(10).forEach(
// "Process" the message
receivedMessage -> {
System.out.println("Message: " + receivedMessage.getMessageText());
// Let the service know we're finished with
// the message and it can be safely deleted.
queueClient.deleteMessage(receivedMessage.getMessageId(), receivedMessage.getPopReceipt());
}
);
Delete a queue
The following code cleans up the resources the app created by deleting the queue using the Delete method.
Add this code to the end of the main method:
System.out.println("\nPress Enter key to delete the queue...");
System.console().readLine();
// Clean up
System.out.println("Deleting queue: " + queueClient.getQueueName());
queueClient.delete();
System.out.println("Done");
Run the code
This app creates and adds three messages to an Azure queue. The code lists the messages in the queue, then retrieves and deletes them, before finally deleting the queue.
In your console window, navigate to your application directory, then build and run the application.
mvn compile
Then, build the package.
mvn package
Use the following mvn command to run the app.
mvn exec:java -Dexec.mainClass="com.queues.quickstart.App" -Dexec.cleanupDaemonThreads=false
The output of the app is similar to the following example:
Azure Queue Storage client library v12 - Java quickstart sample
Adding messages to the queue...
Peek at the messages in the queue...
Message: First message
Message: Second message
Message: Third message
Updating the third message in the queue...
Press Enter key to receive messages and delete them from the queue...
Message: First message
Message: Second message
Message: Third message has been updated
Press Enter key to delete the queue...
Deleting queue: quickstartqueues-fbf58f33-4d5a-41ac-ac0e-1a05d01c7003
Done
When the app pauses before receiving messages, check your storage account in the Azure portal. Verify the messages are in the queue.
Press the Enter key to receive and delete the messages. When prompted, press the Enter key again to delete the queue and finish the demo.
Next steps
In this quickstart, you learned how to create a queue and add messages to it using Java code. Then you learned to peek, retrieve, and delete messages. Finally, you learned how to delete a message queue.
For tutorials, samples, quick starts, and other documentation, visit:
- For more Azure Queue Storage sample apps, see Azure Queue Storage client library v12 for Java - samples.