3

我们正在尝试一个场景,我们在一个有效负载中发送请求列表,根据某些 xpath 将该有效负载拆分为单个请求,进行单个 http 出站调用并将它们聚合在后面。整个过程运行良好,但我们尝试使用从拆分消息有效负载中提取的值添加调用属性,并且它始终显示所有拆分消息的值。这是我拥有的骡流配置:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:https="http://www.mulesoft.org/schema/mule/https" xmlns:vm="http://www.mulesoft.org/schema/mule/vm" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.5.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
http://www.mulesoft.org/schema/mule/https http://www.mulesoft.org/schema/mule/https/current/mule-https.xsd">
    <!-- HTTPS Connection Configuration With CERTS -->
    <https:connector name="HTTP_HTTPS" cookieSpec="netscape" validateConnections="true" sendBufferSize="0" receiveBufferSize="0" receiveBacklog="0" clientSoTimeout="10000" serverSoTimeout="10000" socketSoLinger="0" doc:name="HTTP-HTTPS">
        <https:tls-server  path="/Users/rkesara/opt/MULE/truststore/cacerts" storePassword="changeit" />
    </https:connector>

    <!-- List of transformers that might be useful for this flow -->
    <mulexml:dom-to-xml-transformer outputEncoding="UTF-8" name="DOM_to_XML" doc:name="DOM to XML"/>

    <!-- Root flow that can trigger pull and push functionality -->
    <flow name="PushInventoryFlow" doc:name="PushInventoryFlow">
        <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/>
        <logger message="PushInventoryFlow - Listing requests payload : #[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>

        <logger message="PullAndPushInventoryFlow - Splitting listing requests..." level="INFO" doc:name="Logger"/>
        <splitter enableCorrelation="ALWAYS" expression="#[xpath('//listings/listing')]" doc:name="ListingRequestSplitter"/>
        <logger message="PullAndPushInventoryFlow - Number of listing requests found after bunlding : #[header:MULE_CORRELATION_GROUP_SIZE] - current request number : #[header:MULE_CORRELATION_SEQUENCE]" level="INFO" doc:name="Logger"/>

        <flow-ref name="ListingMessageProcessorFlow" />

        <collection-aggregator />        
        <logger message="Aggregated Payload-&gt; #[payload]" level="INFO" doc:name="Logger"/>
   </flow>

   <flow name="ListingMessageProcessorFlow" doc:name="ListingMessageProcessorFlow">

        <message-properties-transformer scope="invocation" doc:name="Remember correlation">
            <add-message-property value="#[header:OUTBOUND:MULE_CORRELATION_ID]" key="cid" />
            <add-message-property value="#[header:OUTBOUND:MULE_CORRELATION_GROUP_SIZE]" key="cgs" />

            <add-message-property value="#[xpath://listing/section]" key="section" />
        </message-properties-transformer>

        <logger message="ListingMessageProcessorFlow - Invoking listingAPI for Section: #[header:INVOCATION:section] with request payload : #[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>
        <https:outbound-endpoint exchange-pattern="request-response" host="api-dev.srwd10.com" port="443" path="inventory/listings/v1/" method="POST" doc:name="HTTP" connector-ref="HTTP_HTTPS" transformer-refs="DOM_to_XML" contentType="application/xml" mimeType="application/xml">
            <message-properties-transformer scope="outbound">
                <add-message-property key="Authorization" value="Bearer blahblahblah" />
                <add-message-property key="TARGET_HOST" value="srwq12" />
                <add-message-property key="Acccept" value="application/xml" />
            </message-properties-transformer>
        </https:outbound-endpoint>
        <logger message="ListingMessageProcessorFlow - Listing API response : #[message.payloadAs(java.lang.String)]" level="INFO" doc:name="Logger"/>

        <message-properties-transformer scope="outbound">
            <add-message-property value="#[header:INVOCATION:cid]" key="MULE_CORRELATION_ID" />
            <add-message-property value="#[header:INVOCATION:cgs]" key="MULE_CORRELATION_GROUP_SIZE" />
        </message-properties-transformer>

    </flow>

</mule>

在请求 xml 之后:

<listings>
   <listing>
      <eventId>4457573</eventId>
      <eventDescription>The Book of Mormon Houston</eventDescription>
      <pricePerTicket>
         <amount>900</amount>
         <currency>USD</currency>
      </pricePerTicket>
      <quantity>1</quantity>
      <section>CENTER ORCHESTRA</section>
      <rows>M</rows>
      <seats>2</seats>
      <splitOption>NONE</splitOption>
   </listing>
   <listing>
      <eventId>4457573</eventId>
      <eventDescription>The Book of Mormon Houston</eventDescription>
      <pricePerTicket>
         <amount>902</amount>
         <currency>USD</currency>
      </pricePerTicket>
      <quantity>1</quantity>
      <section>CENTER ORCHESTRA</section>
      <rows>M</rows>
      <seats>1</seats>
      <splitOption>NONE</splitOption>
   </listing>
   <listing>
      <eventId>4457573</eventId>
      <eventDescription>The Book of Mormon Houston</eventDescription>
      <pricePerTicket>
         <amount>905</amount>
         <currency>USD</currency>
      </pricePerTicket>
      <quantity>2</quantity>
      <section>CENTER ORCHESTRA</section>
      <rows>M</rows>
      <seats>4,6</seats>
      <splitOption>NONE</splitOption>
   </listing>
   <listing>
      <eventId>4457573</eventId>
      <eventDescription>The Book of Mormon Houston</eventDescription>
      <pricePerTicket>
         <amount>906</amount>
         <currency>USD</currency>
      </pricePerTicket>
      <quantity>1</quantity>
      <section>CENTER ORCHESTRA</section>
      <rows>M</rows>
      <seats>5</seats>
      <splitOption>NONE</splitOption>
   </listing>
   <listing>
      <eventId>4457573</eventId>
      <eventDescription>The Book of Mormon Houston</eventDescription>
      <pricePerTicket>
         <amount>907</amount>
         <currency>USD</currency>
      </pricePerTicket>
      <quantity>2</quantity>
      <section>CENTER ORCHESTRA</section>
      <rows>M</rows>
      <seats>7,9</seats>
      <splitOption>NONE</splitOption>
   </listing>
   <listing>
      <eventId>4457573</eventId>
      <eventDescription>The Book of Mormon Houston</eventDescription>
      <pricePerTicket>
         <amount>908</amount>
         <currency>USD</currency>
      </pricePerTicket>
      <quantity>2</quantity>
      <section>CENTER ORCHESTRA</section>
      <rows>M</rows>
      <seats>3,8</seats>
      <splitOption>NONE</splitOption>
   </listing>
   <listing>
      <eventId>4457573</eventId>
      <eventDescription>The Book of Mormon Houston</eventDescription>
      <pricePerTicket>
         <amount>909</amount>
         <currency>USD</currency>
      </pricePerTicket>
      <quantity>1</quantity>
      <section>CENTER ORCHESTRA</section>
      <rows>M</rows>
      <seats>10</seats>
      <splitOption>NONE</splitOption>
   </listing>
</listings>

这是我们在日志中看到的行为:

INFO  2014-06-25 11:29:07,860 [[telecharge].connector.http.mule.default.receiver.04] org.mule.api.processor.LoggerMessageProcessor: PushInventoryFlow - Number of listing requests found after bunlding : 7 - current request number : 1
INFO  2014-06-25 11:29:07,862 [[telecharge].connector.http.mule.default.receiver.04] org.mule.api.processor.LoggerMessageProcessor: ListingMessageProcessorFlow - Invoking listingAPI for Section: [CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA] with request payload : <listing><eventId>4457573</eventId><eventDescription>The Book of Mormon Houston</eventDescription><pricePerTicket><amount>900</amount><currency>USD</currency></pricePerTicket><quantity>1</quantity><section>CENTER ORCHESTRA</section><rows>M</rows><seats>2</seats><splitOption>NONE</splitOption></listing>
INFO  2014-06-25 11:29:12,200 [[telecharge].connector.http.mule.default.receiver.04] org.mule.api.processor.LoggerMessageProcessor: ListingMessageProcessorFlow - Listing API response Payload: {"listing":{"id":"1082835915","status":"ACTIVE"}}
INFO  2014-06-25 11:29:12,201 [[telecharge].connector.http.mule.default.receiver.04] org.mule.api.processor.LoggerMessageProcessor: PushInventoryFlow - Number of listing requests found after bunlding : 7 - current request number : 2
INFO  2014-06-25 11:29:12,202 [[telecharge].connector.http.mule.default.receiver.04] org.mule.api.processor.LoggerMessageProcessor: ListingMessageProcessorFlow - Invoking listingAPI for Section: [CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA] with request payload : <listing><eventId>4457573</eventId><eventDescription>The Book of Mormon Houston</eventDescription><pricePerTicket><amount>902</amount><currency>USD</currency></pricePerTicket><quantity>1</quantity><section>CENTER ORCHESTRA</section><rows>M</rows><seats>1</seats><splitOption>NONE</splitOption></listing>
INFO  2014-06-25 11:29:12,695 [[telecharge].connector.http.mule.default.receiver.04] org.mule.api.processor.LoggerMessageProcessor: ListingMessageProcessorFlow - Listing API response Payload: {"listing":{"id":"1082835916","status":"ACTIVE"}}
INFO  2014-06-25 11:29:12,695 [[telecharge].connector.http.mule.default.receiver.04] org.mule.api.processor.LoggerMessageProcessor: PushInventoryFlow - Number of listing requests found after bunlding : 7 - current request number : 3
INFO  2014-06-25 11:29:12,697 [[telecharge].connector.http.mule.default.receiver.04] org.mule.api.processor.LoggerMessageProcessor: ListingMessageProcessorFlow - Invoking listingAPI for Section: [CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA] with request payload : <listing><eventId>4457573</eventId><eventDescription>The Book of Mormon Houston</eventDescription><pricePerTicket><amount>905</amount><currency>USD</currency></pricePerTicket><quantity>2</quantity><section>CENTER ORCHESTRA</section><rows>M</rows><seats>4,6</seats><splitOption>NONE</splitOption></listing>

有人可以帮我指出这个问题。仅供参考,我在该 xpath 值上遇到了 set-variable 的不同问题。在这一点上,我想看到对于每次调用,我都维护单个拆分消息具有的值,但在这里它获取所有拆分消息的值。

我的问题是这个值 - 只是为了更多地解释它

ListingMessageProcessorFlow - Invoking listingAPI for Section:

[CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA, CENTER ORCHESTRA]

带有请求有效负载:

<listing><eventId>4457573</eventId><eventDescription>The Book of Mormon Houston</eventDescription><pricePerTicket><amount>900</amount><currency>USD</cur‌​rency></pricePerTicket><quantity>1</quantity><section>CENTER ORCHESTRA</section><rows>M</rows><seats>2</seats><splitOption>NONE</splitOption>‌​&lt;/listing>
4

1 回答 1

0

似乎您可以通过 Anypoint 3.5 或 3.6 做一些事情来改进这一点。

  1. 使用xpath('<expression>', payload)而不是xpath('<expression>')作为拆分器表达式。拆分器在将有效负载拆分为块方面仍然可以正常工作,但是,子流中的 xpath 调用对原始有效负载的当前块起作用,而不是像在您的示例中那样针对整个原始有效负载。@Ryan Carter 在评论中提到了这一点,并且仍然适用于 Anypoint Studio 3.5.2。
  2. 使用与@David Dossot 提到的您正在设置的项目相对应的当前版本适当的设置器组件。设置流变量<set-variable>对应于您当前正在执行的操作。
    • <set-property>用于设置出站属性
    • <set-variable>用于设置流量变量
    • <set-session-variable>用于设置会话变量
  3. 使用<foreach>范围而不是拆分器聚合器模式。使用 XML 的拆分器聚合器的缺点是您获得了 DOM 对象的集合,您必须将其合并为单个对象,然后再发送回调用者。如果您使用 foreach,那么您可以修改集合中的现有对象,因为 foreach 使用对原始有效负载的引用。
    • 如何将 XPath 与 foreach 一起使用
    • 如果您需要替换 foreach 集合中当前项目的内容,您可以使用rootMessage.payload[counter-1] = payload;where 右侧的有效负载是当前 for-each 迭代的有效负载,仅对该迭代有效,而 rootMessage.payload 是整个收藏。您可以四处看看它与 foreach 和 DOM 有效负载和 XPath 集合的工作情况。
于 2015-02-16T05:39:57.020 回答