To send and receive Azure Service Bus messages in Spring Boot, add the azure-messaging-servicebus client (or the Spring Cloud Azure Service Bus starter), build a ServiceBusSenderClient to publish and a ServiceBusProcessorClient to consume, and settle every message with complete or abandon. Authenticate with Microsoft Entra ID (managed identity) in production and use a connection string only for local development.
The examples below target Spring Boot 3 and Java 17 or later, using a queue named orders. Topics and subscriptions work the same way with a different builder setup.
Create a Service Bus namespace and a queue in Azure, then add the SDK:
Manage the versions through the Azure SDK BOM (com.azure:azure-sdk-bom) or the Spring Cloud Azure BOM. The Spring Cloud Azure starter spring-cloud-azure-starter-servicebus adds property-based configuration and Spring Boot auto-configuration on top of the same client, if you prefer that style.
DefaultAzureCredential uses your az login session locally and a managed identity when deployed to Azure. Assign the identity the role Azure Service Bus Data Sender (and Data Receiver for consumers) on the queue, so no secret is stored in the application.
Setting messageId lets you enable duplicate detection on the queue. For many messages, use sender.createMessageBatch() and tryAddMessage so you send them in one network call.
ServiceBusProcessorClient pulls messages on background threads and calls your handler for each one. Start it when the application is ready and close it on shutdown.
By default the processor uses peek-lock mode with auto-complete: when the handler returns without an exception the message is completed, and when it throws the message is abandoned. Calling complete or abandon yourself, as above, makes the intent explicit. The processError callback is mandatory and receives transport and handler errors, not only failures of your code.
| Action | Effect |
|---|---|
complete() | Removes the message from the queue |
abandon() | Releases the lock; delivery count increases |
deadLetter() | Moves the message to the dead-letter sub-queue with a reason |
defer() | Keeps the message in the queue for retrieval by sequence number |
When the delivery count reaches the queue's MaxDeliveryCount (10 by default), Service Bus moves the message to the dead-letter queue automatically. Call context.deadLetter() yourself for messages that can never succeed, such as invalid JSON. Process each message idempotently, since delivery is at least once.
maxAutoLockRenewDuration.ServiceBusClientBuilder-derived client per queue and reuse it, rather than building senders per request.Both use the same underlying client. The starter adds property-based configuration and Spring integration such as ServiceBusTemplate. The plain SDK, as shown here, gives the most explicit control.
A queue delivers each message to one consumer. A topic copies each message to every subscription, and each subscription behaves like its own queue.
Your handler throws or exceeds the lock duration, so the message is abandoned or the lock expires and delivery repeats until MaxDeliveryCount sends it to the dead-letter queue.
Use sessions with a session ID on messages, and a session-enabled processor, to get first-in-first-out handling per session.
Build a ServiceBusSenderClient and a ServiceBusProcessorClient from one ServiceBusClientBuilder, authenticate with DefaultAzureCredential, and settle each message explicitly. Rely on MaxDeliveryCount and the dead-letter queue for poison messages, and keep handlers idempotent.