Azure Service Bus Samples client library for Java
Azure Service Bus samples are a set of self-contained Java programs that demonstrate interacting with Azure Service Bus using the client library. Each sample focuses on a specific scenario and can be executed independently.
Key concepts
Key concepts are explained in detail here.
Getting started
Please refer to the Getting Started section.
Obtaining a Service Bus namespace connection string
Most of the samples authorize with Service Bus using a connection string generated for that Service Bus namespace. The connection string value can be obtained by:
- Going to your Service Bus namespace in Azure Portal.
- Go to "Shared access policies" for your Service Bus namespace.
- Click on the "RootManageSharedAccessKey" policy.
- Copying the connection string from the policy's properties.
Examples
Asynchronously sending and receiving
- Send a message
- Send messages using Azure Identity
- Process all messages using processor
- Close a processor with a drain timeout
- Process messages with reactive handlers and manual settlement
- Processor client Java doc code samples
- Receive and auto-complete messages
- Receive and manually settle messages
- Receive messages with auto-lock renewal
- Schedule and cancel a message
- Peek at a message
To run the processor drain-timeout sample, set
AZURE_SERVICEBUS_NAMESPACE_CONNECTION_STRING to a connection string with Send and Listen permissions and
AZURE_SERVICEBUS_SAMPLE_QUEUE_NAME to an empty test queue that is not session-enabled.
Run ServiceBusProcessorDrainTimeoutSample.main: it sends a uniquely identified message, waits for that message's
handler to start, then calls close() from the main thread. The configured 10-second drain timeout lets the handler
finish and settle its message before the receiver closes; if it expires, shutdown proceeds anyway. The sample fails
if its handler does not start within 30 seconds or its message is not settled before close() returns.
The sample uses close() to demonstrate the documented drain and resource-cleanup contract.
Synchronous sending and receiving
Reactive processor
Run ServiceBusProcessorAsyncSample.main with AZURE_SERVICEBUS_FULLY_QUALIFIED_NAMESPACE
(for example, your-namespace.servicebus.windows.net) and AZURE_SERVICEBUS_SAMPLE_QUEUE_NAME.
Use an empty test queue and an identity with Send and Listen permissions. The sample sends a message before starting
the processor. For a session-enabled queue, pass session as the program argument.
The handler returns a Mono covering all downstream work and manual settlement. Replace the illustrative delay with
the asynchronous durable write or flush in your application, and complete the Service Bus message only after it succeeds.
Do not return Mono.empty() while a detached callback is still working: the processor uses the returned operation's
lifetime to bound concurrency, track processing spans, and drain handlers on shutdown.
Automatic settlement is also reactive; when enabled, successful handler completion triggers message completion.
On failure, the processing span includes the error handler and automatic abandonment. Recovery checks run every ten
seconds so persistent receive failures cannot create a tight restart loop.
The session processor's maxConcurrentCalls bounds total concurrent handlers across sessions, not handlers per session.
For ordered work, use sessions and set maxConcurrentCalls to one. Higher values allow handlers for the same session
to overlap. Setting concurrency to one on a non-session queue does not guarantee ordering.
Session idle timeout starts when no accepted handlers or settlements remain pending; it does not interrupt a
long-running handler. Configure lock renewal to cover the full processing time.
stop() pauses new handler dispatch without cancelling accepted work. Subscribe to start() to resume, or call
close() from a controlling thread to drain accepted work and release the receivers.
Message sessions
- Send messages to a session
- Process all session messages using processor
- Receive messages from a specific session
- Receive messages from the first available session
- List session IDs synchronously
- List session IDs asynchronously
Synchronous Administration Client operations
To run the topic filter-count sample, set AZURE_SERVICEBUS_NAMESPACE_CONNECTION_STRING to a namespace connection
string with Manage permission and AZURE_SERVICEBUS_SAMPLE_TOPIC_NAME to an existing topic, then run
TopicFilterCountsSample.main from your IDE. The sample reads runtime properties; it does not create or change rules.
The sample uses service API version 2024-05 to read both filter counts.
Generate Shared Access Signature
Advanced Configuration
Troubleshooting
See Troubleshooting.
Next steps
See Next steps.
Contributing
If you would like to become an active contributor to this project please refer to our Contribution Guidelines for more information.