แก้ไข

แชร์ผ่าน


Enable auto forwarding for Azure Service Bus queues and subscriptions

The Service Bus auto forwarding feature enables you to chain a queue or subscription to another queue or topic that is part of the same namespace. When auto forwarding is enabled, Service Bus automatically removes messages that are placed in the first queue or subscription (source) and puts them in the second queue or topic (destination). It's still possible to send a message to the destination entity directly. For more information, See Chaining Service Bus entities with auto forwarding. This article shows you different ways to enable auto forwarding for Service Bus queues and subscriptions.

Important

The basic tier of Service Bus doesn't support the auto forwarding feature. The standard and premium tiers support the feature. For differences between these tiers, see Service Bus pricing.

Using Azure portal

When creating a queue or a subscription for a topic in the Azure portal, select Forward messages to queue/topic as shown in the following examples. Then, specify whether you want messages to be forwarded to a queue or a topic. In this example, the Queue option is selected and a queue from the same namespace is selected.

Create a queue with auto forwarding enabled

Enable auto forward at the time of the queue creation

Create a subscription for a topic with auto forwarding enabled

Enable auto forward at the time of the subscription creation

Update the auto forward setting for an existing queue

On the Overview page for your Service Bus queue, select the current value for the Forward messages to setting. In the following example, the current value is Disabled. In the Forward messages to queue/topic window, you can select the queue or topic where you want the messages to be forwarded.

Enable auto forward for an existing queue

Update the auto forward setting for an existing subscription

On the Overview page for your Service Bus subscription, select the current value for the Forward messages to setting. In the following example, the current value is Disabled. In the Forward messages to queue/topic window, you can select the queue or topic where you want the messages to be forwarded.

Enable auto forward for an existing subscription

Using Azure CLI

To create a queue with auto forwarding enabled, use the az servicebus queue create command with --forward-to set to the name of queue or topic to which you want the messages to be forwarded.

az servicebus queue create \
    --resource-group myresourcegroup \
    --namespace-name mynamespace \
    --name myqueue \
    --forward-to myqueue2

To update the auto forward setting for an existing queue, use the az servicebus queue update command with --forward-to set to the name of the queue or topic to which you want the messages to be forwarded.

az servicebus queue update \
    --resource-group myresourcegroup \
    --namespace-name mynamespace \
    --name myqueue \
    --forward-to myqueue2

To create a subscription to a topic with auto forwarding enabled, use the az servicebus topic subscription create command with --forward-to set to the name of queue or topic to which you want the messages to be forwarded.

az servicebus topic subscription create \
    --resource-group myresourcegroup \
    --namespace-name mynamespace \
    --topic-name mytopic \
    --name mysubscription \
    --forward-to myqueue2

To update the auto forward setting for a subscription to a topic, use the az servicebus topic subscription update command with --forward-to set to the name of queue or topic to which you want the messages to be forwarded.

az servicebus topic subscription update \
    --resource-group myresourcegroup \
    --namespace-name mynamespace \
    --topic-name mytopic \
    --name mysubscription \
    --forward-to myqueue2

Using Azure PowerShell

To create a queue with auto forwarding enabled, use the New-AzServiceBusQueue command with -ForwardTo set to the name of queue or topic to which you want the messages to be forwarded.

New-AzServiceBusQueue -ResourceGroup myresourcegroup `
    -NamespaceName mynamespace `
    -Name myqueue `
    -ForwardTo myqueue2

To update the auto forward setting for an existing queue, use the Set-AzServiceBusQueue command as shown in the following example.

Set-AzServiceBusQueue -ResourceGroup myresourcegroup `
    -NamespaceName mynamespace `
    -Name myqueue `
    -ForwardTo myqueue2

To create a subscription for a topic with auto forwarding enabled, use the New-AzServiceBusSubscription command with -ForwardTo set to the name of queue or topic to which you want the messages to be forwarded.

New-AzServiceBusSubscription -ResourceGroup myresourcegroup `
    -NamespaceName mynamespace `
    -TopicName mytopic `
    -Name mysubscription `
    -ForwardTo myqueue2

To update the auto forward setting for an existing subscription, see the following example.

Set-AzServiceBusSubscription -ResourceGroup myresourcegroup `
    -NamespaceName mynamespace `
    -TopicName mytopic `
    -Name mysub `
    -ForwardTo mytopic2 

Using Azure Resource Manager template

To create a queue with auto forwarding enabled, set forwardTo in the queue properties section to the name of queue or topic to which you want the messages to be forwarded. For more information, see Microsoft.ServiceBus namespaces/queues template reference.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "serviceBusNamespaceName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Service Bus namespace"
      }
    },
    "serviceBusQueueName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Queue"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "resources": [
    {
      "type": "Microsoft.ServiceBus/namespaces",
      "apiVersion": "2018-01-01-preview",
      "name": "[parameters('serviceBusNamespaceName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard"
      },
      "properties": {},
      "resources": [
        {
          "type": "Queues",
          "apiVersion": "2017-04-01",
          "name": "[parameters('serviceBusQueueName')]",
          "dependsOn": [
            "[resourceId('Microsoft.ServiceBus/namespaces', parameters('serviceBusNamespaceName'))]"
          ],
          "properties": {
            "forwardTo": "myqueue2"
          }
        }
      ]
    }
  ]
}

To create a subscription for a topic with auto forwarding enabled, set forwardTo in the queue properties section to the name of queue or topic to which you want the messages to be forwarded. For more information, see Microsoft.ServiceBus namespaces/topics/subscriptions template reference.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "service_BusNamespace_Name": {
      "type": "string",
      "metadata": {
        "description": "Name of the Service Bus namespace"
      }
    },
    "serviceBusTopicName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Topic"
      }
    },
    "serviceBusSubscriptionName": {
      "type": "string",
      "metadata": {
        "description": "Name of the Subscription"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    }
  },
  "resources": [
    {
      "apiVersion": "2018-01-01-preview",
      "name": "[parameters('service_BusNamespace_Name')]",
      "type": "Microsoft.ServiceBus/namespaces",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard"
      },
      "properties": {},
      "resources": [
        {
          "apiVersion": "2017-04-01",
          "name": "[parameters('serviceBusTopicName')]",
          "type": "topics",
          "dependsOn": [
            "[resourceId('Microsoft.ServiceBus/namespaces/', parameters('service_BusNamespace_Name'))]"
          ],
          "properties": {
            "maxSizeInMegabytes": 1024
          },
          "resources": [
            {
              "apiVersion": "2017-04-01",
              "name": "[parameters('serviceBusSubscriptionName')]",
              "type": "Subscriptions",
              "dependsOn": [
                "[parameters('serviceBusTopicName')]"
              ],
              "properties": {
                "forwardTo": "myqueue2"
              }
            }
          ]
        }
      ]
    }
  ]
}

.NET

Azure.Messaging.ServiceBus (latest)

You can enable the auto forwarding feature by setting CreateQueueOptions.ForwardTo or CreateSubscriptionOptions.ForwardTo, and then by using the CreateQueueAsync or CreateSubscriptionAsync methods that take CreateQueueOptions or CreateSubscriptionOptions parameters.

Microsoft.Azure.ServiceBus (legacy)

You can enable autoforwarding by setting the QueueDescription.ForwardTo or SubscriptionDescription.ForwardTo for the source, as in the following example:

SubscriptionDescription srcSubscription = new SubscriptionDescription (srcTopic, srcSubscriptionName);
srcSubscription.ForwardTo = destTopic;
namespaceManager.CreateSubscription(srcSubscription));

On 30 September 2026, we'll retire the Azure Service Bus SDK libraries WindowsAzure.ServiceBus, Microsoft.Azure.ServiceBus, and com.microsoft.azure.servicebus, which don't conform to Azure SDK guidelines. We'll also end support of the SBMP protocol, so you'll no longer be able to use this protocol after 30 September 2026. Migrate to the latest Azure SDK libraries, which offer critical security updates and improved capabilities, before that date.

Although the older libraries can still be used beyond 30 September 2026, they'll no longer receive official support and updates from Microsoft. For more information, see the support retirement announcement.

Java

azure-messaging-servicebus (latest)

You can enable the auto forwarding feature by using the CreateQueueOptions.setForwardTo(String forwardTo) method or the CreateSubscriptionOptions.setForwardTo(String forwardTo), and then by using the createQueue method or the createSubscription method that take CreateQueueOptions or CreateSubscriptionOptions parameters.

azure-servicebus (legacy)

You can enable autoforwarding by using the QueueDescription.setForwardTo(String forwardTo) or SubscriptionDescription.setForwardTo(String forwardTo) for the source.

On 30 September 2026, we'll retire the Azure Service Bus SDK libraries WindowsAzure.ServiceBus, Microsoft.Azure.ServiceBus, and com.microsoft.azure.servicebus, which don't conform to Azure SDK guidelines. We'll also end support of the SBMP protocol, so you'll no longer be able to use this protocol after 30 September 2026. Migrate to the latest Azure SDK libraries, which offer critical security updates and improved capabilities, before that date.

Although the older libraries can still be used beyond 30 September 2026, they'll no longer receive official support and updates from Microsoft. For more information, see the support retirement announcement.

Next steps

Try the samples in the language of your choice to explore Azure Service Bus features.

Find samples for the older .NET and Java client libraries:

On 30 September 2026, we'll retire the Azure Service Bus SDK libraries WindowsAzure.ServiceBus, Microsoft.Azure.ServiceBus, and com.microsoft.azure.servicebus, which don't conform to Azure SDK guidelines. We'll also end support of the SBMP protocol, so you'll no longer be able to use this protocol after 30 September 2026. Migrate to the latest Azure SDK libraries, which offer critical security updates and improved capabilities, before that date.

Although the older libraries can still be used beyond 30 September 2026, they'll no longer receive official support and updates from Microsoft. For more information, see the support retirement announcement.