Azure Service Bus のキューまたはサブスクリプションのメッセージ セッションを有効にする

Azure Service Bus セッションでは、関連メッセージのバインドなしシーケンスの結合および順序指定処理が可能です。 セッションは、先入れ先出し (FIFO) および要求 - 応答のパターンで使用できます。 詳細については、「メッセージ セッション」を参照してください。 この記事では、Service Bus のキューまたはサブスクリプションのセッションを有効にするさまざまな方法について説明します。

重要

  • Service Bus の Basic レベルはセッションをサポートしていません。 Standard レベルと Premium レベルはセッションをサポートしています。 これらのレベルの違いについては、「Service Bus の価格」を参照してください。
  • キューまたはサブスクリプションの作成後に、メッセージ セッションを有効または無効にすることはできません。 それができるのはキューまたはサブスクリプションの作成時のみです。

Azure Portal の使用

Azure portal でキューを作成する場合は、次の図に示すように [セッションの有効化] を選択します。

Enable session at the time of the queue creation

Azure portal でトピックのサブスクリプションを作成する場合は、次の図に示すように [セッションの有効化] を選択します。

Enable session at the time of the subscription creation

Azure CLI の使用

メッセージ セッションを有効にしたキューを作成するには、--enable-sessiontrue に設定して az servicebus queue create コマンドを使用します。

az servicebus queue create \
    --resource-group myresourcegroup \
    --namespace-name mynamespace \
    --name myqueue \
    --enable-session true

メッセージ セッションを有効にしたトピックのサブスクリプションを作成するには、--enable-sessiontrue に設定して az servicebus topic subscription create コマンドを使用します。

az servicebus topic subscription create \
    --resource-group myresourcegroup \
    --namespace-name mynamespace \
    --topic-name mytopic \
    --name mysubscription \
    --enable-session true

Azure PowerShell の使用

メッセージ セッションを有効にしたキューを作成するには、-RequiresSession$True に設定して New-AzServiceBusQueue コマンドを使用します。

New-AzServiceBusQueue -ResourceGroup myresourcegroup `
    -NamespaceName mynamespace `
    -QueueName myqueue `
    -RequiresSession $True

メッセージ セッションを有効にしたトピックのサブスクリプションを作成するには、-RequiresSessiontrue に設定して New-AzServiceBusSubscription コマンドを使用します。

New-AzServiceBusSubscription -ResourceGroup myresourcegroup `
    -NamespaceName mynamespace `
    -TopicName mytopic `
    -SubscriptionName mysubscription `
    -RequiresSession $True

Azure Resource Manager テンプレートの使用

メッセージ セッションを有効にしたキューを作成するには、キューの properties セクションで requiresSessiontrue に設定します。 詳細については、Microsoft.ServiceBus の名前空間/キューのテンプレート リファレンスに関するページを参照してください。

{
  "$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": {
            "requiresSession": true
          }
        }
      ]
    }
  ]
}

メッセージ セッションを有効にしたトピックのサブスクリプションを作成するには、サブスクリプションの properties セクションで requiresSessiontrue に設定します。 詳細については、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"
      }
    },
    "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": {
                "requiresSession": true
              }
            }
          ]
        }
      ]
    }
  ]
}

次のステップ

Azure Service Bus の機能については、使用する言語のサンプルを試してみてください。

以前の .NET および Java クライアント ライブラリのサンプルについては、以下を参照してください。

2026 年 9 月 30 日に、Azure SDK ガイドラインに準拠していない Azure Service Bus SDK ライブラリ WindowsAzure.ServiceBus、Microsoft.Azure.ServiceBus、および com.microsoft.azure.servicebus は廃止されます。 SBMP プロトコルのサポートも終了するため、2026 年 9 月 30 日以降、このプロトコルを使用できなくなります。 この日付より前に、重要なセキュリティ更新プログラムと強化された機能を提供する最新の Azure SDK ライブラリに移行してください。

以前のライブラリは 2026 年 9 月 30 日以降も引き続き使用できますが、Microsoft から公式のサポートと更新プログラムは提供されなくなります。 詳細については、サポート廃止のお知らせに関するページを参照してください。