Wait

wait 策略以并行方式执行其直接子策略,在完成之前会等待其直接子策略全部完成或其中一个完成。 wait 策略可以将下面的一项或多项作为其直接子策略:send-requestcache-lookup-valuechoose 策略。

注意

按照策略声明中提供的顺序设置策略的元素和子元素。 详细了解如何设置或编辑 API 管理策略

策略语句

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

属性

属性 说明 需要 默认
for 确定 wait 策略是等待所有直接子策略完成,还是只等待其中之一完成。 允许值包括:

- all - 等待所有直接子策略完成
- any - 等待任一直接子策略完成。 第一个直接子策略完成后,wait 策略即告完成,同时会终止执行任何其他直接子策略。

允许使用策略表达式。
all

元素

可能只包含 send-requestcache-lookup-valuechoose 策略作为子元素。

使用情况

示例

在以下示例中有两个 choose 策略,充当 wait 策略的直接子策略。 这些 choose 策略都可以并行执行。 每个 choose 策略都会尝试检索缓存的值。 如果缓存未命中,则会调用后端服务来提供值。 在此示例中,wait 策略在完成之前必须等待其所有直接子策略完成,因为 for 属性设置为 all。 在此示例中,上下文变量(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>

后续步骤

有关使用策略的详细信息,请参阅: