Skip to content

Commit fd08587

Browse files
authored
Adds javadocs for Service Bus (Azure#36844)
* Add javadocs for admin client. * Add javadocs for builder. * Adding javadocs for async client. * Adding javadocs for sync client. * Adding javadocs for sync client. * Add additional async javadocs. * fix spelling of createqueueoptions * Add samples for Rule actions and filters. * Add more information to key concepts. * Regenerate and fix any broken links from rebase. * Updating models package-info. * Remove additional quotation. * Clean-up package-info samples. * Update sender samples. * Add builder documentation. * Add documentation for ReceiverAsyncClient. * Update receive code snippets and javadoc. * Update receive snippets to settle messages. * Clean up send snippets. * Add notes to package-info * Fix spelling. * Regenerate snippets.
1 parent 9acda3f commit fd08587

28 files changed

+1955
-278
lines changed

sdk/servicebus/azure-messaging-servicebus/README.md

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ ServiceBusReceiverAsyncClient asyncReceiver = new ServiceBusClientBuilder()
121121
.queueName(queueName)
122122
.buildAsyncClient();
123123

124-
// Use the receiver and finally close it.
124+
// When users are done with the receiver, dispose of the receiver.
125+
// Clients should be long-lived objects as they require resources
126+
// and time to establish a connection to the service.
125127
asyncReceiver.close();
126128
```
127129

@@ -165,6 +167,15 @@ The snippet below creates a synchronous [`ServiceBusSenderClient`][ServiceBusSen
165167
queue.
166168

167169
```java com.azure.messaging.servicebus.servicebussenderclient.createMessageBatch
170+
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
171+
172+
// 'fullyQualifiedNamespace' will look similar to "{your-namespace}.servicebus.windows.net"
173+
ServiceBusSenderClient sender = new ServiceBusClientBuilder()
174+
.credential(fullyQualifiedNamespace, credential)
175+
.sender()
176+
.queueName(queueName)
177+
.buildClient();
178+
168179
List<ServiceBusMessage> messages = Arrays.asList(
169180
new ServiceBusMessage("test-1"),
170181
new ServiceBusMessage("test-2"));
@@ -192,7 +203,9 @@ if (batch.getCount() > 0) {
192203
sender.sendMessages(batch);
193204
}
194205

195-
// Finally dispose of the sender.
206+
// Continue using the sender and finally, dispose of the sender.
207+
// Clients should be long-lived objects as they require resources
208+
// and time to establish a connection to the service.
196209
sender.close();
197210
```
198211

@@ -203,6 +216,7 @@ To receive messages, you will need to create a `ServiceBusProcessorClient` with
203216
When receiving message with [PeekLock][peek_lock_mode_docs] mode, it tells the broker that the application logic wants to settle (e.g. complete, abandon) received messages explicitly.
204217

205218
```java com.azure.messaging.servicebus.servicebusprocessorclient#receive-mode-peek-lock-instantiation
219+
// Function that gets called whenever a message is received.
206220
Consumer<ServiceBusReceivedMessageContext> processMessage = context -> {
207221
final ServiceBusReceivedMessage message = context.getMessage();
208222
// Randomly complete or abandon each message. Ideally, in real-world scenarios, if the business logic
@@ -212,23 +226,30 @@ Consumer<ServiceBusReceivedMessageContext> processMessage = context -> {
212226
if (success) {
213227
try {
214228
context.complete();
215-
} catch (Exception completionError) {
216-
System.out.printf("Completion of the message %s failed\n", message.getMessageId());
217-
completionError.printStackTrace();
229+
} catch (RuntimeException error) {
230+
System.out.printf("Completion of the message %s failed.%n Error: %s%n",
231+
message.getMessageId(), error);
218232
}
219233
} else {
220234
try {
221235
context.abandon();
222-
} catch (Exception abandonError) {
223-
System.out.printf("Abandoning of the message %s failed\n", message.getMessageId());
224-
abandonError.printStackTrace();
236+
} catch (RuntimeException error) {
237+
System.out.printf("Abandoning of the message %s failed.%nError: %s%n",
238+
message.getMessageId(), error);
225239
}
226240
}
227241
};
228242

229243
// Sample code that gets called if there's an error
230244
Consumer<ServiceBusErrorContext> processError = errorContext -> {
231-
System.err.println("Error occurred while receiving message: " + errorContext.getException());
245+
if (errorContext.getException() instanceof ServiceBusException) {
246+
ServiceBusException exception = (ServiceBusException) errorContext.getException();
247+
248+
System.out.printf("Error source: %s, reason %s%n", errorContext.getErrorSource(),
249+
exception.getReason());
250+
} else {
251+
System.out.printf("Error occurred: %s%n", errorContext.getException());
252+
}
232253
};
233254

234255
TokenCredential tokenCredential = new DefaultAzureCredentialBuilder().build();
@@ -257,6 +278,7 @@ processorClient.close();
257278
When receiving message with [ReceiveAndDelete][receive_and_delete_mode_docs] mode, tells the broker to consider all messages it sends to the receiving client as settled when sent.
258279

259280
```java com.azure.messaging.servicebus.servicebusprocessorclient#receive-mode-receive-and-delete-instantiation
281+
// Function that gets called whenever a message is received.
260282
Consumer<ServiceBusReceivedMessageContext> processMessage = context -> {
261283
final ServiceBusReceivedMessage message = context.getMessage();
262284
System.out.printf("Processing message. Session: %s, Sequence #: %s. Contents: %s%n",
@@ -265,13 +287,21 @@ Consumer<ServiceBusReceivedMessageContext> processMessage = context -> {
265287

266288
// Sample code that gets called if there's an error
267289
Consumer<ServiceBusErrorContext> processError = errorContext -> {
268-
System.err.println("Error occurred while receiving message: " + errorContext.getException());
290+
if (errorContext.getException() instanceof ServiceBusException) {
291+
ServiceBusException exception = (ServiceBusException) errorContext.getException();
292+
293+
System.out.printf("Error source: %s, reason %s%n", errorContext.getErrorSource(),
294+
exception.getReason());
295+
} else {
296+
System.out.printf("Error occurred: %s%n", errorContext.getException());
297+
}
269298
};
270299

271300
TokenCredential tokenCredential = new DefaultAzureCredentialBuilder().build();
272301

273302
// Create the processor client via the builder and its sub-builder
274303
// 'fullyQualifiedNamespace' will look similar to "{your-namespace}.servicebus.windows.net"
304+
// 'disableAutoComplete()' will opt in to manual settlement (e.g. complete, abandon).
275305
ServiceBusProcessorClient processorClient = new ServiceBusClientBuilder()
276306
.credential(fullyQualifiedNamespace, tokenCredential)
277307
.processor()
@@ -282,7 +312,6 @@ ServiceBusProcessorClient processorClient = new ServiceBusClientBuilder()
282312
.disableAutoComplete()
283313
.buildProcessorClient();
284314

285-
286315
// Starts the processor in the background. Control returns immediately.
287316
processorClient.start();
288317

@@ -364,7 +393,9 @@ ServiceBusReceiverClient receiver = new ServiceBusClientBuilder()
364393
.subQueue(SubQueue.DEAD_LETTER_QUEUE)
365394
.buildClient();
366395

367-
// Use the receiver and finally close it.
396+
// When users are done with the receiver, dispose of the receiver.
397+
// Clients should be long-lived objects as they require resources
398+
// and time to establish a connection to the service.
368399
receiver.close();
369400
```
370401

@@ -375,14 +406,15 @@ between clients which can be achieved by sharing the top level builder as shown
375406
```java com.azure.messaging.servicebus.connection.sharing
376407
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
377408

378-
// Retrieve 'connectionString' and 'queueName' from your configuration.
379409
// 'fullyQualifiedNamespace' will look similar to "{your-namespace}.servicebus.windows.net"
410+
// Any clients created from this builder will share the underlying connection.
380411
ServiceBusClientBuilder sharedConnectionBuilder = new ServiceBusClientBuilder()
381412
.credential(fullyQualifiedNamespace, credential);
382413

383414
// Create receiver and sender which will share the connection.
384415
ServiceBusReceiverClient receiver = sharedConnectionBuilder
385416
.receiver()
417+
.receiveMode(ServiceBusReceiveMode.PEEK_LOCK)
386418
.queueName(queueName)
387419
.buildClient();
388420
ServiceBusSenderClient sender = sharedConnectionBuilder
@@ -395,6 +427,8 @@ try {
395427
sender.sendMessage(new ServiceBusMessage("payload"));
396428
receiver.receiveMessages(1);
397429
} finally {
430+
// Clients should be long-lived objects as they require resources
431+
// and time to establish a connection to the service.
398432
sender.close();
399433
receiver.close();
400434
}

0 commit comments

Comments
 (0)