等候

適用於:所有 API 管理 層

wait 原則會以平行方式執行其直屬子原則,並等候其所有或其中一個直屬子原則完成後再完成。 wait 原則可以將下列一或多個原則當作直屬子原則:send-requestcache-lookup-valuechoose 原則。

注意

請依照原則陳述式中提供的順序,來設定原則的元素和子元素。 深入了解如何設定或編輯 APIM 原則

原則陳述式

<wait for="all | any">
  <!--Wait policy can contain send-request, cache-lookup-value,
        and choose policies as child elements -->
</wait>

屬性

屬性 描述 是必要欄位 預設
決定 wait 原則是要等候所有直屬子原則完成或只等候一個完成。 允許的值如下:

- all - 等候所有直屬子原則完成
- any - 等候任一個直屬子原則完成。 第一個直屬子原則完成後,wait 原則便會完成,並終止執行任何其他直屬子原則。

允許使用原則運算式。
No all

元素

只能包含做為子元素 send-requestcache-lookup-valuechoose 原則。

使用方式

範例

在下列範例中,有兩個 choose 原則做為 wait 原則的直屬子原則。 在這些 choose 原則中,每一個都會以平行方式執行。 每個 choose 原則都會嘗試擷取快取的值。 如果有快取遺漏,便會呼叫後端服務以提供值。 在此範例中,因為 for 屬性設為 all,所以要等到其所有直屬子原則完成後,wait 原則才會完成。 在此範例中,內容變數 (execute-branch-onevalue-oneexecute-branch-twovalue-two) 會在此範例原則的範圍之外進行宣告。

<wait for="all">
  <choose>
    <when condition="@((bool)context.Variables["execute-branch-one="])">
      <cache-lookup-value key="key-one" variable-name="value-one" />
      <choose>
        <when condition="@(!context.Variables.ContainsKey("value-one="))">
          <send-request mode="new" response-variable-name="value-one">
            <set-url>https://backend-one</set-url>
            <set-method>GET</set-method>
          </send-request>
        </when>
      </choose>
    </when>
  </choose>
  <choose>
    <when condition="@((bool)context.Variables["execute-branch-two="])">
      <cache-lookup-value key="key-two" variable-name="value-two" />
      <choose>
        <when condition="@(!context.Variables.ContainsKey("value-two="))">
          <send-request mode="new" response-variable-name="value-two">
            <set-url>https://backend-two</set-url>
            <set-method>GET</set-method>
          </send-request>
        </when>
      </choose>
    </when>
  </choose>
</wait>

如需使用原則的詳細資訊,請參閱: