在mule esb中使用for-each遍历消息有效负载

时间:2015-01-22 18:41:31

标签: xpath mule

我们有一个骡子消息(一个Web服务响应)太冗长了。如下所示

<root>
    <record></record>
    <record></record>
    .
    .
    .
    .
    3000 like this
</root>

我们正在使用mule for-each,批量大小为1000.因此,我们将分3批将消息发送到下一个消息处理器。对于每个批处理,如果我们检查ElementNsImpl类型的有效负载(Apache xerces)。现在我们想对每个批次采取如下行动。

  

每个批次(包含1000条记录)必须再次拆分为1000条记录,以便我们一次处理一条记录。 (由于我们有其他约束,我们无法对3000条记录执行此操作)。

要将批次剪切为1000个单独的记录,我们希望再次使用for-each或splitter。但是这个批处理是一组没有根节点的1000条记录。对于每个或者分离器或者在XPath上工作的任何东西都不能正常工作。在mule中有没有最安全的方法来打破没有根节点的1000记录集到1000个虚拟记录?

2 个答案:

答案 0 :(得分:1)

问题如下......

<flow name="foreachFlow">
    <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="81" path="foreachtest" />
    <mulexml:dom-to-xml-transformer />
    <async>
        <foreach batchSize="3" counterVariableName="counter" collection="#[xpath://messageTO]">
            <logger level="INFO" message="#[groovy:message.getPayloadForLogging()]"/>
            <flow-ref name="nextFlow" />
        </foreach>
    </async>
    <set-payload value="some payload sent back"/>
</flow>

<flow name="nextFlow">
    <foreach batchSize="2" counterVariableName="counter" collection="#[xpath://messageTO]">
        <flow-ref name="nextFlow1" />
    </foreach>
</flow>

<flow name="nextFlow1">
    <foreach batchSize="1" counterVariableName="counter" collection="#[xpath://messageTO]">
        <logger level="INFO" message="#[groovy:message.getPayloadForLogging()]"/>
    </foreach>
</flow>

foreachFlow接收如下所示的有效负载

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://www.webserviceX.NET">
   <soapenv:Header/>
   <soapenv:Body>
      <web:GetCitiesByCountry>
         <messageTO>1</messageTO>
         <messageTO>2</messageTO>
         <messageTO>3</messageTO>
         <messageTO>4</messageTO>
         <messageTO>5</messageTO>
         <messageTO>6</messageTO>
         <messageTO>7</messageTO>
         <messageTO>8</messageTO>
         <messageTO>9</messageTO>
         <messageTO>10</messageTO>
      </web:GetCitiesByCountry>
   </soapenv:Body>
</soapenv:Envelope>   

并且第一个foreach中的有效负载是带有elementnsimpl对象的arraylist,如下所示

INFO  2015-01-24 14:49:14,663 [[testmuleproject].foreachFlow.1.02]         org.mule.api.processor.LoggerMessageProcessor: [
org.mule.DefaultMuleMessage
{
  id=1042f970-a3aa-11e4-829b-74d435d3375f
  payload=org.apache.xerces.dom.ElementNSImpl
  correlationId=<not set>
  correlationGroup=-1
  correlationSeq=-1
  encoding=UTF-8
  exceptionPayload=<not set>

Message properties:
  INVOCATION scoped properties:
  INBOUND scoped properties:
  OUTBOUND scoped properties:
  SESSION scoped properties:
}, 
org.mule.DefaultMuleMessage
{
  id=1042f971-a3aa-11e4-829b-74d435d3375f
  payload=org.apache.xerces.dom.ElementNSImpl
  correlationId=<not set>
  correlationGroup=-1
  correlationSeq=-1
  encoding=UTF-8
  exceptionPayload=<not set>

Message properties:
  INVOCATION scoped properties:
  INBOUND scoped properties:
  OUTBOUND scoped properties:
  SESSION scoped properties:
}, 
org.mule.DefaultMuleMessage
{
  id=1042f972-a3aa-11e4-829b-74d435d3375f
  payload=org.apache.xerces.dom.ElementNSImpl
  correlationId=<not set>
  correlationGroup=-1
  correlationSeq=-1
  encoding=UTF-8
  exceptionPayload=<not set>

Message properties:
  INVOCATION scoped properties:
  INBOUND scoped properties:
  OUTBOUND scoped properties:
  SESSION scoped properties:
}]

所以最后的问题是如何将这些处理批量处理到nextFlow1,最后我应该在nextFlow1中打印messageTO有效负载

目前mule抛出此设计的异常,如下所示

    ERROR 2015-01-24 15:12:20,635 [[testmuleproject].nextFlow.stage1.05] org.mule.exception.DefaultMessagingExceptionStrategy: 
********************************************************************************
Message               : Could not find a transformer to transform "CollectionDataType{type=java.util.ArrayList, itemType=java.lang.Object, mimeType='text/xml'}" to "SimpleDataType{type=org.w3c.dom.Document, mimeType='*/*'}".
Code                  : MULE_ERROR-236
--------------------------------------------------------------------------------
Exception stack is:
1. Could not find a transformer to transform "CollectionDataType{type=java.util.ArrayList, itemType=java.lang.Object, mimeType='text/xml'}" to "SimpleDataType{type=org.w3c.dom.Document, mimeType='*/*'}". (org.mule.api.transformer.TransformerException)
  org.mule.registry.MuleRegistryHelper:268 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/transformer/TransformerException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.api.transformer.TransformerException: Could not find a transformer to transform "CollectionDataType{type=java.util.ArrayList, itemType=java.lang.Object, mimeType='text/xml'}" to "SimpleDataType{type=org.w3c.dom.Document, mimeType='*/*'}".
    at org.mule.registry.MuleRegistryHelper.lookupTransformer(MuleRegistryHelper.java:268)
    at org.mule.DefaultMuleMessage.getPayload(DefaultMuleMessage.java:368)
    at org.mule.DefaultMuleMessage.getPayload(DefaultMuleMessage.java:323)
    + 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)
********************************************************************************

答案 1 :(得分:0)

使用表达式组件将当前有效负载转换为具有如下所示的根节点

<expression-component >payload = "&lt;someRoot>"+payload+"&lt;/someRoot>"</expression-component>

这只会将ur payload视为字符串并附加根元素。现在,您可以在其上应用xpath。