サービス バス メッセージを送信する

適用対象: 開発者 |基本 |標準 |プレミアム

send-service-bus-message ポリシーは、Azure Service Bus キューまたはトピックにメッセージを送信します。 オプションでAPIリクエストをバックエンドサービスに転送することも可能です。

ポリシーの要素と子要素を、ポリシー ステートメントで指定された順序で設定します。 API Management ポリシーを設定または編集する方法について説明します

ポリシー ステートメント

<send-service-bus-message 
    queue-name="service bus queue"
    topic-name="service bus topic"
    namespace="FQDN of service bus namespace"
    client-id="ID of user-assigned managed identity"
    message-id="message ID"
    session-id="session ID"
    time-to-live="message time to live"
    response-variable-name="context variable name"
    ignore-error="false">
        <message-properties>
                <message-property name="property-name">property-value</message-property>
                <!-- if there are multiple properties, then add additional message-property elements -->
        </message-properties>
        <payload>"message content"</payload>
</send-service-bus-message>

Attributes

特性 Description 必須 既定値
queue-name メッセージの送信先となるサービス バス キューの名前を指定します。 ポリシー式と名前付き値を使用できます。 queue-nameまたはtopic-nameを指定する必要があります。 いいえ N/A
topic-name メッセージの送信先となる Service Bus トピックの名前を指定します。 ポリシー式と名前付き値を使用できます。 queue-nameまたはtopic-nameを指定する必要があります。 いいえ N/A
namespace Service Bus 名前空間の完全修飾ドメイン名を指定します。 ポリシー式と名前付き値を使用できます。 いいえ N/A
client-id Service Bus で認証するユーザー割り当てマネージド ID のクライアント ID を指定します。 ID には、Azure Service Bus データ送信者ロールが割り当てられている必要があります。 ポリシー式と名前付き値を使用できます。 この属性を指定しない場合は、システムに割り当てられたアイデンティティが使われます。 いいえ N/A
message-id メッセージ識別子。 有効な GUID である必要があります。 ポリシー式を使用できます。 省略すると、API ManagementはGUIDを生成します。 いいえ 生成GUID
session-id 関連メッセージをグループ化するために使われるService Busセッション識別子。 有効な GUID である必要があります。 ポリシー式を使用できます。 いいえ N/A
time-to-live メッセージが処理可能で期限が切れるまでの期間。 例えばTimeSpanの値を 00:10:00 いいえ N/A
response-variable-name Service Bus送信操作に関する情報を受け取るコンテキスト変数の名前。 いいえ N/A
ignore-error Service Busの送信失敗がポリシー実行を継続できるかどうか。 true = 続ける; false = 通常のエラー処理を呼び出します。 いいえ false

response-variable-nameを指定すると、API Managementは送信Service Bus操作の情報をそのコンテキスト変数に保存します。 成功すると、 MessageIdSessionIdTimeToLiveが含まれています。 無視された送信失敗では、 Error.ReasonError.Messageが含まれます。

Elements

要素 Description 必須
payload サービス バスに送信するメッセージ ペイロードを指定します。 ポリシー式と名前付き値を使用できます。 イエス
message-properties メッセージ ペイロードと共に渡すメタデータを指定する message-property サブ要素のコレクション。 各 message-property は、名前と値のペアで構成されます。 ポリシー式と名前付き値を使用できます。 いいえ

Usage

使用に関する注記

  • メッセージを受信する Azure Service Bus キューまたはトピックを事前に作成する必要があります。
  • このポリシーは、ポリシー定義ごとに複数回使用できます。
  • message-idを省略すると、API ManagementはGUIDを生成します。
  • 設定された message-id 値と session-id 値は有効なGUIDでなければなりません。
  • Service Bus団体がセッションを必要とする場合に使うsession-id
  • time-to-live 00:10:00のようなTimeSpan値を用います。
  • ignore-error 送信失敗に適用されます。 無効なGUIDやTTLなどの無効なメッセージ設定でもポリシー失敗の原因となります。

例示

Service Bus キューにメッセージを送信する

この例では、リクエスト本体からなるメッセージがオーダーキューに送られます。 リクエストIDはメッセージIDとなり、メッセージは10分後に終了し、送信情報は serviceBusResultに保存されます。 送信失敗はAPI管理のエラー処理を呼び起こします。 API Management インスタンスは、アクセスにユーザー割り当て ID を使用します。 その後、要求はバックエンド サービスに転送されます。

<policies>
    <inbound>
        <send-service-bus-message 
          queue-name="orders"
          namespace="contoso-messaging.servicebus.windows.net"
          message-id="@(context.RequestId.ToString())"
          time-to-live="00:10:00"
          response-variable-name="serviceBusResult"
          ignore-error="false">
          <payload>
            @(context.Request.Body.As<string>(preserveContent: true))
          </payload>
        </send-service-bus-message>
    </inbound>
    <backend>
        <forward-request timeout="60"/>
    </backend>
</policies>

Service Bus トピックにメッセージを送信する

この例では、リクエスト本体を含むメッセージをサービスバストピックに送信します。 API Management インスタンスは、アクセスにシステム割り当て ID を使用します。 その後、そのリクエストをバックエンドサービスに転送します。

<policies>
    <inbound>
        <send-service-bus-message topic-name="orders" namespace="my-service-bus.servicebus.windows.net">
           <payload>@(context.Request.Body.As<string>(preserveContent: true))</payload>
        </send-service-bus-message>
    </inbound>
    <backend>
        <forward-request timeout="60"/>
    </backend>
</policies>

メッセージとメタデータを送信する

この例では、リクエスト本体を含むメッセージをサービスバストピックに送信し、ペイロードとともにメタデータを送信するメッセージプロパティを設定します。 API Management インスタンスは、アクセスにシステム割り当て ID を使用します。 その後、そのリクエストをバックエンドサービスに転送します。

<policies>
    <inbound>
        <send-service-bus-message topic-name="orders" namespace="my-service-bus.servicebus.windows.net">
           <message-properties>
              <message-property name="Customer">Contoso</message-property>
           </message-properties>
           <payload>@(context.Request.Body.As<string>(preserveContent: true))</payload>
        </send-service-bus-message>
    </inbound>
    <backend>
        <forward-request timeout="60"/>
    </backend>
</policies>

メッセージを送信してすぐに返す

この例では、リクエスト本体を含むメッセージをサービスバストピックに送信します。 API Management インスタンスは、アクセスにシステム割り当て ID を使用します。 その後、 201 応答ステータスコードを即座に発信者に返します。

<policies>
    <inbound>
        <send-service-bus-message topic-name="orders" namespace="my-service-bus.servicebus.windows.net">
           <payload>@(context.Request.Body.As<string>(preserveContent: true))</payload>
        </send-service-bus-message>
        <return-response>
            <set-status code="201" reason="Created!" />
        </return-response>
    </inbound>
</policies>

ポリシーに対する処理の詳細については、次のトピックを参照してください。