針對 Azure 服務匯流排佇列或主題啟用重複訊息偵測

當您針對佇列或主題啟用重複偵測時,Azure 服務匯流排會保留一個歷程,其中記錄傳送至佇列或主題的所有訊息,其保留時間為設定的時間長度。 在該間隔期間,您的佇列或主題不會儲存任何重複的訊息。 啟用此屬性可保證在使用者定義的時間範圍只傳遞一次。 如需詳細資訊,請參閱重複偵測。 本文說明為服務匯流排佇列或主題啟用重複訊息偵測的不同方式。

注意

  • 基本層的服務匯流排不支援重複偵測。 標準和進階層支援重複偵測。 如需這些階層之間的差異,請參閱服務匯流排定價
  • 您無法在建立佇列或主題之後啟用或停用重複偵測。 您只能在建立佇列或主題時這麼做。

使用 Azure 入口網站

在 Azure 入口網站中建立佇列時,請選取 [啟用重複偵測],如下圖所示。 您可以在建立佇列或主題時設定重複偵測時段的大小。

Enable duplicate detection at the time of the queue creation

在 Azure 入口網站中建立主題時,請選取 [啟用重複偵測],如下圖所示。

Enable duplicate detection at the time of the topic creation

如果已在建立時啟用重複偵測,您也可以針對現有的佇列或主題設定此設定。

更新現有佇列或主題的重複偵測時段大小

若要變更現有佇列或主題的重複偵測時段大小,請在 [概觀] 頁面上,針對 [重複偵測時段] 選取 [變更]

佇列

Set duplicate detection window size for a queue

主題

Set duplicate detection window size for a topic

使用 Azure CLI

若要建立已啟用重複偵測的佇列,請使用 az servicebus queue create 命令,並將 --enable-duplicate-detection 設定為 true

az servicebus queue create \
    --resource-group myresourcegroup \
    --namespace-name mynamespace \
    --name myqueue \
    --enable-duplicate-detection true \
    --duplicate-detection-history-time-window P1D

若要建立已啟用重複偵測的主題,請使用 az servicebus topic create 命令,並將 --enable-duplicate-detection 設定為 true

az servicebus topic create \
    --resource-group myresourcegroup \
    --namespace-name mynamespace \
    --name mytopic \
    --enable-duplicate-detection true \
    --duplicate-detection-history-time-window P1D

上述範例也會使用 --duplicate-detection-history-time-window 參數來設定重複偵測時段的大小。 時段大小會設定為一天。 預設值為 10 分鐘,允許的最大值為七天。

若要使用新的偵測時段大小更新佇列,請使用 az servicebus queue update 命令搭配 --duplicate-detection-history-time-window 參數。 在此範例中,時段大小會更新為七天。

az servicebus queue update \
    --resource-group myresourcegroup \
    --namespace-name mynamespace \
    --name myqueue \
    --duplicate-detection-history-time-window P7D

同樣地,若要使用新的偵測時段大小更新主題,請使用 az servicebus topic update 命令搭配 --duplicate-detection-history-time-window 參數。 在此範例中,時段大小會更新為七天。

az servicebus topic update \
    --resource-group myresourcegroup \
    --namespace-name mynamespace \
    --name myqueue \
    --duplicate-detection-history-time-window P7D

使用 Azure PowerShell

若要建立已啟用重複偵測的佇列,請使用 New-AzServiceBusQueue 命令,並將 -RequiresDuplicateDetection 設定為 $True

New-AzServiceBusQueue -ResourceGroup myresourcegroup `
    -NamespaceName mynamespace `
    -QueueName myqueue `
    -RequiresDuplicateDetection $True `
    -DuplicateDetectionHistoryTimeWindow P1D

若要建立已啟用重複偵測的主題,請使用 New-AzServiceBusTopic 命令,並將 -RequiresDuplicateDetection 設定為 true

New-AzServiceBusTopic -ResourceGroup myresourcegroup `
    -NamespaceName mynamespace `
    -Name mytopic `
    -RequiresDuplicateDetection $True
    -DuplicateDetectionHistoryTimeWindow P1D

上述範例也會使用 -DuplicateDetectionHistoryTimeWindow 參數來設定重複偵測時段的大小。 時段大小會設定為一天。 預設值為 10 分鐘,允許的最大值為七天。

若要使用新的偵測時段大小更新佇列,請參閱下列範例。 在此範例中,時段大小會更新為七天。

$queue=Get-AzServiceBusQueue -ResourceGroup myresourcegroup `
    -NamespaceName mynamespace `
    -QueueName myqueue 

$queue.DuplicateDetectionHistoryTimeWindow='P7D'

Set-AzServiceBusQueue -ResourceGroup myresourcegroup `
    -NamespaceName mynamespace `
    -QueueName myqueue `
    -QueueObj $queue

若要使用新的偵測時段大小更新主題,請參閱下列範例。 在此範例中,時段大小會更新為七天。

$topic=Get-AzServiceBusTopic -ResourceGroup myresourcegroup `
    -NamespaceName mynamespace `
    -Name mytopic

$topic.DuplicateDetectionHistoryTimeWindow='P7D'

Set-AzServiceBusTopic -ResourceGroup myresourcegroup `
    -NamespaceName mynamespace `
    -Name mytopic `
    -TopicObj $topic

使用 Azure Resource Manager 範本

若要建立已啟用重複偵測的佇列,請在佇列屬性區段中將 requiresDuplicateDetection 設定為 true。 如需詳細資訊,請參閱 Microsoft.ServiceBus 命名空間/佇列範本參考。 指定 duplicateDetectionHistoryTimeWindow 屬性的值,以設定重複偵測時段的大小。 在下列範例中,其會設定為一天。

{
  "$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": {
            "requiresDuplicateDetection": true,
            "duplicateDetectionHistoryTimeWindow": "P1D"
          }
        }
      ]
    }
  ]
}

若要建立已啟用重複偵測的主題,請在主題屬性區段中將 requiresDuplicateDetection 設定為 true。 如需詳細資訊,請參閱 Microsoft.ServiceBus 命名空間/主題範本參考

{
  "$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"
      }
    },
    "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": {
            "requiresDuplicateDetection": true,
            "duplicateDetectionHistoryTimeWindow": "P1D"
          }
        }
      ]
    }
  ]
}

下一步

請以您所選擇的語言嘗試各式範例,以探索 Azure 服務匯流排的功能。

請在下方尋找舊版 .NET 和 JAVA 用戶端程式庫的範例:

在 2026 年 9 月 30 日,我們將淘汰不符合 Azure SDK 準則的 Azure 服務匯流排 SDK 程式庫 WindowsAzure.ServiceBus、Microsoft.Azure.ServiceBus 和 com.microsoft.azure.servicebus。 我們也將結束 SBMP 通訊協定的支援,因此您將無法在 2026 年 9 月 30 日之後再使用此通訊協定。 請在該日期之前移轉至最新的 Azure SDK 程式庫,該程式庫提供重要的安全性更新和改進的功能。

雖然較舊的程式庫仍可在 2026 年 9 月 30 日之後使用,但這些程式庫將無法繼續收到 Microsoft 的官方支援和更新。 如需詳細資訊,請參閱支援淘汰公告