Azure Service Bus Topics

Detailed documentation on the Azure Service Bus Topics pubsub component

Component format

To setup Azure Service Bus Topics pubsub create a component of type pubsub.azure.servicebus.topics. See this guide on how to create and apply a pubsub configuration.

This component uses topics on Azure Service Bus; see the official documentation for the differences between topics and queues.
For using queues, see the Azure Service Bus Queues pubsub component.

Connection String Authentication

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: servicebus-pubsub
  5. spec:
  6. type: pubsub.azure.servicebus.topics
  7. version: v1
  8. metadata:
  9. # Required when not using Azure AD Authentication
  10. - name: connectionString
  11. value: "Endpoint=sb://{ServiceBusNamespace}.servicebus.windows.net/;SharedAccessKeyName={PolicyName};SharedAccessKey={Key};EntityPath={ServiceBus}"
  12. # - name: consumerID # Optional: defaults to the app's own ID
  13. # value: "{identifier}"
  14. # - name: timeoutInSec # Optional
  15. # value: 60
  16. # - name: handlerTimeoutInSec # Optional
  17. # value: 60
  18. # - name: disableEntityManagement # Optional
  19. # value: "false"
  20. # - name: maxDeliveryCount # Optional
  21. # value: 3
  22. # - name: lockDurationInSec # Optional
  23. # value: 60
  24. # - name: lockRenewalInSec # Optional
  25. # value: 20
  26. # - name: maxActiveMessages # Optional
  27. # value: 10000
  28. # - name: maxConcurrentHandlers # Optional
  29. # value: 10
  30. # - name: defaultMessageTimeToLiveInSec # Optional
  31. # value: 10
  32. # - name: autoDeleteOnIdleInSec # Optional
  33. # value: 3600
  34. # - name: minConnectionRecoveryInSec # Optional
  35. # value: 2
  36. # - name: maxConnectionRecoveryInSec # Optional
  37. # value: 300
  38. # - name: maxRetriableErrorsPerSec # Optional
  39. # value: 10
  40. # - name: publishMaxRetries # Optional
  41. # value: 5
  42. # - name: publishInitialRetryIntervalInMs # Optional
  43. # value: 500

NOTE: The above settings are shared across all topics that use this component.

Warning

The above example uses secrets as plain strings. It is recommended to use a secret store for the secrets as described here.

Spec metadata fields

FieldRequiredDetailsExample
connectionStringYShared access policy connection string for the Service Bus. Required unless using Azure AD authentication.See example above
namespaceNameNParameter to set the address of the Service Bus namespace, as a fully-qualified domain name. Required if using Azure AD authentication.“namespace.servicebus.windows.net”
consumerIDNConsumer ID (a.k.a consumer tag) organizes one or more consumers into a group. Consumers with the same consumer ID work as one virtual consumer, i.e. a message is processed only once by one of the consumers in the group. If the consumer ID is not set, the dapr runtime will set it to the dapr application ID.
timeoutInSecNTimeout for sending messages and for management operations. Default: 6030
handlerTimeoutInSecNTimeout for invoking the app’s handler. Default: 6030
lockRenewalInSecNDefines the frequency at which buffered message locks will be renewed. Default: 20.20
maxActiveMessagesNDefines the maximum number of messages to be processing or in the buffer at once. This should be at least as big as the maximum concurrent handlers. Default: 10002000
maxConcurrentHandlersNDefines the maximum number of concurrent message handlers. Default: 0 (unlimited)10
disableEntityManagementNWhen set to true, queues and subscriptions do not get created automatically. Default: “false”“true”, “false”
defaultMessageTimeToLiveInSecNDefault message time to live, in seconds. Used during subscription creation only.10
autoDeleteOnIdleInSecNTime in seconds to wait before auto deleting idle subscriptions. Used during subscription creation only. Default: 0 (disabled)3600
maxDeliveryCountNDefines the number of attempts the server will make to deliver a message. Used during subscription creation only. Default set by server.10
lockDurationInSecNDefines the length in seconds that a message will be locked for before expiring. Used during subscription creation only. Default set by server.30
minConnectionRecoveryInSecNMinimum interval (in seconds) to wait before attempting to reconnect to Azure Service Bus in case of a connection failure. Default: 25
maxConnectionRecoveryInSecNMaximum interval (in seconds) to wait before attempting to reconnect to Azure Service Bus in case of a connection failure. After each attempt, the component waits a random number of seconds, increasing every time, between the minimum and the maximum. Default: 300 (5 minutes)600
maxRetriableErrorsPerSecNMaximum number of retriable errors that are processed per second. If a message fails to be processed with a retriable error, the component adds a delay before it starts processing another message, to avoid immediately re-processing messages that have failed. Default: 1010
publishMaxRetriesNThe max number of retries for when Azure Service Bus responds with “too busy” in order to throttle messages. Defaults: 55
publishInitialRetryIntervalInMsNTime in milliseconds for the initial exponential backoff when Azure Service Bus throttle messages. Defaults: 500500

Azure Active Directory (AAD) authentication

The Azure Service Bus Topics pubsub component supports authentication using all Azure Active Directory mechanisms, including Managed Identities. For further information and the relevant component metadata fields to provide depending on the choice of AAD authentication mechanism, see the docs for authenticating to Azure.

Example Configuration

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: servicebus-pubsub
  5. spec:
  6. type: pubsub.azure.servicebus.topics
  7. version: v1
  8. metadata:
  9. - name: namespaceName
  10. # Required when using Azure Authentication.
  11. # Must be a fully-qualified domain name
  12. value: "servicebusnamespace.servicebus.windows.net"
  13. - name: azureTenantId
  14. value: "***"
  15. - name: azureClientId
  16. value: "***"
  17. - name: azureClientSecret
  18. value: "***"

Message metadata

Azure Service Bus messages extend the Dapr message format with additional contextual metadata. Some metadata fields are set by Azure Service Bus itself (read-only) and others can be set by the client when publishing a message.

Sending a message with metadata

To set Azure Service Bus metadata when sending a message, set the query parameters on the HTTP request or the gRPC metadata as documented here.

  • metadata.MessageId
  • metadata.CorrelationId
  • metadata.SessionId
  • metadata.Label
  • metadata.ReplyTo
  • metadata.PartitionKey
  • metadata.To
  • metadata.ContentType
  • metadata.ScheduledEnqueueTimeUtc
  • metadata.ReplyToSessionId

Note: The metadata.MessageId property does not set the id property of the cloud event returned by Dapr and should be treated in isolation.

NOTE: If the metadata.SessionId property is not set but the topic requires sessions then an empty session id will be used.

Receiving a message with metadata

When Dapr calls your application, it will attach Azure Service Bus message metadata to the request using either HTTP headers or gRPC metadata. In addition to the settable metadata listed above, you can also access the following read-only message metadata.

  • metadata.DeliveryCount
  • metadata.LockedUntilUtc
  • metadata.LockToken
  • metadata.EnqueuedTimeUtc
  • metadata.SequenceNumber

To find out more details on the purpose of any of these metadata properties, please refer to the official Azure Service Bus documentation.

Note: that all times are populated by the server and are not adjusted for clock skews.

Subscribe to a session enabled topic

To subscribe to a topic that has sessions enabled you can provide the following properties in the subscription metadata.

  • requireSessions (default: false)
  • sessionIdleTimeoutInSec (default: 60)
  • maxConcurrentSessions (default: 8)

Create an Azure Service Bus broker for topics

Follow the instructions here on setting up Azure Service Bus Topics.

Last modified February 15, 2023: Add ASB session docs (#3171) (127aa75d)