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
- 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
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.