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
In this quickstart, you'll create a Java app to send messages to and receive messages from an Azure Service Bus queue.
17
+
In this quickstart, you create a Java app to send messages to and receive messages from an Azure Service Bus queue.
18
18
19
19
> [!NOTE]
20
20
> This quick start provides step-by-step instructions for a simple scenario of sending messages to a Service Bus queue and receiving them. You can find pre-built Java samples for Azure Service Bus in the [Azure SDK for Java repository on GitHub](https://github.com/azure/azure-sdk-for-java/tree/main/sdk/servicebus/azure-messaging-servicebus/src/samples).
@@ -24,52 +24,76 @@ In this quickstart, you'll create a Java app to send messages to and receive mes
24
24
25
25
## Prerequisites
26
26
- An Azure subscription. To complete this tutorial, you need an Azure account. You can activate your [MSDN subscriber benefits](https://azure.microsoft.com/pricing/member-offers/credit-for-visual-studio-subscribers/?WT.mc_id=A85619ABF) or sign up for a [free account](https://azure.microsoft.com/free/?WT.mc_id=A85619ABF).
27
-
- If you don't have a queue to work with, follow steps in the [Use Azure portal to create a Service Bus queue](service-bus-quickstart-portal.md) article to create a queue. Note down the **connection string** for your Service Bus namespace and the name of the **queue** you created.
28
27
- Install [Azure SDK for Java][Azure SDK for Java]. If you're using Eclipse, you can install the [Azure Toolkit for Eclipse][Azure Toolkit for Eclipse] that includes the Azure SDK for Java. You can then add the **Microsoft Azure Libraries for Java** to your project. If you're using IntelliJ, see [Install the Azure Toolkit for IntelliJ](/azure/developer/java/toolkit-for-intellij/installation).
In this section, you'll create a Java console project, and add code to send messages to the queue that you created earlier.
37
+
In this section, you create a Java console project, and add code to send messages to the queue that you created earlier.
33
38
34
39
### Create a Java console project
35
40
Create a Java project using Eclipse or a tool of your choice.
36
41
37
42
### Configure your application to use Service Bus
38
43
Add references to Azure Core and Azure Service Bus libraries.
39
44
40
-
If you are using Eclipse and created a Java console application, convert your Java project to a Maven: right-click the project in the **Package Explorer** window, select **Configure** -> **Convert to Maven project**. Then, add dependencies to these two libraries as shown in the following example.
45
+
If you're using Eclipse and created a Java console application, convert your Java project to a Maven: right-click the project in the **Package Explorer** window, select **Configure** -> **Convert to Maven project**. Then, add dependencies to these two libraries as shown in the following example.
> - Replace `NAMESPACENAME` with the name of your Service Bus namespace.
134
+
> - This sample uses `AZURE_PUBLIC_CLOUD` as the authority host. For supported authority hosts, see [`AzureAuthorityHosts`](/dotnet/api/azure.identity.azureauthorityhosts)
135
+
136
+
```java
137
+
static void sendMessage()
138
+
{
139
+
// create a token using the default Azure credential
@@ -103,7 +172,8 @@ If you are using Eclipse and created a Java console application, convert your Ja
103
172
System.out.println("Sent a single message to the queue: "+ queueName);
104
173
}
105
174
```
106
-
1.Add a method named `createMessages` in the classto create a list of messages. Typically, you get these messages from different parts of your application. Here, we create a list of sample messages.
175
+
---
176
+
4.Add a method named `createMessages` in the classto create a list of messages. Typically, you get these messages from different parts of your application. Here, we create a list of sample messages.
107
177
108
178
```java
109
179
static List<ServiceBusMessage> createMessages()
@@ -117,8 +187,69 @@ If you are using Eclipse and created a Java console application, convert your Ja
117
187
return Arrays.asList(messages);
118
188
}
119
189
```
120
-
1.Add a method named `sendMessageBatch` method to send messages to the queue you created. This method creates a `ServiceBusSenderClient` for the queue, invokes the `createMessages` method to get the list of messages, prepares one or more batches, and sends the batches to the queue.
190
+
5.Add a method named `sendMessageBatch` method to send messages to the queue you created. This method creates a `ServiceBusSenderClient` for the queue, invokes the `createMessages` method to get the list of messages, prepares one or more batches, and sends the batches to the queue.
>-Replace `NAMESPACENAME` with the name of your ServiceBus namespace.
196
+
>-This sample uses `AZURE_PUBLIC_CLOUD` as the authority host. For supported authority hosts, see [`AzureAuthorityHosts`](/dotnet/api/azure.identity.azureauthorityhosts)
197
+
121
198
199
+
```java
200
+
staticvoid sendMessageBatch()
201
+
{
202
+
// create a token using the default Azure credential
// We try to add as many messages as a batch can fit based on the maximum size and send to Service Bus when
221
+
// the batch can hold no more messages. Create a new batch for next set of messages and repeat until all
222
+
// messages are sent.
223
+
for (ServiceBusMessage message : listOfMessages) {
224
+
if (messageBatch.tryAddMessage(message)) {
225
+
continue;
226
+
}
227
+
228
+
// The batch is full, so we create a new batch and send the batch.
229
+
senderClient.sendMessages(messageBatch);
230
+
System.out.println("Sent a batch of messages to the queue: "+ queueName);
231
+
232
+
// create a new batch
233
+
messageBatch = senderClient.createMessageBatch();
234
+
235
+
// Add that message that we couldn't before.
236
+
if (!messageBatch.tryAddMessage(message)) {
237
+
System.err.printf("Message is too large for an empty batch. Skipping. Max size: %s.", messageBatch.getMaxSizeInBytes());
238
+
}
239
+
}
240
+
241
+
if (messageBatch.getCount() >0) {
242
+
senderClient.sendMessages(messageBatch);
243
+
System.out.println("Sent a batch of messages to the queue: "+ queueName);
244
+
}
245
+
246
+
//close the client
247
+
senderClient.close();
248
+
}
249
+
```
250
+
251
+
### [ConnectionString](#tab/connection-string)
252
+
122
253
```java
123
254
staticvoid sendMessageBatch()
124
255
{
@@ -166,11 +297,51 @@ If you are using Eclipse and created a Java console application, convert your Ja
166
297
}
167
298
```
168
299
300
+
---
301
+
169
302
## Receive messages from a queue
170
-
Inthis section, you'll add code to retrieve messages from the queue.
303
+
Inthis section, you add code to retrieve messages from the queue.
171
304
172
305
1.Add a method named `receiveMessages` to receive messages from the queue. This method creates a `ServiceBusProcessorClient` for the queue by specifying a handler for processing messages and another one for handling errors. Then, it starts the processor, waits for few seconds, prints the messages that are received, and then stops and closes the processor.
>-Replace `NAMESPACENAME` with the name of your ServiceBus namespace.
311
+
>-Replace `QueueTest` in `QueueTest::processMessage` in the code with the name of your class.
312
+
>-This sample uses `AZURE_PUBLIC_CLOUD` as the authority host. For supported authority hosts, see [`AzureAuthorityHosts`](/dotnet/api/azure.identity.azureauthorityhosts)
1.If you're using Eclipse, right-click the project, select **Export**, expand **Java**, select **Runnable JAR file**, and follow the steps to create a runnable JAR file.
433
+
1. If you are signed into the machine using a user account that's different from the user account added to the **AzureServiceBusDataOwner** role, follow these steps. Otherwise, skip this step and move on to run the Jar file in the next step.
434
+
435
+
1. [InstallAzureCLI](/cli/azure/install-azure-cli-windows) on your machine.
436
+
1.Run the following CLI command to sign in to Azure. Use the same user account that you added to the **AzureServiceBusDataOwner** role.
437
+
438
+
```azurecli
439
+
az login
440
+
```
441
+
1.Run the Jar file using the following command.
442
+
443
+
```java
444
+
java -jar <JARFILENAME>
445
+
```
446
+
1.You see the following output in the console window.
On the **Overview** page for the Service Bus namespace in the Azure portal, you can see **incoming** and **outgoing** message count. You may need to wait for a minute or so and then refresh the page to see the latest values.
0 commit comments