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 trial subscription before you begin. You can install and use Azure CLI on your machine.

Note

Before you can use Azure CLI in Microsoft Azure operated by 21Vianet, please run az cloud set -n AzureChinaCloud first to change the cloud environment. If you want to switch back to Azure Public Cloud, run az cloud set -n AzureCloud again.

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. Run the following command to sign in to Azure China.

    az cloud set -n AzureChinaCloud
    az login
    
  2. 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 chinaeast
    
  3. 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 chinaeast
    
  4. Run the following command to create a topic in the namespace.

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

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

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

    az servicebus topic subscription create --resource-group MyResourceGroup --namespace-name $namespaceName --topic-name MyTopic --name S3    
    
  8. 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')"    
    
  9. 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'"    
    
  10. 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')"     
    
  11. 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.