Edit

Quickstart: Create a Service Bus topic and subscriptions using Azure PowerShell

In this quickstart, you use Azure PowerShell to create a Service Bus topic and then create subscriptions to that topic. You also create filters for each subscription to route messages based on custom properties.

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 don't communicate directly with each other; instead they exchange messages via a topic, which acts as an intermediary.

Diagram that shows publishers sending messages to a Service Bus topic, which distributes copies to multiple subscriber queues.

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's possible to register multiple subscriptions to a topic. When a message is sent to a topic, it's 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

  • An Azure account with an active subscription. Create an account for free.
  • Azure Cloud Shell or Azure PowerShell installed locally. This quickstart uses Azure Cloud Shell.

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.

  1. Sign in to the Azure portal.

  2. Launch Azure Cloud Shell by selecting the icon shown in the following image.

    Button to launch the Azure Cloud Shell.

  3. In the bottom Cloud Shell window, switch from Bash to PowerShell.

    Screenshot of the Cloud Shell toolbar showing how to switch from Bash to PowerShell mode.

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

    New-AzResourceGroup -Name MyResourceGroup -Location eastus
    
  5. Run the following command to create a Service Bus messaging namespace. Update the name of the namespace to be unique.

    $namespaceName = "MyNameSpace$(Get-Random)"
    New-AzServiceBusNamespace -ResourceGroupName MyResourceGroup -Name $namespaceName -Location eastus
    
  6. Run the following command to create a topic in the namespace.

    New-AzServiceBusTopic -ResourceGroupName MyResourceGroup -NamespaceName $namespaceName -Name MyTopic
    
  7. Create the first subscription to the topic.

    New-AzServiceBusSubscription -ResourceGroupName MyResourceGroup -NamespaceName $namespaceName -TopicName MyTopic -Name S1
    
  8. Create the second subscription to the topic.

    New-AzServiceBusSubscription -ResourceGroupName MyResourceGroup -NamespaceName $namespaceName -TopicName MyTopic -Name S2
    
  9. Create the third subscription to the topic.

    New-AzServiceBusSubscription -ResourceGroupName MyResourceGroup -NamespaceName $namespaceName -TopicName MyTopic -Name S3
    
  10. Create a filter on the first subscription with a filter using custom properties (StoreId is one of Store1, Store2, and Store3).

    New-AzServiceBusRule -ResourceGroupName MyResourceGroup -NamespaceName $namespaceName -TopicName MyTopic -SubscriptionName S1 -Name MyFilter -SqlExpression "StoreId IN ('Store1','Store2','Store3')"
    
  11. Create a filter on the second subscription with a filter using customer properties (StoreId = Store4).

    New-AzServiceBusRule -ResourceGroupName MyResourceGroup -NamespaceName $namespaceName -TopicName MyTopic -SubscriptionName S2 -Name MySecondFilter -SqlExpression "StoreId = 'Store4'"
    
  12. Create a filter on the third subscription with a filter using customer properties (StoreId not in Store1, Store2, Store3, or Store4).

    New-AzServiceBusRule -ResourceGroupName MyResourceGroup -NamespaceName $namespaceName -TopicName MyTopic -SubscriptionName S3 -Name MyThirdFilter -SqlExpression "StoreId NOT IN ('Store1','Store2','Store3','Store4')"
    
  13. Run the following command to get the primary connection string for the namespace. You use this connection string to connect to the topic and send and receive messages.

    Get-AzServiceBusKey -ResourceGroupName MyResourceGroup -Namespace $namespaceName -Name RootManageSharedAccessKey
    

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

Clean up resources

When you no longer need the Service Bus namespace, topic, and subscriptions, delete them to avoid incurring charges. Run the following command to delete the resource group and all its resources:

Remove-AzResourceGroup -Name MyResourceGroup

Next steps

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