拆分后的Mule消息属性在一个字段中显示拆分消息的所有属性值

时间:2014-06-25 18:38:03

标签: mule mule-studio mule-el

我们正在尝试一种方案,我们在一个有效负载中发送请求列表,根据某个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>‌​</listing>

1 个答案:

答案 0 :(得分:0)

似乎您可以采取一些措施来改善Anypoint 3.5或3.6。

  1. 使用xpath('<expression>', payload)代替xpath('<expression>')作为拆分器表达式。在将有效负载拆分为块时,拆分器仍然可以正常工作,但是,子流中的xpath调用对原始有效负载的当前块工作,而不是像在示例中那样对整个原始有效负载工作。 @Ryan Carter的评论中提到了这一点,并且仍然适用于Anypoint Studio 3.5.2。
  2. 使用当前版本的相应setter组件,这些组件对应于您提到的项目@David Dossot。设置流变量<set-variable>对应于您当前正在执行的操作。
    • <set-property>用于设置出站属性
    • <set-variable>用于设置流量变量
    • <set-session-variable>用于设置会话变量
  3. 使用<foreach>范围代替分割器聚合器模式。使用XML的splitter-aggregator的缺点是你得到一个DOM对象的集合,你必须在发送回调用者之前将它们合并到一个对象中。如果使用foreach,则可以修改集合中的现有对象,因为foreach使用对原始有效内容的引用。
    • How to use XPath with foreach
    • 如果您需要替换foreach集合中当前项目的内容,您可以使用rootMessage.payload[counter-1] = payload;,其中右侧有效负载是当前for-each迭代的有效负载,仅对该有效负载有效。迭代和rootMessage.payload是整个集合。您可以使用foreach以及DOM有效负载和XPath集合来查看它的工作情况。