Use Azure CLI to create a Service Bus topic and subscriptions to the topic

In this quickstart, you use Azure CLI to create a Service Bus topic and then create subscriptions to that topic.

What are Service Bus topics and subscriptions?

Service Bus topics and subscriptions support a publish/subscribe messaging communication model. When using topics and subscriptions, components of a distributed application do not communicate directly with each other; instead they exchange messages via a topic, which acts as an intermediary.

TopicConcepts

In contrast with Service Bus queues, in which each message is processed by a single consumer, topics and subscriptions provide a one-to-many form of communication, using a publish/subscribe pattern. It is possible to register multiple subscriptions to a topic. When a message is sent to a topic, it is then made available to each subscription to handle/process independently. A subscription to a topic resembles a virtual queue that receives copies of the messages that were sent to the topic. You can optionally register filter rules for a topic on a per-subscription basis, which allows you to filter or restrict which messages to a topic are received by which topic subscriptions.

Service Bus topics and subscriptions enable you to scale to process a large number of messages across a large number of users and applications.

Prerequisites

If you don't have an Azure subscription, you can create a free account before you begin.

In this quickstart, you use Azure Cloud Shell that you can launch after sign into the Azure portal. For details about Azure Cloud Shell, see Overview of Azure Cloud Shell. You can also install and use Azure PowerShell on your machine.

Create a Service Bus topic and subscriptions

Each subscription to a topic can receive a copy of each message. Topics are fully protocol and semantically compatible with Service Bus queues. Service Bus topics support a wide array of selection rules with filter conditions, with optional actions that set or modify message properties. Each time a rule matches, it produces a message. To learn more about rules, filters, and actions, follow this link.

  1. Sign into the Azure portal.

  2. Launch Azure Cloud Shell by selecting the icon shown in the following image. Switch to Bash mode if the Cloud Shell is in PowerShell mode.

    Launch Cloud Shell

  3. Run the following command to create an Azure resource group. Update the resource group name and the location if you want.

    az group create --name MyResourceGroup --location eastus
    
  4. Run the following command to create a Service Bus messaging namespace. Update the name of the namespace to be unique.

    namespaceName=MyNameSpace$RANDOM
    az servicebus namespace create --resource-group MyResourceGroup --name $namespaceName --location eastus
    
  5. Run the following command to create a topic in the namespace.

    az servicebus topic create --resource-group MyResourceGroup   --namespace-name $namespaceName --name MyTopic
    
  6. Create the first subscription to the topic

    az servicebus topic subscription create --resource-group MyResourceGroup --namespace-name $namespaceName --topic-name MyTopic --name S1    
    
  7. Create the second subscription to the topic

    az servicebus topic subscription create --resource-group MyResourceGroup --namespace-name $namespaceName --topic-name MyTopic --name S2    
    
  8. Create the third subscription to the topic

    az servicebus topic subscription create --resource-group MyResourceGroup --namespace-name $namespaceName --topic-name MyTopic --name S3    
    
  9. Create a filter on the first subscription with a filter using custom properties (StoreId is one of Store1, Store2, and Store3).

    az servicebus topic subscription rule create --resource-group MyResourceGroup --namespace-name $namespaceName --topic-name MyTopic --subscription-name S1 --name MyFilter --filter-sql-expression "StoreId IN ('Store1','Store2','Store3')"    
    
  10. Create a filter on the second subscription with a filter using customer properties (StoreId = Store4)

    az servicebus topic subscription rule create --resource-group MyResourceGroup --namespace-name $namespaceName --topic-name myTopic --subscription-name S2 --name MySecondFilter --filter-sql-expression "StoreId = 'Store4'"    
    
  11. Create a filter on the third subscription with a filter using customer properties (StoreId not in Store1, Store2, Store3, or Store4).

    az servicebus topic subscription rule create --resource-group MyResourceGroup --namespace-name $namespaceName --topic-name MyTopic --subscription-name S3 --name MyThirdFilter --filter-sql-expression "StoreId NOT IN ('Store1','Store2','Store3', 'Store4')"     
    
  12. Run the following command to get the primary connection string for the namespace. You use this connection string to connect to the queue and send and receive messages.

    az servicebus namespace authorization-rule keys list --resource-group MyResourceGroup --namespace-name $namespaceName --name RootManageSharedAccessKey --query primaryConnectionString --output tsv    
    

    Note down the connection string and the topic name. You use them to send and receive messages.

Next steps

To learn how to send messages to a topic and receive those messages via a subscription, see the following article: select the programming language in the TOC.